469,086 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

Creating watermark with transparency on jpeg using PIL?

I have been trying to add a watermark to a jpeg using PIL, but the
watermark has a black box around it. I looked at

http://aspn.activestate.com/ASPN/Coo.../Recipe/362879

and

http://mail.python.org/pipermail/pyt...ay/003369.html

but I think these only refer to gif or png. I know jpegs really do not
support transparency, but is there some way to take a watermark in a
non jpeg format and add it to a jpeg without that box appearing around
it?

Best Regards

Ty

Aug 18 '05 #1
2 4529
On Thursday 18 August 2005 10:07 am, tv****@gmail.com wrote:
I have been trying to add a watermark to a jpeg using PIL, but the
watermark has a black box around it. I looked at [...] but I think these only refer to gif or png. I know jpegs really do not
support transparency, but is there some way to take a watermark in a
non jpeg format and add it to a jpeg without that box appearing around
it?


Jpeg is a lossy compression format. You shouldn't do image processing
in such a format at all. Convert the data to PNG, do your processing,
and convert back to JPG for delivery. PIL can do that.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 18 '05 #2
You need to pass a mask in when you paste in the watermark.

see the documentation for the paste method at
http://effbot.org/imagingbook/image.htm
for more information

This should at least get you started...
import Image
import ImageDraw
import ImageFont
import ImageEnhance
im=Image.new('RGB',(300,300),(0,0,0))
font=ImageFont.truetype('verdana.ttf',12)
wm=Image.new('RGBA',(100,50),(255,255,255))
im=Image.new('RGB',(300,300),(255,255,255))
draw=ImageDraw.Draw(wm)
draw.text((0,0),'Watermark',(0,0,0),font)
wm.show()
en=ImageEnhance.Brightness(wm)
mask=en.enhance(0.5)
im.paste(wm,(25,25),mask)
im.show()


Here, the alpha channel of 'mask' is used as the mask.

max

Aug 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Derek | last post: by
3 posts views Thread by Mr. x | last post: by
6 posts views Thread by tommaso.gastaldi | last post: by
4 posts views Thread by tshad | last post: by
reply views Thread by ameshkin | last post: by
9 posts views Thread by Chuck Anderson | last post: by
5 posts views Thread by Tem | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.