Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Watermarks using Pillow
#1
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?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020