Python Forum
Watermarks using Pillow - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Watermarks using Pillow (/thread-11583.html)



Watermarks using Pillow - Tomallama - Jul-17-2018

Hey guys, I've been stuck on trying to place watermarks on a bunch of different sized images using Pillow. I can't get them to line up correctly and to the correct size. Here is a basic script of what I've tried so far.

from PIL import Image

SQUARE_FIT_SIZE = 300

im = Image.open('final_crop.png')
print(im.size)
logo = Image.open('catlogo.png')
(logo_w, logo_h) = logo.size
(im_w, im_h) = im.size
if im_w > SQUARE_FIT_SIZE or im_h > SQUARE_FIT_SIZE:
    if im_w < im_h:
        im_h = int((300 * im_w) / im_h)
        im_w = SQUARE_FIT_SIZE
    else:
        im_w = int((300 * im_h) / im_w)
        im_h = SQUARE_FIT_SIZE


im.paste(logo, ((im_w - logo_w), (im_h - logo_h), im_w, im_h), logo)
im.show()
I'm trying to rework the problem from Automate The Boring Stuff because his code also does not work for me, so I'm assuming it's outdated. I figured I'd try to do it from scratch. The code runs, but the logo is pasted in the completely wrong place and wrong size. I think I have my coordinates correct during the im.paste since that's how I'd solve it on paper, but I can't figure out why the logo won't automatically resize. I've tried different things using logo.resize, but since this is supposed to work for multiple size images I can't really figure out how to resize it correctly. I've also tried doing just two parameters in im.paste, with just using (im_w - logo_w), (im_h - logo_h) and leaving the last two coordinates off, but it doesn't help. I know the paste() method also has a box argument and mask argument, but I don't understand how they work and I'm not sure if I need to use them.

Anyone know a way to perform this task?