Created
March 18, 2011 17:00
-
-
Save snay2/876425 to your computer and use it in GitHub Desktop.
How to apply a semi-transparent watermark to an image using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw | |
def main(): | |
# Open the original image | |
main = Image.open("12voltm.jpg") | |
# Create a new image for the watermark with an alpha layer (RGBA) | |
# the same size as the original image | |
watermark = Image.new("RGBA", main.size) | |
# Get an ImageDraw object so we can draw on the image | |
waterdraw = ImageDraw.ImageDraw(watermark, "RGBA") | |
# Place the text at (10, 10) in the upper left corner. Text will be white. | |
waterdraw.text((10, 10), "The Image Project") | |
# Get the watermark image as grayscale and fade the image | |
# See <http://www.pythonware.com/library/pil/handbook/image.htm#Image.point> | |
# for information on the point() function | |
# Note that the second parameter we give to the min function determines | |
# how faded the image will be. That number is in the range [0, 256], | |
# where 0 is black and 256 is white. A good value for fading our white | |
# text is in the range [100, 200]. | |
watermask = watermark.convert("L").point(lambda x: min(x, 100)) | |
# Apply this mask to the watermark image, using the alpha filter to | |
# make it transparent | |
watermark.putalpha(watermask) | |
# Paste the watermark (with alpha layer) onto the original image and save it | |
main.paste(watermark, None, watermark) | |
main.save("12volt-watermarked.jpg", "JPEG") | |
if __name__ == '__main__': | |
main() |
Doesn't work for me.
i want to use it not only for a single image ... a bunch of image fetching from database ,how can i use it?I am using django.
Works like a charm. To be able to use this from the command line I suggest to add #!/usr/bin/env python
.
My recipe for those who want a watermark to be a vector image or want to use a blend mode as well as semi-transparency.
Thank you! This works like a charm.
@ehsansh84 here is the improved one (quick draft of mine) that puts in the bottom + uses custom font:
#!/usr/bin/env python
import os
from PIL import Image, ImageDraw, ImageFont
def work(filename):
# Open the original image
main = Image.open("without_watermark/"+filename)
# Create a new image for the watermark with an alpha layer (RGBA)
# the same size as the original image
watermark = Image.new("RGBA", main.size)
# Get an ImageDraw object so we can draw on the image
waterdraw = ImageDraw.ImageDraw(watermark, "RGBA")
# Place the text at (10, 10) in the upper left corner. Text will be white.
font_path = "/System/Library/Fonts/Palatino.ttc"
font = ImageFont.truetype(font_path, 30)
im = Image.open("without_watermark/"+filename)
width, height = im.size
print height
print width
waterdraw.text((10, height-35), "www.reactjs.co", fill=(128,128,128,128), font=font)
# Get the watermark image as grayscale and fade the image
# See <http://www.pythonware.com/library/pil/handbook/image.htm#Image.point>
# for information on the point() function
# Note that the second parameter we give to the min function determines
# how faded the image will be. That number is in the range [0, 256],
# where 0 is black and 256 is white. A good value for fading our white
# text is in the range [100, 200].
watermask = watermark.convert("L").point(lambda x: min(x, 100))
# Apply this mask to the watermark image, using the alpha filter to
# make it transparent
watermark.putalpha(watermask)
# Paste the watermark (with alpha layer) onto the original image and save it
main.paste(watermark, None, watermark)
main.save("watermark/"+filename, "PNG")
if __name__ == '__main__':
li = os.listdir('without_watermark')
for f in li:
if("gif" in f): continue
print f
work(f)
Thanks. Great to have a bit of working code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to set watermark in position of 10 pixel from bottom?