{"id":3870,"date":"2019-10-03T08:03:59","date_gmt":"2019-10-03T06:03:59","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3870"},"modified":"2019-10-03T19:18:41","modified_gmt":"2019-10-03T17:18:41","slug":"a-brief-guide-to-pil-python-image-library","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/a-brief-guide-to-pil-python-image-library\/","title":{"rendered":"A brief guide to PIL, python image library"},"content":{"rendered":"<div class=\"container\">\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>What is PIL<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p><b>PIL<\/b> is a powerful module for <b>Python<\/b> that allows you to create and elaborate <b>images<\/b> by conding in Python. You can do almost anything, building the perfect image tools for your needs.We will take a fast look at the main functions of this very useful module.<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Install<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>First you need to install pil&#8217;s fork pillow:pip install pillow<img decoding=\"async\" src=\"https:\/\/cdn.glitch.com\/8f79309f-0a88-41bb-bfd9-ccac7007a188%2Fpip_install_pil.PNG?v=1569514131173\" \/><\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Import<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>The first thing you need to import is the Class Image<br \/>\n&gt;&gt;&gt; <b style=\"color: orange;\">from <\/b>PIL <b style=\"color: orange;\">import <\/b>Image<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Create<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img = Image.<b style=\"color: blue;\">new<\/b>(&#8216;RBG&#8217;, (200,200), &#8216;yellow&#8217;)<\/p>\n<p>This will <b>create<\/b> a yellow square image of 200 px of <b>width<\/b> and 200 px of <b>height.<\/b> <b>RGB<\/b> is for Red, Green and Blue, aka the image is in color. If you want to use <b>transparency<\/b> you have to use <b>RGBA<\/b> instead of RGB.<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Open<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img = Image.<b style=\"color: blue;\">open<\/b>(&#8216;existing.png&#8217;)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Save<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img.<b style=\"color: blue;\">save<\/b>(&#8216;myimage.png&#8217;)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Show<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img.<b style=\"color: blue;\">show<\/b>()<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Resize<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img.<b style=\"color: blue;\">resize<\/b>((100,100), Image.ANTIALIAS)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Filters for the images<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img.<b style=\"color: blue;\">filter<\/b>(ImageFilter.BLUR)<\/p>\n<p>There&#8217;s a much smoother blur called SMOOTH<br \/>\n&gt;&gt;&gt; i = i.<b style=\"color: blue;\">filter<\/b>(ImageFilter.SMOOTH )<\/p>\n<p>You can then create a &#8216;contour&#8217; effect<br \/>\n&gt;&gt;&gt; i = i.<b style=\"color: blue;\">filter<\/b>( ImageFilter.CONTOUR )<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Blend 2 images together<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img = Image.<b style=\"color: blue;\">blend<\/b>(Image.<b style=\"color: blue;\">open<\/b>(&#8216;image1.png&#8217;,&#8217;image2.png&#8217;, 0.5) )<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Pasting an image on another<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; img.<b style=\"color: blue;\">paste<\/b>((0,0),&#8217;image2.png&#8217;)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Write text on an image (ImageDraw)<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; draw = ImageDraw.<b style=\"color: blue;\">Draw<\/b>(img)<br \/>\n&gt;&gt;&gt; draw.<b style=\"color: blue;\">text<\/b>(0,0,&#8217;Th<b style=\"color: orange;\">is <\/b>text goes on top of the image&#8217;)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Thumbnail<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>&gt;&gt;&gt; im.<b style=\"color: blue;\">thumbnail<\/b>((128, 128), Image.ANTIALIAS)<\/p>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<p><!-- iterate all posts keys\/values --><\/p>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>text and font<\/h3>\n<\/div>\n<p><!-- colonna titolo --><\/p>\n<div class=\"col-md-8\">\n<p>To add a <b>text<\/b> to an <b>image<\/b> after you created an image:<br \/>\n&gt;&gt;&gt; im = img.<b style=\"color: blue;\">new<\/b>(&#8220;RGBA&#8221;, (600,400), &#8220;yellow&#8221;)<\/p>\n<p>You create a <b style=\"color: blue;\">ImageDraw<\/b>.Draw object to wich you pass the <b style=\"color: blue;\">im<\/b> object you created and on wich you draw:<br \/>\n&gt;&gt;&gt; d = ImageDraw.<b style=\"color: blue;\">Draw<\/b>(im)<\/p>\n<p>Now you are ready to add a text to the <b style=\"color: blue;\">d<\/b> object, but you can define the <b>size<\/b> and <b>family<\/b> of the text, using the <b style=\"color: blue;\">ImageFont<\/b> <b>class<\/b> (that you need to import from PIL) together with the method <b style=\"color: blue;\">truetype:<\/b><\/p>\n<p><b style=\"color: blue;\"><br \/>\n&gt;&gt;&gt;<\/b> font = ImageFont.<b style=\"color: blue;\">truetype<\/b>(&#8220;Arial 20&#8221;)<\/p>\n<p>Now, you can finally add this font to the <b style=\"color: blue;\">font<\/b> <b style=\"color: blue;\">argument<\/b> when you add the text like this:<\/p>\n<p>&gt;&gt;&gt; d.<b style=\"color: blue;\">text<\/b>((10,10), &#8220;text to show&#8221;, (255,255,255), font=font)<\/p>\n<script>\r\nlet title = \"PIL - elaborate images\"\r\nlet links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/add-some-text-as-a-chunk-into-a-png-file-and-recover-it\/\",\"Input and recover text into a png with PIL\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/split-images-with-pil-aka-pillow-and-python-for-sprite-animation\/\",\"Crop Images\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/a-brief-guide-to-pil-python-image-library\/\",\"PIL GUIDE\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/a-matrix-wallpaper-with-python-and-pil-1\/\",\"Matrix wallpaper with PIL\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/cheat-sheet-for-pythons-pil-module\/\",\"Pil Cheat sheet\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/animated-cartoon-gif-with-pil-and-python-1\/\",\"Animated Gif\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/installing-pil-on-python-3-7-to-make-thumbnails\/\",\"Install PIL and make thumbnail\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/make-an-image-with-text-with-python\/\",\"Create an image with text\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/transform-a-png-in-a-thumbnail\/\",\"Make a thumbnail (2)\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/powerpoint-pil-png-animated-gif\/\",\"Gif with Powerpoint and PIL\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/resize-images-with-pil\/\",\"Resize Images\"],\r\n\t\r\n\t];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n<\/div>\n<p><!-- colonna paragrafo --><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Take a rapid look at the main features of PIL with clear examples to manipulate and create images with Python\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/a-brief-guide-to-pil-python-image-library\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3873,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[179],"tags":[218,584,306,161,162,4,116],"class_list":["post-3870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-images","tag-font","tag-imagedraw","tag-images","tag-pil","tag-pillow","tag-python","tag-text"],"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\/3870","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=3870"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3870\/revisions"}],"predecessor-version":[{"id":3896,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3870\/revisions\/3896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3873"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}