Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 884
DocWiki Avatar asked Jul 06 '11 17:07

DocWiki


2 Answers

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.

like image 178
kindall Avatar answered Sep 18 '22 03:09

kindall


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()
like image 40
gzerone Avatar answered Sep 18 '22 03:09

gzerone