69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
from PIL import Image
|
|
from PIL import ImageDraw
|
|
from PIL import ImageFont
|
|
|
|
|
|
def watermark_text(input_image_path: str,
|
|
output_image_path: str,
|
|
font_path: str,
|
|
text: str,
|
|
pos: tuple[float, float] = (50, 50),
|
|
size: int = 40,
|
|
show: bool = False):
|
|
photo = Image.open(input_image_path)
|
|
|
|
# make the image editable
|
|
drawing = ImageDraw.Draw(photo)
|
|
|
|
black = (3, 8, 12)
|
|
white = (254, 254, 254)
|
|
font = ImageFont.truetype(font_path, size, encoding='unic')
|
|
drawing.text(pos, text=text, fill=white, font=font, anchor="la")
|
|
if show:
|
|
photo.show()
|
|
photo.save(output_image_path)
|
|
|
|
|
|
def watermark_text_bottom_right(input_image_path: str,
|
|
output_image_path: str,
|
|
font_path: str,
|
|
text: str,
|
|
pos: tuple[float, float] = (300, 200),
|
|
size: int = 40):
|
|
photo = Image.open(input_image_path)
|
|
w, h = photo.size
|
|
watermark_text(
|
|
pos=(w-pos[0], h-pos[1]),
|
|
input_image_path=input_image_path,
|
|
output_image_path=output_image_path,
|
|
font_path=font_path,
|
|
text=text,
|
|
size=size)
|
|
|
|
|
|
def watermark_photo(input_image_path,
|
|
output_image_path,
|
|
watermark_image_path,
|
|
pos):
|
|
base_image = Image.open(input_image_path)
|
|
watermark = Image.open(watermark_image_path)
|
|
|
|
# add watermark to your image
|
|
base_image.paste(watermark, pos)
|
|
base_image.show()
|
|
base_image.save(output_image_path)
|
|
|
|
|
|
def watermark_with_transparency(input_image_path,
|
|
output_image_path,
|
|
watermark_image_path,
|
|
pos):
|
|
base_image = Image.open(input_image_path)
|
|
watermark = Image.open(watermark_image_path)
|
|
width, height = base_image.size
|
|
|
|
transparent = Image.new('RGBA', (width, height), (0,0,0,0))
|
|
transparent.paste(base_image, (0,0))
|
|
transparent.paste(watermark, pos, mask=watermark)
|
|
transparent.show()
|
|
transparent.save(output_image_path) |