Last active
October 23, 2023 02:13
-
-
Save Chhunneng/0d17ec214a6b5b206a51ddc29357e84c to your computer and use it in GitHub Desktop.
This file contains 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 | |
def watermark(img_path, watermark_path, output_path): | |
# watermark_path is the image of watermark. Not text | |
with Image.open(img_path) as im: | |
width, height = int(im.size[0]), int(im.size[1]) | |
x_interval = int(width / 10) | |
y_interval = int(height / 10) | |
with Image.open(watermark_path) as watermark: | |
# Load watermark and resize to fit | |
watermark = watermark.resize((x_interval, y_interval)) | |
# Composite into image | |
for i in range(0, width, x_interval): | |
for j in range(0, height, y_interval): | |
im.paste(watermark, (i, j), watermark) | |
im.save(output_path) | |
return output_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment