Python PIL 图片添加水印

工作需要每天发彩信日报,日报里面添加一张图片,包括彩信编号和今天的日期,于是学习写了下面的小程序,站在这里留念一下

from PIL import Image, ImageDraw, ImageFont
import time

def watermark(img, img_ed, date, number, fnt):
    """Adds a watermark to an image."""
    #draw the watermark on the empty pic at a specified position
    img_draw = ImageDraw.Draw(img_ed)
    img_draw.text((134, 179), date, font=fnt, fill=(255,255,255,255))
    img_draw.text((170, 10), number, font=fnt, fill=(255,255,255,255))

    out = Image.alpha_composite(img, img_ed)
    out.save("1.gif")
    print "saved as gif"

    
def daily():
    img = Image.open("D:\SCMDR.gif").convert('RGBA')
    date = time.strftime("%Y/%m/%d")
    number = raw_input("Input the number please:")

    #make a blank image for the text, initialized to transparent text color
    img_ed = Image.new('RGBA', img.size, (255,255,255,0))
    h,w = img.size

    #draw txt on the pic    
    fnt = ImageFont.truetype('E:\myexp\运维日报\MSYH.ttc',12)

    watermark(img, img_ed, date, number, fnt)


if __name__ == '__main__':
    daily()


主要的过程是:

1、获取这个图片

2、构造一个一样大小的白色图片

3、把相关文字draw到那个白色图片上

4、把两张图片合并起来即可



但是很奇怪这个python程序无法双击执行,不知道是为什么呢

你可能感兴趣的:(python)