首页 > 其他 > 详细

图片增加水印

时间:2019-09-25 17:20:34      阅读:96      评论:0      收藏:0      [点我收藏+]

From PIL import Image, ImageDraw, ImageFont
import sys

def watermark_with(file_obj, text, color, fontfamily=None):
    image = Image.open(file_obj).convert(‘RGBA‘)
    draw = ImageDraw.Draw(image)
    width, height = image.size
    margin = 10
    if fontfamily:
        font = ImageFont.truetype(fontfamily, int(height / 20))
    else:
        font = None
    textWidth, textHeight = draw.textsize(text, font)
    x = (width - textWidth - margin) / 2  # 计算横轴位置
    y = height - textHeight - margin  # 计算纵轴位置
    draw.text((x, y), text, color, font)
    return image
if __name__ == ‘__main__‘:

    org_file = sys.argv[1]

    with open(org_file, ‘rb‘) as f:

        image_with = watermark_with(f, ‘水印‘, ‘red‘)

    with open(‘new_image_water.png‘, ‘wb‘) as f:

        image_with.save(f)

更多技术资讯可关注:gzitcast

图片增加水印

原文:https://www.cnblogs.com/heimaguangzhou/p/11585489.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!