-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatermark.py
30 lines (26 loc) · 1.33 KB
/
watermark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from PIL import Image, ImageDraw
def main():
# Open the original image
main = Image.open("test-image-cover.jpeg")
# 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((100, 100), "My Python 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")
main()