码迷,mamicode.com
首页 > 编程语言 > 详细

python添加水印

时间:2019-09-08 18:28:00      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:type   div   水印   fill   composite   __name__   range   for   pre   

# coding:utf-8

from PIL import Image, ImageDraw, ImageFont


def add_text_to_image(image, text):
    font = ImageFont.truetype(C:\Windows\Fonts\STXINGKA.TTF, 36)

    # 添加背景
    new_img = Image.new(RGBA, (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
    new_img.paste(image, image.size)

    # 添加水印
    font_len = len(text)
    rgba_image = new_img.convert(RGBA)
    text_overlay = Image.new(RGBA, rgba_image.size, (255, 255, 255, 0))
    image_draw = ImageDraw.Draw(text_overlay)

    for i in range(0, rgba_image.size[0], font_len*40+100):
        for j in range(0, rgba_image.size[1], 200):
            image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
    text_overlay = text_overlay.rotate(-45)
    image_with_text = Image.alpha_composite(rgba_image, text_overlay)

    # 裁切图片
    image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
    return image_with_text


if __name__ == __main__:
    img = Image.open("test.jpg")
    im_after = add_text_to_image(img, u石家庄)
    im_after.save(u水印.png)

 

python添加水印

标签:type   div   水印   fill   composite   __name__   range   for   pre   

原文地址:https://www.cnblogs.com/zyt-bg/p/11487513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!