开发者

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

开发者 https://www.devze.com 2023-03-18 17:15 出处:网络
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.

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):

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

and a picture like this:

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

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

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

开发者_JS百科

and Final Result by altering the opacity of logos:

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

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

Thanks a lot!


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.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消