{"id":2524,"date":"2019-07-27T09:02:46","date_gmt":"2019-07-27T07:02:46","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2524"},"modified":"2019-07-28T17:54:56","modified_gmt":"2019-07-28T15:54:56","slug":"cheat-sheet-for-pythons-pil-module","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/cheat-sheet-for-pythons-pil-module\/","title":{"rendered":"Cheat Sheet for Python&#8217;s PIL module"},"content":{"rendered":"<p>We are looking into PIL library lately, so I thought it could be nice to have a sort of cheat sheet of the functions and classes we&#8217;ve used in our code.<\/p>\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>What is PIL<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>PIL is a powerful module for Python that allows you to create and elaborate images by conding in Python. You can do almost anything, building the perfect image tools for your needs.<\/p>\n<p>In this tutorial you will be guided through the most interesting tools you can use with a lot of code examples, to avoid being stuck with theory.<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Install<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>First you need to install pil&#8217;s fork pillow:<\/p>\n<p>pip install pillow<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Import<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>from PIL import Image<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Create<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img = Image.new(&#8216;RBG&#8217;, (600,400), &#8216;yellow&#8217;)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Open<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img = Image.open(&#8216;existing.png&#8217;)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Save<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img.save(&#8216;myimage.png&#8217;)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Show<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img.show()<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Resize<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img.resize((100,100), Image.ANTIALIAS)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Blur (from PIL import ImageFilter)<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img.filter.(ImageFilter.BLUR)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Blend 2 images together<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img = Image.blend(Image.open(&#8216;image1.png&#8217;,&#8217;image2.png&#8217;, 0.5))<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Pasting an image on another<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>img.paste((0,0),&#8217;image2.png&#8217;)<\/p>\n<\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-md-4\">\n<h3>Write text on an image (ImageDraw)<\/h3>\n<\/div>\n<div class=\"col-md-8\">\n<p>draw = ImageDraw.Draw(img)<\/p>\n<p>draw.text(0,0,&#8217;This text goes on top of the image&#8217;)<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Cheat sheet for PIL\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/cheat-sheet-for-pythons-pil-module\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2528,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[464],"class_list":["post-2524","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","tag-cheat-sheet"],"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\/2524","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=2524"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2524\/revisions"}],"predecessor-version":[{"id":2542,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2524\/revisions\/2542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2528"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}