Python给图片加水印代码

import time
import os
try:
    from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
    import Image, ImageDraw, ImageFont, ImageEnhance



fontpath = "/home/hey/Desktop/hey.ttf"
waterfontpath = "/home/hey/Desktop/WeiRuanYaHei-1.ttf"
out_file = "/home/hey/Desktop/img/"


# def logo_watermark(img, logo_path):
#     '''''
#     添加一个图片水印,原理就是合并图层,用png比较好
#     '''
#     baseim = img
#     logoim = Image.open(logo_path)
#     bw, bh = baseim.size
#     lw, lh = logoim.size
#     baseim.paste(logoim, (bw - lw, bh - lh))
#     baseim.save('test3.jpg', 'JPEG')
#     print(u'logo水印组合成功')



def text_watermark(img, text, out_file, angle=23, opacity=0.50):
    ''''' 
    添加一个文字水印,做成透明水印的模样,应该是png图层合并 
    http://www.pythoncentral.io/watermark-images-python-2x/ 
    这里会产生著名的 ImportError("The _imagingft C module is not installed") 错误 
    Pillow通过安装来解决 pip install Pillow 
    '''
    watermark = Image.new('RGBA', img.size, (0, 0, 0,0))  # 我这里有一层白色的膜,去掉(255,255,255) 这个参数就好了
    FONT = waterfontpath
    size = 70
    n_font = ImageFont.truetype(FONT, size)  # 得到字体
    n_width, n_height = n_font.getsize(text)
    n_font = ImageFont.truetype(FONT, size=size)
    # watermark = watermark.resize((text_width,text_height), Image.ANTIALIAS)
    draw = ImageDraw.Draw(watermark, 'RGBA')  # 在水印层加画笔
    ###左3
    draw.text((watermark.size[0] - 1100, watermark.size[1] - 1850),text, font=n_font, fill="#ccc")
    draw.text((watermark.size[0] - 1250, watermark.size[1] - 1400), text, font=n_font, fill="#ccc")
    draw.text((watermark.size[0] - 1400, watermark.size[1] - 950), text, font=n_font, fill="#ccc")
    ###右3
    draw.text((watermark.size[0] - 650, watermark.size[1] - 1600), text, font=n_font, fill="#ccc")
    draw.text((watermark.size[0] - 800, watermark.size[1] - 1150), text, font=n_font, fill="#ccc")
    draw.text((watermark.size[0] - 950, watermark.size[1] - 700), text, font=n_font, fill="#ccc")
    watermark = watermark.rotate(angle, Image.BICUBIC)
    alpha = watermark.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    watermark.putalpha(alpha)
    out_file = out_file + time.strftime("%Y%m%d%H%M%S") + ".jpg"
    Image.composite(watermark, img, watermark).save(out_file, 'JPEG')
    print("文字水印成功")
    return out_file



def image_to_text():
    out_file_path = text_watermark(im, '三湘银行抵押快贷', out_file)
    targetimg = Image.open(out_file_path)
    # 将img添加到画板
    imgdraw = ImageDraw.Draw(targetimg)
    # 设置需要绘制的字体 参数:字体名,字体大小
    imgfont = ImageFont.truetype(fontpath, size=22)
    # 字体颜色
    fillcolor = "black"
    # 获取img的宽和高
    # imgw,imgh = img.size
    # 开始将文字内容绘制到img的画板上 参数:坐标,绘制内容,填充颜色,字体
    imgdraw.text((20, 20),"测试", fill=fillcolor, font=imgfont)
    imgdraw.text((0, 0), "测试", fill=fillcolor, font=imgfont)
    # # 设置img的保存路径和文件名
    # imgsavetarget = savepath + time.strftime("%Y%m%d%H%M%S") + ".png"
    # 开始保存
    targetimg.save(out_file_path, "png")

    # 返回保存结果
    return out_file_path







if __name__ == "__main__":

    # im = Image.open('/home/hey/Desktop/test.jpg')  # image 对象
    # logo_watermark(im, '/home/hey/Desktop/logo.jpg')
    sourceimg1 = "/home/hey/Desktop/sx_1.jpg"
    im = Image.open(sourceimg1)  # image 对象
    result = image_to_text()
    sourceimg2 = "/home/hey/Desktop/sx_2.jpg"
    im = Image.open(sourceimg2)  # image 对象
    result = image_to_text()
    sourceimg3 = "/home/hey/Desktop/sx_3.jpg"
    im = Image.open(sourceimg3)  # image 对象
    result = image_to_text()
    sourceimg4 = "/home/hey/Desktop/sx_4.jpg"
    im = Image.open(sourceimg4)  # image 对象
    result = image_to_text()
    print(result)

你可能感兴趣的:(Python给图片加水印代码)