Creating watermark with transparency on jpeg using PIL?

Discussion in 'Python' started by tvmaly@gmail.com, Aug 18, 2005.

  1. Guest

    , Aug 18, 2005
    #1
    1. Advertising

  2. On Thursday 18 August 2005 10:07 am, 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
     
    Terry Hancock, Aug 18, 2005
    #2
    1. Advertising

  3. Max Erickson Guest

    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
     
    Max Erickson, Aug 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. News KF
    Replies:
    3
    Views:
    424
    Oliver Wong
    Sep 19, 2005
  2. Marek

    PIL and transparency in IE

    Marek, Jun 26, 2003, in forum: Python
    Replies:
    0
    Views:
    1,081
    Marek
    Jun 26, 2003
  3. jean-Baptiste Cazier

    Transparency with PIL and paste ?

    jean-Baptiste Cazier, Jul 23, 2003, in forum: Python
    Replies:
    0
    Views:
    661
    jean-Baptiste Cazier
    Jul 23, 2003
  4. Robin Becker

    PIL & TIFF transparency?

    Robin Becker, Jul 30, 2003, in forum: Python
    Replies:
    2
    Views:
    754
    Robin Becker
    Jul 31, 2003
  5. Thomas

    Watermark images using PIL

    Thomas, Nov 17, 2004, in forum: Python
    Replies:
    0
    Views:
    441
    Thomas
    Nov 17, 2004
Loading...

Share This Page