{"id":2408,"date":"2019-07-21T11:50:58","date_gmt":"2019-07-21T09:50:58","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2408"},"modified":"2019-07-22T09:03:25","modified_gmt":"2019-07-22T07:03:25","slug":"pil-create-a-transparent-image","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/pil-create-a-transparent-image\/","title":{"rendered":"PIL => create a transparent image"},"content":{"rendered":"<p>To create a transparent png with PIL you can use this code, as example. In this particular example I joined 2 transparent png and pasting them side by side on a new transparent image of the same size of the two together.<\/p>\n<p>The process in the code is this:<\/p>\n<ul>\n<li>we create a new image (Image.new) with &#8220;RGBA&#8221; as first argument and (0,0,0,0) as third argument<\/li>\n<li>then we open two png (transparent)<\/li>\n<li>we past the 2 element one next to the other<\/li>\n<\/ul>\n<p>First Image<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/fodler.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2410\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/fodler.png\" alt=\"\" width=\"195\" height=\"247\" \/><\/a><\/p>\n<p>second image<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2411\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/py.png\" alt=\"\" width=\"144\" height=\"144\" \/><\/a><\/p>\n<pre class=\"lang:default decode:true \"># combine 2 png images horizzontally\r\n\r\nfrom PIL import Image\r\n\r\ndef combine(i1,i2):\r\n    i1 = Image.open(i1)\r\n    i2 = Image.open(i2)\r\n    x1, y1 = i1.size\r\n    x2, y2 = i2.size\r\n    x3 = x1 + x2\r\n    y3 = y1 if y1 &gt; y2 else y2\r\n    i3 = Image.new(\"RGBA\",(x3,y3), (0,0,0,0))\r\n    if y1 &gt; y2:\r\n        y2 = (y1 - y2) \/\/ 2\r\n    else:\r\n        y2 = y1\r\n    i3.paste(i1, (0,0), i1)\r\n    i3.paste(i2, (x1,y2), i2)\r\n    return i3\r\n\r\ni = combine(\"folder.png\",\"py.png\")\r\ni.save(\"folder.png\", \"PNG\")\r\ni<\/pre>\n<p>The output (images 1 and 2 side by side) on a transparent background<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/pil5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2409\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/pil5.png\" alt=\"\" width=\"339\" height=\"247\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/pil5.png 339w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/pil5-320x233.png 320w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Make transparent images with PIL joining more transparent images\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/pil-create-a-transparent-image\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2419,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[161,4,454],"class_list":["post-2408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","tag-pil","tag-python","tag-transparent-images"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2408"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2408\/revisions"}],"predecessor-version":[{"id":2418,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2408\/revisions\/2418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2419"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}