{"id":122470,"date":"2018-04-24T12:12:01","date_gmt":"2018-04-24T12:12:01","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=122470"},"modified":"2018-04-24T12:33:10","modified_gmt":"2018-04-24T12:33:10","slug":"python-imaging-librarypil-examples","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/","title":{"rendered":"Python Imaging Library-(PIL) Examples"},"content":{"rendered":"<p>The PIL(Python Imaging Library) is the python library which primarily uses for<br \/>\nfor the image manipulation like create, resize, merge etc.<\/p>\n<p>This blog post contains the code snippets related to PIL.<\/p>\n<h3>How to resize an image<\/h3>\n<pre class=\"brush:py\">import os\r\nfrom PIL import Image\r\nimg = Image.open(\"original.png\")\r\n#Open the original Image\r\nsize = img.size\r\nimg = img.resize(size,Image.ANTIALIAS)\r\n#Resize the original Image\r\nimg.save(\"result.jpg\",optimize=True,quality=95)\r\n#Save  the result image\r\n\r\n\r\n\r\n\r\n\r\n\r\n<\/pre>\n<h3>==&gt;How to add transparent image watermarkup on an image:<\/h3>\n<pre class=\"brush:py\">import os\r\nfrom PIL import Image\r\nimg = Image.open(\"original.jpg\") #Open the original Image\r\nwidth, height = img.size #Get the image width, height \r\nimage_mode = img.mode #Get the image mode \r\n\r\n\r\nwatermark = Image.open('watermark_image.jpg').convert(\"RGBA\")\r\n# watermark = watermark.resize((width, height), Image.ANTIALIAS)\r\nposition_left,position_top = 10 ,10 #Watermark position\r\nposition=(position_left,position_top)\r\norientation = 'horizontal' #Watermark orientation\r\nif orientation=='vertical':  # In case vertical, rotate the image by 45 degree\r\n    angle = 45\r\n    watermark = watermark.rotate( angle, expand=1 )\r\n    img.paste(watermark ,position,watermark)\r\nelse:\r\n    img.paste(im = watermark,box= position,mask=0)\r\n    \r\n    \r\ntransparent = Image.new(mode = 'RGBA', size = (width, height), color = (0,0,0,0))\r\n# Create a new transparent image\r\ntransparent.paste(img, (0,0))\r\n# paste the original image\r\n\r\ntransparent.paste(watermark, position, mask=watermark)\r\n# paste the watermark image\r\n\r\nif image_mode=='RGB':\r\n    transparent = transparent.convert(image_mode)\r\nelse:\r\n    transparent = transparent.convert('P')\r\ntransparent.save(\"result.jpg\",optimize=True,quality=95)\r\n#Save  the result image\r\n\r\n\r\n\r\n\r\n<\/pre>\n<h3>==&gt;How to add text watermarkup on an image:<\/h3>\n<pre class=\"brush:py\">from PIL import Image\r\nfrom PIL import Image\r\nfrom PIL import ImageEnhance\r\nfrom PIL import ImageDraw, ImageFont\r\nimg = Image.open(\"original.jpg\")\r\nmargin_left,margin_top = img.size\r\nwidth, height = img.size\r\nimage_mode = img.mode\r\n\r\nposition_left,position_top = 10 ,10\r\nposition=(position_left,position_top)\r\nfill = (0,0,0)\r\n#Watermark text color\r\nwatermark = Image.new(\"RGBA\", img.size, color = (0, 0, 0, 0))\r\n#Create a Watermark image\r\n\r\nwaterdraw = ImageDraw.ImageDraw(watermark, \"RGBA\")\r\n#Create  a waterdraw for text\r\n\r\nfont_path = 'Lato-HaiIta-webfont.ttf'\r\n#Image font path url\r\n\r\ntxt = 'example.com'\r\n#Watermark text\r\nsize = max(1, 100)\r\n\r\n#####################################\r\n# this part will reduce the watermark text size\r\nfontsize = 1\r\nimg_fraction = .40\r\nfont_obj = ImageFont.truetype(font_path, 1)\r\nwhile font_obj.getsize(txt)[0] &lt; img_fraction*margin_left:\r\n    fontsize += 1\r\n    font_obj = ImageFont.truetype(font_path, fontsize)\r\nfontsize -= 1\r\n#####################################\r\nfont_obj = ImageFont.truetype(font_path, fontsize)\r\n#Create the font obj with  fonth type and size\r\n\r\nwaterdraw.text(position, text = txt, fill=fill,font=font_obj )\r\npositioning = 'horizontal'\r\nif positioning=='vertical':\r\n    angle = 45\r\n    watermark = watermark.rotate( angle, expand=1 )\r\n\r\ntransparent_mode = 'RGBA' if image_mode=='P' else 'RGB'\r\n\r\ntransparent = Image.new(mode = transparent_mode, size =img.size, color = (0,0,0,0))\r\n#Create the  new image(result image)\r\ntransparent.paste(img, (0,0))\r\n#Paste the original image\r\ntransparent.paste(watermark, None, watermark)\r\n#Paste the watermark image\r\ntransparent.save(\"final.jpg\",optimize=True,quality=95)\r\n#Save  the result image\r\n<\/pre>\n<pre class=\"brush:py\"><\/pre>\n<p>That\u2019s all for today. I hope this blog will help you. I\u2019d be very grateful if you\u2019d write your opinions, comments, and suggestions to keep the page updated and interesting.<\/p>\n<p>You may also like our post on <a href=\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\">Using StringIO and BytesIO for managing data as <\/a>the file object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PIL(Python Imaging Library) is the python library which primarily uses for for the image manipulation like create, resize, merge etc. This blog post contains the code snippets related to PIL. How to resize an image import os from PIL import Image img = Image.open(&#8220;original.png&#8221;) #Open the original Image size = img.size img = img.resize(size,Image.ANTIALIAS) <a href=\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":86,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2383],"tags":[6580,3324],"class_list":["post-122470","post","type-post","status-publish","format-standard","hentry","category-blog","tag-pil","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Imaging Library-(PIL) Examples - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Imaging Library-(PIL) Examples - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"The PIL(Python Imaging Library) is the python library which primarily uses for for the image manipulation like create, resize, merge etc. This blog post contains the code snippets related to PIL. How to resize an image import os from PIL import Image img = Image.open(&quot;original.png&quot;) #Open the original Image size = img.size img = img.resize(size,Image.ANTIALIAS) [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-24T12:12:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-24T12:33:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prakash Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prakash Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\"},\"author\":{\"name\":\"Prakash Kumar\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\"},\"headline\":\"Python Imaging Library-(PIL) Examples\",\"datePublished\":\"2018-04-24T12:12:01+00:00\",\"dateModified\":\"2018-04-24T12:33:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\"},\"wordCount\":112,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"PIL\",\"python\"],\"articleSection\":[\"blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\",\"url\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\",\"name\":\"Python Imaging Library-(PIL) Examples - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-04-24T12:12:01+00:00\",\"dateModified\":\"2018-04-24T12:33:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Imaging Library-(PIL) Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\",\"name\":\"Prakash Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prakash Kumar\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Imaging Library-(PIL) Examples - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Imaging Library-(PIL) Examples - Webkul Blog","og_description":"The PIL(Python Imaging Library) is the python library which primarily uses for for the image manipulation like create, resize, merge etc. This blog post contains the code snippets related to PIL. How to resize an image import os from PIL import Image img = Image.open(\"original.png\") #Open the original Image size = img.size img = img.resize(size,Image.ANTIALIAS) [...]","og_url":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-04-24T12:12:01+00:00","article_modified_time":"2018-04-24T12:33:10+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prakash Kumar","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prakash Kumar","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/"},"author":{"name":"Prakash Kumar","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b"},"headline":"Python Imaging Library-(PIL) Examples","datePublished":"2018-04-24T12:12:01+00:00","dateModified":"2018-04-24T12:33:10+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/"},"wordCount":112,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["PIL","python"],"articleSection":["blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/","url":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/","name":"Python Imaging Library-(PIL) Examples - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-04-24T12:12:01+00:00","dateModified":"2018-04-24T12:33:10+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/python-imaging-librarypil-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Imaging Library-(PIL) Examples"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b","name":"Prakash Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prakash Kumar"},"url":"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=122470"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122470\/revisions"}],"predecessor-version":[{"id":122497,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/122470\/revisions\/122497"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=122470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=122470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=122470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}