{"id":3444,"date":"2019-09-08T07:35:20","date_gmt":"2019-09-08T05:35:20","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3444"},"modified":"2019-09-09T08:14:02","modified_gmt":"2019-09-09T06:14:02","slug":"a-matrix-wallpaper-with-python-and-pil-1","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/a-matrix-wallpaper-with-python-and-pil-1\/","title":{"rendered":"A matrix wallpaper with Python and PIL &#8211; 1"},"content":{"rendered":"<p>Let&#8217;s start trying to make a <strong>wallpaper<\/strong> with a <strong>matrix<\/strong> code kind of things using <strong>PIL<\/strong> to make interesting effects. Trying to make it with a program like inkscape could be a little hard, if you are not so skilled, because soon there will be too many suff to handle. Otherwise, we want to experiment new stuff coding, so here is an example of how to make it with python and PIL. The code is very basic, but it could be more complex if we spend some more time with it with some interesting ideas. Maybe we can add some other interesting effects in the future. For now, this is what we achieved.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3446\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix.png 800w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix-320x240.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix-768x576.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>It is something extremely basic. In fact, we made it with a couple of lines of code as you can se in this post below and in the video too.<\/p>\n<p>The code is this:<\/p>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw, ImageFilter\r\nfrom random import randint\r\ni = Image.new(\"RGB\", (800,600), \"black\")\r\nd = ImageDraw.Draw(i)\r\nw,h = i.size\r\nfor x in range(0, w, 8):\r\n    for y in range(0, h, 8):\r\n        bool = randint(0,1)\r\n        d.text((x,y), str(bool), \"lime\") # left corner up (h=0);\r\ni = i.filter(ImageFilter.SMOOTH)\r\ni.save(\"base\\\\matrix.png\")\r\ni.show()<\/pre>\n<h2>The filters<\/h2>\n<p>The filters that you can use with ImageFilter are these:<\/p>\n<ul class=\"simple\">\n<li><strong>BLUR<\/strong><\/li>\n<li><strong>CONTOUR<\/strong><\/li>\n<li><strong>DETAIL<\/strong><\/li>\n<li><strong>EDGE_ENHANCE<\/strong><\/li>\n<li><strong>EDGE_ENHANCE_MORE<\/strong><\/li>\n<li><strong>EMBOSS<\/strong><\/li>\n<li><strong>FIND_EDGES<\/strong><\/li>\n<li><strong>SHARPEN<\/strong><\/li>\n<li><strong>SMOOTH<\/strong><\/li>\n<li><strong>SMOOTH_MORE<\/strong><\/li>\n<\/ul>\n<p>For more information go to the <a href=\"https:\/\/pillow.readthedocs.io\/en\/5.1.x\/reference\/ImageFilter.html\">official documentation page<\/a> here.<\/p>\n<p>For a quick cheat sheet about <a href=\"https:\/\/pil.glitch.me\/\">PIL go in my glitch page<\/a> here.<\/p>\n<h2>The live coding video<\/h2>\n<p><iframe loading=\"lazy\" title=\"Make your own matrix wallpaper using Python code\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/HK63qU3Q5aU?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2>Mod 1: opacity<\/h2>\n<p>Whit the following code the wallpaper changes its look, using different opacity for each 0 and 1.<\/p>\n<pre class=\"lang:default decode:true \">from PIL import Image, ImageDraw, ImageFilter\r\nfrom random import randint, random\r\ni = Image.new(\"RGBA\", (800,600), \"black\")\r\nd = ImageDraw.Draw(i)\r\nw,h = i.size\r\nfor x in range(0, w, 8):\r\n    for y in range(0, h, 8):\r\n        boolean = randint(0,1)\r\n        bool2 = int(random()*100)\r\n        d.text((x,y), str(boolean), (0,230,0,bool2)) # left corner up (h=0);\r\n#i = i.filter(ImageFilter.SMOOTH)\r\ni.save(\"base\\\\matrix2.png\")\r\ni<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3482\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6.jpg\" alt=\"\" width=\"998\" height=\"236\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6.jpg 998w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6-320x76.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6-768x182.jpg 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix6-960x227.jpg 960w\" sizes=\"auto, (max-width: 998px) 100vw, 998px\" \/><\/a><\/p>\n<h2>Mod 2: colors and numbers<\/h2>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw, ImageFilter, ImageFont\r\nfrom random import randint, random\r\ni = Image.new(\"RGBA\", (800,600), \"black\")\r\nnum =20\r\nfont = ImageFont.truetype(\"impact.ttf\", num)\r\nd = ImageDraw.Draw(i)\r\nw,h = i.size\r\nfor x in range(0, w, num-8):\r\n    for y in range(0, h, num-5):\r\n        boolean = randint(0,10)\r\n        bool2 = int(random()*100)\r\n        d.text((x,y), str(boolean), (y\/\/4,230-x\/\/4,x\/\/4,bool2), font=font) # left corner up (h=0);\r\ni = i.filter(ImageFilter.SMOOTH)\r\ni.save(\"base\\\\matrix3.png\")\r\ni<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3483\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7.jpg\" alt=\"\" width=\"999\" height=\"243\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7.jpg 999w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7-320x78.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7-768x187.jpg 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/matrix7-960x234.jpg 960w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\" \/><\/a><\/p>\n<h2>Mod 3: letters and numbers carnival<\/h2>\n<pre class=\"lang:default decode:true \">from PIL import Image, ImageDraw, ImageFilter, ImageFont\r\nfrom random import randint, random, choice\r\ni = Image.new(\"RGBA\", (800,600), \"black\")\r\nnum = 28\r\nfont = ImageFont.truetype(\"impact.ttf\", num)\r\nd = ImageDraw.Draw(i)\r\nw,h = i.size\r\nfor x in range(0, w, num\/\/2):\r\n    for y in range(0, h, num\/\/4):\r\n        boolean = choice(\"abcdefg123456ghtnjkdfpeod\u00f2wpekccndzwxqr\")\r\n        opacity = 255#int(random()*100)\r\n        ca, cb, cc = [randint(0,255) for x in range(3)]\r\n        d.text((x,y), str(boolean), (ca,cb,cc,opacity), font=font) # left corner up (h=0);\r\n#i = i.filter(ImageFilter.SMOOTH)\r\n#i = i.filter(ImageFilter.CONTOUR)\r\n#i = i.filter(ImageFilter.SMOOTH)\r\ni.save(\"base\\\\matrix3.png\")\r\ni<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/carnival.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3484\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/carnival.jpg\" alt=\"\" width=\"857\" height=\"184\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/carnival.jpg 857w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/carnival-320x69.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/carnival-768x165.jpg 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/a><\/p>\n<h2>Mod 4: sparse<\/h2>\n<pre class=\"lang:default decode:true \">from PIL import Image, ImageDraw, ImageFilter, ImageFont\r\nfrom random import randint, random, choice\r\ni = Image.new(\"RGBA\", (800, 300), \"black\")\r\nnum = 24\r\nfont = ImageFont.truetype(\"impact.ttf\", num)\r\nd = ImageDraw.Draw(i)\r\nw,h = i.size\r\nfor x in range(0, w, num\/\/2):\r\n    for y in range(0, h, num\/\/2):\r\n        x1 = randint(10, 790)\r\n        y1 = randint(10, 590)\r\n        num = randint(12,28)\r\n        boolean = choice(\"ab cdefg1 23456ghtnjkdfpeo d\u00f2wpek ccndz wxqr\")\r\n        opacity = 100 + int(random()*155)\r\n        ca, cb, cc = [randint(0,255) for x in range(3)]\r\n        d.text((x1,y1), str(boolean), (ca,cb,cc,opacity), font=ImageFont.truetype(\"impact.ttf\", num)) # left corner up (h=0);\r\n#i = i.filter(ImageFilter.SMOOTH)\r\n# i = i.filter(ImageFilter.CONTOUR)\r\n#i = i.filter(ImageFilter.SMOOTH)\r\ni.save(\"base\\\\matrix3.png\")\r\ni<\/pre>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/sparse.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3485\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/sparse.jpg\" alt=\"\" width=\"859\" height=\"306\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/sparse.jpg 859w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/sparse-320x114.jpg 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/sparse-768x274.jpg 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/a><\/p>\n<p>You can make a lot of effect changing some parameters in the code, so express your creativity making some incredible visual effect.<\/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":"Make wallpapers with Python and PIL\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/a-matrix-wallpaper-with-python-and-pil-1\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3454,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[540,161,4,541],"class_list":["post-3444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","tag-matrix","tag-pil","tag-python","tag-wallpapers"],"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\/3444","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=3444"}],"version-history":[{"count":7,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3444\/revisions"}],"predecessor-version":[{"id":3486,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3444\/revisions\/3486"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3454"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}