{"id":13959,"date":"2020-09-05T20:41:29","date_gmt":"2020-09-05T20:41:29","guid":{"rendered":"https:\/\/holypython.com\/?p=13959"},"modified":"2022-08-27T11:47:42","modified_gmt":"2022-08-27T11:47:42","slug":"how-to-watermark-images-w-python-pil","status":"publish","type":"post","link":"https:\/\/holypython.com\/how-to-watermark-images-w-python-pil\/","title":{"rendered":"How to Watermark Images w\/Python (PIL)"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"13959\" class=\"elementor elementor-13959\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-f7ebf43 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f7ebf43\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-8eb02ce\" data-id=\"8eb02ce\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-53a9d42 elementor-widget elementor-widget-heading\" data-id=\"53a9d42\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">What's Watermarking?\n\n<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fc4958b elementor-widget elementor-widget-text-editor\" data-id=\"fc4958b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family );\">Watermark is a semi-transparent text or logo that sort of blends with an image in the background to make the image more difficult to copy or share without appropriate references.<\/span><\/p><p>Watermark is an old idea (usually applied to money, stamp or institutional papers) that&#8217;s been adapted nicely to the digital image practices.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-106e45d elementor-widget elementor-widget-text-editor\" data-id=\"106e45d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fc7ec1d elementor-widget elementor-widget-heading\" data-id=\"fc7ec1d\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Why does someone need Watermarking?<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-47f9dd1 elementor-widget elementor-widget-text-editor\" data-id=\"47f9dd1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family );\">The need for watermarks arises in situations like:<\/span><\/p><ul><li>When you&#8217;d like to make your mark on your digital product.<\/li><li>When you want to discourage copying of your material<\/li><li>To make copyright claims known without blocking image&#8217;s visibility<\/li><li>To communicate a message such as on digital images, pitch deck or business presentations: Confidentiality, Warning, Incomplete, TBD (to be decided) etc.<\/li><li>For marketing reasons<\/li><li>To gain more recognition\u00a0etc.<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ee5b41e elementor-widget elementor-widget-text-editor\" data-id=\"ee5b41e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>So, if you own or work with a business, website, blog, professional career, startup, book, e-book, reports or if you&#8217;re involved with digital photography at a professional level chances are very high that you will at some point need watermarking tools.\u00a0<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6b96224 elementor-widget elementor-widget-heading\" data-id=\"6b96224\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Why Watermark with Python or another programming language?<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-683bdc3 elementor-widget elementor-widget-text-editor\" data-id=\"683bdc3\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family );\">Python works phenomenally with Watermarking as it does in so many other occasions. We live in a competitive world and competing well also means optimizing your time, optimizing your routine tasks, decreasing labor and increasing intellectual work that creates value.<\/span><\/p><p>Watermarking can be time-consuming, tedious and distracting. Definitely not a very intellectual occupation. With coding you can make it a 1-click automated process saving you tons of time and focused work in future. This is the kind of optimization that makes individuals and businesses thrive.<\/p><p><span style=\"color: var( --e-global-color-text );\">You can fine tune many parameters: text color, transparency, randomness, location, transparency via alpha channel, image type, watermark message etc.<\/span><\/p><ul><li>You can formulate your watermarking process<ul><li>For instance, there is a big difference between saying <strong>place watermark<\/strong> in (300,200) coordinates and saying <strong>place watermark\u00a0<\/strong>in (image_height\/2, image_width\/2) You get the point right? This leads to automation and opens doors to a new world such as batch editing, scripting etc.<\/li><\/ul><\/li><li>You can create your own software<\/li><li>You can automate Watermarking process and make it a 1-click solution in future.<\/li><li>It&#8217;s much less resource-hungry compared to GUI alternatives like Photoshop or GIMP<\/li><li>If written well it won&#8217;t cause freezing or glitches as heavy GUI alternatives sometimes do<\/li><li>It&#8217;s a great way to practice coding , conceptualizing, planning, automation and intellectual thinking<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-6291d45 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6291d45\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b7a6b1d\" data-id=\"b7a6b1d\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-bf64e27 elementor-widget__width-auto elementor-widget elementor-widget-heading\" data-id=\"bf64e27\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Watermarking with Python Tutorial  (<i>Applying Text<\/i>)<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2f6305d elementor-widget elementor-widget-text-editor\" data-id=\"2f6305d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We&#8217;re going to use PIL imaging library of Python in this tutorial.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-95f8d5a elementor-widget elementor-widget-text-editor\" data-id=\"95f8d5a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Let&#8217;s first create a text on the image without transparency. We got an appetizing image that can be worked on.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7dcaa93 elementor-widget elementor-widget-image\" data-id=\"7dcaa93\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"636\" height=\"492\" src=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/cake-Custom2.jpg\" class=\"attachment-full size-full wp-image-15327\" alt=\"\" srcset=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/cake-Custom2.jpg 636w, https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/cake-Custom2-300x232.jpg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c6ce098 elementor-widget elementor-widget-text-editor\" data-id=\"c6ce098\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We&#8217;ll apply a text on the image in about 7 steps:<\/p>\n<ol>\n<li>Import the libraries (Image, ImageDraw and ImageFont from PIL library)<\/li>\n<li>Open the image to work on<\/li>\n<li>Create draw object (this is a handle that takes the opened image as argument)<\/li>\n<li>Create text and font object (we&#8217;re selecting arial as font and 82 as size) Font size here can be formulated proportional to image size. Especially in a batch processing scenario font can appear too small or too big as image resolution fluctuates if it&#8217;s given as a constant integer.<\/li>\n<li>We&#8217;re simply calculating x and y coordinates for text. Horizontally it&#8217;s in the middle and text size is also encountered. Vertically it&#8217;s 300 pixels up from the end of the image.<\/li>\n<li>&nbsp;Simply saving the new image. You can use the same path and name which will overwrite the original image. Or, if you want to keep the originals as is, you can give it a new name as in the example. At this step, you can also convert the image type if it&#8217;s more convenient for you. For instance, we&#8217;re starting with a .jpg image and converting it to .png at the saving step.<\/li>\n<\/ol>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1bfa70b elementor-widget elementor-widget-html\" data-id=\"1bfa70b\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<pre><code>from PIL import Image, ImageDraw, ImageFont\r\n\r\n#Opening Image\r\nimg = Image.open(r'Desktop\/cake.jpg') \r\n\r\n#Creating draw object\r\ndraw = ImageDraw.Draw(img) \r\n\r\n#Creating text and font object\r\ntext = \"HolyPython.com\"\r\nfont = ImageFont.truetype('arial.ttf', 82)\r\n\r\n#Positioning Text\r\ntextwidth, textheight = draw.textsize(text, font)\r\nwidth, height = img.size \r\nx=width\/2-textwidth\/2\r\ny=height-textheight-300\r\n\r\n#Applying text on image via draw object\r\ndraw.text((x, y), text, font=font) \r\n\r\n#Saving the new image\r\nimg.save(r'Desktop\/cake_watermarked.png')\r\n<\/code><\/pre>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-77a2b25 elementor-widget elementor-widget-image\" data-id=\"77a2b25\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"636\" height=\"492\" src=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno-Custom2.png\" class=\"attachment-full size-full wp-image-15329\" alt=\"\" srcset=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno-Custom2.png 636w, https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno-Custom2-300x232.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fdb2e5b elementor-widget elementor-widget-text-editor\" data-id=\"fdb2e5b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family );\">Great, we&#8217;ve made our mark on the image. Now, let&#8217;s get more sophisticated.<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c6453bd elementor-widget elementor-widget-text-editor\" data-id=\"c6453bd\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Although nothing too complex, adding transparency to image slightly complicates the process. But, don&#8217;t worry, once you understand it or implement it, it&#8217;s actually pretty reasonable.<\/p>\n<p>We will achieve transparency through alpha channel in an RGBA (stands for red-green-blue-alpha) type image. Each of these R-G-B-A channels take an integer value between 0 and 255. So if Alpha equals 25 it will approximately equal to 10% transparency.<\/p>\n<p>Check out this example (0, 0, 0, 25). This image has all RGB channels set to 0, which means black color and then Alpha Channel is equal to 25 which means 10% transparent.<\/p>\n<p>On the other hand you&#8217;ll most likely prefer a light color like white for your watermark, so we will use an RGBA combination similar to: <b>(255, 255, 255, 25)<\/b><\/p><p><b>You can check out this <a href=\"https:\/\/holypython.com\/digital-image-basics-with-pil-library-in-python\/\">digital image basics tutorial in Python<\/a> if you&#8217;d like to polish your knowledge about digital images from coding perspective.<\/b><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-fa3f8e4 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"fa3f8e4\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-27ccb46\" data-id=\"27ccb46\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-a167174 elementor-widget__width-auto elementor-widget elementor-widget-heading\" data-id=\"a167174\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Creating Transparent Text (<i>Watermark<\/i>)<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-502a95a elementor-widget elementor-widget-text-editor\" data-id=\"502a95a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Now, let&#8217;s create a transparent watermark as it&#8217;s usually meant to be.<\/p><p>It may appear complicated, it&#8217;s not.<\/p><p>The only difference here are:<\/p><p>We will convert the image we&#8217;re opening to RGBA so it includes an alpha channel which we will work with.<\/p><p>We will create a new transparent image. Semi-transparent text will be applied to this image. You can think of it as a text layer to speak with Photoshop or Gimp lingo. This will be achieved using <strong>.new()<\/strong> method of Image module.<\/p><p>While saving, we will combine both images (original image and transparent text layer). This will be achieved using <code>.alpha_composite(img1, img2)<\/code> method of Image module.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cc43910 elementor-widget elementor-widget-html\" data-id=\"cc43910\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<pre><code>#Importing Libraries\r\nimport random\r\nfrom PIL import Image, ImageDraw, ImageFont\r\n\r\n#Opening Image & Creating New Text Layer\r\nimg = Image.open(r'Desktop\/cake.jpg').convert(\"RGBA\")\r\ntxt = Image.new('RGBA', img.size, (255,255,255,0))\r\n\r\n#Creating Text\r\ntext = \"HolyPython.com\"\r\nfont = ImageFont.truetype(\"arial.ttf\", 82)\r\n\r\n#Creating Draw Object\r\nd = ImageDraw.Draw(txt)\r\n\r\n#Positioning Text\r\nwidth, height = img.size \r\ntextwidth, textheight = d.textsize(text, font)\r\nx=width\/2-textwidth\/2\r\ny=height-textheight-300\r\n\r\n#Applying Text\r\nd.text((x,y), text, fill=(255,255,255, 125), font=font)\r\n\r\n#Combining Original Image with Text and Saving\r\nwatermarked = Image.alpha_composite(img, txt)\r\nwatermarked.save(r'cake_watermarked50.png')<\/code><\/pre>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fe8720c elementor-widget elementor-widget-image\" data-id=\"fe8720c\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t<figure class=\"wp-caption\">\n\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"636\" height=\"492\" src=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno2-50-Custom2.png\" class=\"attachment-full size-full wp-image-15330\" alt=\"\" srcset=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno2-50-Custom2.png 636w, https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno2-50-Custom2-300x232.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/>\t\t\t\t\t\t\t\t\t\t\t<figcaption class=\"widget-image-caption wp-caption-text\">PIL watermark demonstration with 50% (alpha channel =125)<\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2d9a636 elementor-widget elementor-widget-text-editor\" data-id=\"2d9a636\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\tHere is a step-by-step breakdown:\n<ol>\n \t<li>Importing Libraries: This step is self-explanatory we&#8217;re importing Image, ImageDraw and ImageFont from PIL library.<\/li>\n \t<li>Opening Image &amp; Creating New Text Layer:\n<ul>\n \t<li>Here we&#8217;re opening the original image but also immediately converting it to <b>RGBA <\/b>using <code>.convert<\/code> method.<\/li>\n \t<li><b>Also, we&#8217;re creating a new text layer, which is also RGBA, with full transparency (alpha channel is 0).<\/b><\/li>\n<\/ul>\n<\/li>\n \t<li>Creating Text: Text is defined as before and font object is also created.<\/li>\n \t<li>Creating Draw Object: Another standard procedure, draw object is created.<\/li>\n \t<li>Positioning Text: Image position is calculated similar to previous operation, horizontally centered and 300 pixels from below.<\/li>\n \t<li><b>Applying Text: At this step, we are using fill argument with RGBA parameters in a tuple. (255,255,255,125) will fill the text with approximately 50% transparency and white color, font argument is assigned to font object we created.<\/b><\/li>\n \t<li>Combining Original Image with Text and Saving: Finally both images are combined using <code>.alpha_composite()<\/code> method. Then the new image object is saved with file name cake_watermarked50<\/li>\n<\/ol>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4e5611b elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4e5611b\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-083bb91\" data-id=\"083bb91\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-990f4b7 elementor-widget__width-auto elementor-widget elementor-widget-heading\" data-id=\"990f4b7\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Creating Multiple Watermarks (<i>using Python For Loop<\/i>)<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9e82b2b elementor-widget elementor-widget-text-editor\" data-id=\"9e82b2b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>What about multiple small watermarks? Those can be handy too. You might prefer scattered watermarks on your images compared to one big watermark in the middle.<\/p><p>In this case powers of coding start to shine. You can simply place relevant watermarking steps inside a loop and loop it as many times as you&#8217;d like.<\/p><p><b>Check out this example: I lowered font size to 27, lowered transparency to approximately 30% and created a loop using range function. x and y positions are randomly chosen with the help of random library. Inside the same loop same text is applied to draw object which creates in multiple watermarks in multiple random positions throughout the image.<\/b><\/p><p>Everything else is similar to the previous example. Here is the Python code:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-aa487bd elementor-widget elementor-widget-html\" data-id=\"aa487bd\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<pre><code>import random\r\nfrom PIL import Image, ImageDraw, ImageFont\r\n\r\n#Opening Image & Creating New Text Layer\r\nimg = Image.open(r'Desktop\/cake.jpg').convert(\"RGBA\")\r\ntxt = Image.new('RGBA', img.size, (255,255,255,0))\r\n\r\n#Creating Text\r\ntext = \"HolyPython.com\"\r\nfont = ImageFont.truetype(\"arial.ttf\", 27)\r\n\r\n#Creating Draw Object\r\ndraw = ImageDraw.Draw(txt)\r\n\r\n#Positioning of Text\r\nwidth, height = img.size \r\n# textwidth, textheight = d.textsize(text, font)\r\n# x=width\/2-textwidth\/2\r\n# y=height-textheight-300\r\n\r\n# Loop for Multiple Watermarks\r\ny=200\r\nfor i in range(7):\r\n    x=random.randint(0, width-300)\r\n    y+=random.randrange(0,int(height\/8), 19)+random.randint(0,100)\r\n    draw.text((x,y), text, fill=(255,255,255, 75), font=font)\r\n\r\n#Combining both layers and saving new image\r\nwatermarked = Image.alpha_composite(img, txt)\r\nwatermarked.save(r'Desktop\/cake_M_30.png')<\/code><\/pre>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2d37bde elementor-widget elementor-widget-text-editor\" data-id=\"2d37bde\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Regarding positions in the loop, I&#8217;ve just assigned a random integer to x coordinate and excluded 300 pixels from the end so that text doesn&#8217;t start too late horizontally and get cut off.<\/p><p>Regarding y coordinate, I tried a slightly more sophisticated approach in an attempt to avoid overlapping watermarks. y starts at 200th pixel and incrementally increases In a range between 0 and &#8220;height of the image divided by amount of watermarks&#8221;. Then a random integer between 0 and 100 is added to create more randomness.<\/p><p>You can play with it to see a more optimal solution. I&#8217;m sure someone can come up with a smarter distribution model regarding x and y coordinates in multiple watermark situations.<\/p><p>You can check out <a href=\"https:\/\/holypython.com\/watermarkd\" target=\"_blank\" rel=\"noopener\">Watermarkd Python library<\/a> here and its <a href=\"https:\/\/github.com\/holypython\/watermarkd\">source code here<\/a>. Watermarkd has an improved watermark positioning algorithm with batch function (for watermarking multiple files at once) and GUI components.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-373c25a elementor-widget elementor-widget-image\" data-id=\"373c25a\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"492\" src=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno3-Custom2.png\" class=\"attachment-full size-full wp-image-15331\" alt=\"\" srcset=\"https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno3-Custom2.png 636w, https:\/\/holypython.com\/wp-content\/uploads\/2020\/09\/bruno3-Custom2-300x232.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4f051c8 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4f051c8\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-96cbc2f\" data-id=\"96cbc2f\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-b88caf6 elementor-widget__width-auto elementor-widget elementor-widget-heading\" data-id=\"b88caf6\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Conclusion & New Ideas (<i>Batch Watermarking, GUI Watermark Application<\/i>)<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1bed31a elementor-widget elementor-widget-text-editor\" data-id=\"1bed31a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This is the end. Hopefully this tutorial can be useful for many people. Besides being a totally practical topic, I find it also helpful to practice Python and gain new coding skills.<\/p><p>Additionally you can combine this knowledge with some other Python tutorials we have such as:<\/p><ul><li><b><a href=\"https:\/\/holypython.com\/how-to-batch-resize-multiple-images-in-python-via-pil-library\/\" target=\"_blank\" rel=\"noopener\">Batch editing images in Python<\/a>: <\/b>can be used to create a Python program that applies watermark to multiple images in a batch.<\/li><li><b><a href=\"https:\/\/holypython.com\/creating-gui-programs-w-python-fast-easy-pysimplegui\/\">Creating GUI applications w\/ Python<\/a>: <\/b>can be used to create a GUI application that helps people select image folder or image file and apply a predefined watermark text.<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4538883 elementor-widget elementor-widget-text-editor\" data-id=\"4538883\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>After this tutorial, you definitely deserved some cake! or another treat that your prefer!<\/p>\n<p>Thanks for checking out this Python tutorial.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>What&#8217;s Watermarking? Watermark is a semi-transparent text or logo that sort of blends with an image in the background to make the image more difficult to copy or share without appropriate references. Watermark is an old idea (usually applied to money, stamp or institutional papers) that&#8217;s been adapted nicely to the digital image practices. Holy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[35,62,52,61],"tags":[],"class_list":["post-13959","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced","category-image-processing","category-tutorials","category-scripting"],"acf":[],"_links":{"self":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts\/13959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/comments?post=13959"}],"version-history":[{"count":0,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts\/13959\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/media\/15333"}],"wp:attachment":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/media?parent=13959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/categories?post=13959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/tags?post=13959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}