471,873 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

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 4762
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
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.