Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparency with Python Image Library

I'm trying to place a PNG watermark with partial transparency on top of a Facebook profile pic (jpg) using the Python Image Library. The part that should be transparent simply comes off as white. Here's my code:

con = urllib2.urlopen('facebook_link_to_profile_pic')
im = Image.open(cStringIO.StringIO(con.read()))

overlayCon = urllib2.urlopen('link_to_overlay')
overlay = Image.open(cStringIO.StringIO(overlayCon.read()))

im.paste(overlay, (0, 0))

im.save('name', 'jpeg', quality=100)

I've tried a few different ways, but haven't gotten anything to work. Any help is appreciated.

like image 240
dchang Avatar asked Sep 29 '11 05:09

dchang


People also ask

How do I save an image with a transparent background in Python?

Use the matplotlib savefig function with the keyword argument transparent=True to save the image as a png file. Of course, that plot doesn't demonstrate the transparency. Here's a screenshot of the PNG file displayed using the ImageMagick display command.

How do you make a pixel transparent in Python?

putpixel((x, y), (255, 255, 255, 0)) can make a pixel transparent.


1 Answers

The 3rd option to paste is a mask (see the docs). It accepts an RGBA image, so the simplest solution is to use your overlay image again: im.paste(overlay, (0, 0), overlay).

like image 64
Eryk Sun Avatar answered Sep 22 '22 23:09

Eryk Sun