Python PIL: How to FIll a Image with a copyright logo like this?
Asked Answered
N

2

6

I need to protect my photos by filling them with copyright logos. My OS is Ubuntu 10.10 and Python 2.6. I intend to use PIL.

Supposed I have a copyright logo like this (You can do this in Photoshop easily):

water mark logo

and a picture like this:

Original Picture

I want to use PIL to get copyrighted pictures like the following (Fill the original pic with pattern):

Copyrighted Picture

and Final Result by altering the opacity of logos:

Final Result

Is there any function in PIL that can do this? Any hint?

Thanks a lot!

Nope answered 6/7, 2011 at 17:34 Comment(2)
Here's a recipe that might be of some help, it only adds a single watermark though so you would have to modify it code.activestate.com/recipes/362879-watermark-with-pilUnprofessional
These links might help: code.activestate.com/recipes/362879-watermark-with-pil .. hasanatkazmi.blogspot.com/2009/06/…Ardeha
F
6

PIL is certainly capable of this. First you'll want to create an image that contains the repeated text. It should be, oh, maybe twice the size of the image you want to watermark (since you'll need to rotate it and then crop it). You can use Image.new() to create such an image, then ImageDraw.Draw.text() in a loop to repeatedly plaster your text onto it, and the image's rotate() method to rotate it 15 degrees or so. Then crop it to the size of the original image using the crop() method of the image.

To combine it first you'll want to use ImageChops.multiply() to superimpose the watermark onto a copy of the original image (which will have it at 100% opacity) then ImageChops.blend() to blend the watermarked copy with the original image at the desired opacity.

That should give you enough information to get going -- if you run into a roadblock, post code showing what you've got so far, and ask a specific question about what you're having difficulty with.

Floodgate answered 6/7, 2011 at 17:48 Comment(1)
is there a way to temporarily add watermark while displaying instead of saving a copyCretan
M
2

Just a sample:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from PIL import Image

def fill_watermark():
    im = Image.open('a.jpg').convert('RGBA')
    bg = Image.new(mode='RGBA', size=im.size, color=(255, 255, 255, 0))
    logo = Image.open('logo.png')  # .rotate(45) if you want, bg transparent
    bw, bh = im.size
    iw, ih = logo.size
    for j in range(bh // ih):
        for i in range(bw // iw):
            bg.paste(logo, (i * iw, j * ih))
    im.alpha_composite(bg)
    im.show()
    
fill_watermark()
Mullion answered 17/10, 2019 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.