{"id":1467,"date":"2018-10-20T19:07:05","date_gmt":"2018-10-20T17:07:05","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1467"},"modified":"2019-08-07T07:44:56","modified_gmt":"2019-08-07T05:44:56","slug":"make-an-image-with-text-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/make-an-image-with-text-with-python\/","title":{"rendered":"Make an Image with text with Python"},"content":{"rendered":"<h1>Create an image with PIL<\/h1>\n<p><strong>Python<\/strong> is great at many things, expecially for repetitive things. We can use it to gain a lot of time, and we all are aware that today time is the most evaluable thing, because things go faster faster in today&#8217;s life.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/p3.pstatp.com\/large\/46f70005a7cb680e50a5\" alt=\"Related image\" \/><\/p>\n<h2>A practical example: make an image<\/h2>\n<p>In this code we will create images. From scratch. I thought to make this code to use it on the web pages to enphatize something with a nice image of a text. This code will show you a nice red rectangle.<\/p>\n<h2>Image.new<\/h2>\n<p>This will create a new image with color size and color as attributes.<\/p>\n<h2>Image.save<\/h2>\n<p>This save the image create with new.<\/p>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw\r\nimport os\r\n\r\n# name of the file to save\r\nfilename = \"img01.png\"\r\n\r\n# create new image\r\nimage = Image.new(mode = \"RGB\", size = (200,70), color = \"red\")\r\n\r\n# save the file\r\nimage.save(filename)\r\n\r\n# open the file\r\nos.system(filename)<\/pre>\n<h2>Write some text in it<\/h2>\n<p>Now we must use the <strong>ImageDraw.Draw<\/strong> class of PIL. The istance will be called <strong>draw<\/strong> (with a lot of fantasy).<\/p>\n<h2>ImageDraw.Draw<\/h2>\n<p>We will put as argument of this class the image object created with <strong>Image.new<\/strong>.<\/p>\n<h2>text<\/h2>\n<p>Finally we will use the<strong> method text<\/strong> of the ImageDraw.Draw object created to add the text to the image. This takes as arguments the position (a tuple), the text (string) and another tuple for the color.<\/p>\n<h2>The complete code<\/h2>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw, ImageFont\r\nimport os\r\n\r\n# name of the file to save\r\nfilename = \"img01.png\"\r\nfnt = ImageFont.truetype('arial.ttf', 15)\r\n# create new image\r\nimage = Image.new(mode = \"RGB\", size = (200,70), color = \"red\")\r\ndraw = ImageDraw.Draw(image)\r\ndraw.text((10,10), \"My Text\", font=fnt, fill=(255,255,0))\r\nimage.save(filename)\r\n\r\nos.system(filename)<\/pre>\n<figure id=\"attachment_1481\" aria-describedby=\"caption-attachment-1481\" style=\"width: 500px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/text_on_img.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1481\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/text_on_img.png\" alt=\"The text on the created red image\" width=\"500\" height=\"324\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/text_on_img.png 500w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/text_on_img-320x207.png 320w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1481\" class=\"wp-caption-text\">The text on the created red image<\/figcaption><\/figure>\n<h2>A function to make changes easy<\/h2>\n<pre class=\"lang:default decode:true \">from PIL import Image, ImageDraw, ImageFont\r\nimport os\r\n\r\ndef text_on_img(filename='01.png', text=\"Hello\", size=12):\r\n\t\"Draw a text on an Image, saves it, show it\"\r\n\tfnt = ImageFont.truetype('arial.ttf', 52)\r\n\t# create image\r\n\timage = Image.new(mode = \"RGB\", size = (200,70), color = \"red\")\r\n\tdraw = ImageDraw.Draw(image)\r\n\t# draw text\r\n\tdraw.text((10,10), text, font=fnt, fill=(255,255,0))\r\n\t# save file\r\n\timage.save(filename)\r\n\t# show file\r\n\tos.system(filename)\r\n\r\n\r\ntext_on_img(text=\"Text\", size=52)<\/pre>\n<h2>Let&#8217;s adapt the width of the img to the lenght of the text<\/h2>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw, ImageFont\r\nimport os\r\n\r\ndef text_on_img(filename='01.png', text=\"Hello\", size=12):\r\n\t\"Draw a text on an Image, saves it, show it\"\r\n\tfnt = ImageFont.truetype('arial.ttf', size)\r\n\t# create image\r\n\timage = Image.new(mode = \"RGB\", size = (int(size\/2)*len(text),size+50), color = \"red\")\r\n\tdraw = ImageDraw.Draw(image)\r\n\t# draw text\r\n\tdraw.text((10,10), text, font=fnt, fill=(255,255,0))\r\n\t# save file\r\n\timage.save(filename)\r\n\t# show file\r\n\tos.system(filename)\r\n\r\n\r\ntext_on_img(text=\"Text to write on img\", size=300)<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1482\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/02.png\" alt=\"\" width=\"510\" height=\"509\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/02.png 510w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/02-150x150.png 150w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/02-320x319.png 320w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/a><\/p>\n<h2>Let&#8217;s make background and color avaiable<\/h2>\n<pre class=\"lang:default decode:true \">from PIL import Image, ImageDraw, ImageFont\r\nimport os\r\n\r\ndef text_on_img(filename='01.png', text=\"Hello\", size=12, color=(255,255,0), bg='red'):\r\n\t\"Draw a text on an Image, saves it, show it\"\r\n\tfnt = ImageFont.truetype('arial.ttf', size)\r\n\t# create image\r\n\timage = Image.new(mode = \"RGB\", size = (int(size\/2)*len(text),size+50), color = bg)\r\n\tdraw = ImageDraw.Draw(image)\r\n\t# draw text\r\n\tdraw.text((10,10), text, font=fnt, fill=(255,255,0))\r\n\t# save file\r\n\timage.save(filename)\r\n\t# show file\r\n\tos.system(filename)\r\n\r\n\r\ntext_on_img(text=\"Text to write on img\", size=300, bg='red')<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1484\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03.png\" alt=\"\" width=\"1550\" height=\"150\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03.png 1550w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03-320x31.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03-768x74.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/10\/03-960x93.png 960w\" sizes=\"auto, (max-width: 1550px) 100vw, 1550px\" \/><\/a><\/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","protected":false},"excerpt":{"rendered":"How to make an image with text with Python for many purposes.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/make-an-image-with-text-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,178],"tags":[99,161,116,299],"class_list":["post-1467","post","type-post","status-publish","format-standard","hentry","category-examples","category-utility","tag-image","tag-pil","tag-text","tag-text-on-image"],"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\/1467","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=1467"}],"version-history":[{"count":6,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1467\/revisions"}],"predecessor-version":[{"id":2721,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1467\/revisions\/2721"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}