{"id":8804,"date":"2024-12-07T01:01:00","date_gmt":"2024-12-07T06:01:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8804"},"modified":"2024-12-04T12:24:07","modified_gmt":"2024-12-04T17:24:07","slug":"remove-background-from-photos-for-holiday-card-creation-with-python","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8804","title":{"rendered":"Remove Background from Photos for Holiday Card Creation with Python"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8804\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>You can use open-source tools to produce holiday cards this year. Suppose you have a great photo of your family or grandchildren, but it was taken in a different season, and now you would like to use that photo with a different background. There is a Python module remgb that will do just that. First, you must set up a Python virtual environment and install the necessary extras to accomplish the task. I created a directory called <strong>rembg<\/strong>. I do that with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir rembg <\/code><\/pre>\n\n\n\n<p>Then I setup a Python virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python3 -m venv \/home\/don\/rembg<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Next I activate the environment with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>source \/home\/don\/rembg\/bin\/activate<\/code><\/code><\/pre>\n\n\n\n<p>Then I install <strong>rembg<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pip3 install rembg<\/code><\/code><\/pre>\n\n\n\n<p>I want to use rembg from the command line so I make the following additional installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip3 install \"rembg&#91;cli]\"<\/code><\/pre>\n\n\n\n<p>Then I install <strong>onnxruntime<\/strong> which is a <a href=\"https:\/\/github.com\/microsoft\/onnxruntime\">machine learning accelerator<\/a>. <\/p>\n\n\n\n<figure class=\"wp-block-image alignleft size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"1800\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/12\/grandparents-1.jpg\" alt=\"\" class=\"wp-image-8806\" style=\"width:278px;height:auto\" title=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pip3 install onnxruntime<\/code><\/code><\/pre>\n\n\n\n<p>Now I am ready to remove the background from the image that I have chosen. This is a recent picture of my wife and I taken in the fall of the year. I like the picture but I want it to have a festive background.<\/p>\n\n\n\n<p>I ensure my image is in the <strong>rembg<\/strong> folder and then execute the following command. The <strong>i<\/strong> switch means that I am operating at the file level.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>rembg i grandparents.jpg grandparents_no_bg.jpg<\/code><\/code><\/pre>\n\n\n\n<p>In the command above, I renamed the output file to have my original still, just in case I wanted to use it again. You can see below that the background has been removed from the image above.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"1800\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/12\/grandparents_no_bg.png\" alt=\"\" class=\"wp-image-8807\" style=\"width:257px;height:auto\"\/><\/figure>\n\n\n\n<p>I can create a nice background for my card with InkScape, add some festive lettering, and use a Pointsettia I downloaded from <a href=\"https:\/\/openclipart.org\">Openclipart.org<\/a>. The completed card is shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"343\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/12\/Christmas_Card2024.jpg\" alt=\"\" class=\"wp-image-8808\" style=\"width:524px;height:auto\" title=\"\"\/><\/figure>\n\n\n\n<p>Rembg is open source and has an&nbsp;MIT License. Onnxruntime is open source and has an&nbsp;<a href=\"https:\/\/github.com\/microsoft\/onnxruntime\/blob\/main\/LICENSE\">MIT License<\/a>.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use open-source tools to produce holiday cards this year. Suppose you have a great photo of<\/p>\n","protected":false},"author":32,"featured_media":8801,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,150,238],"tags":[660,152,659],"class_list":["post-8804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-programming","category-python","tag-holiday","tag-programming","tag-python"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8804"}],"version-history":[{"count":11,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8804\/revisions"}],"predecessor-version":[{"id":8842,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8804\/revisions\/8842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/8801"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}