python實現水印生成器
更新時間:2022年05月07日 09:51:11 作者:東華果汁哥
這篇文章主要為大家詳細介紹了python實現水印生成器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現水印生成器,python制作圖片水印的具體代碼,供大家參考,具體內容如下
制作一個水印生成器,方便訓練水印圖片數據采集。
圖片水印生成器,可以給指定圖片文件或者目錄添加水印,水印支持自定義文本、位置、顏色、大小。
# -*- encoding=utf-8 -*- import time from PIL import Image, ImageDraw,ImageFont,ImageEnhance """圖片水印生成器,自定義文字,顏色,大小,位置""" # 橫向上方水印 def auto_make_watermark1(filepath,content,color,savefilepath): ? ? """ ? ? :param filepath: 圖片路徑 ? ? :param content: 水印文字 ? ? :param color: 水印顏色 ? ? :param savefilepath: 保存路徑 ? ? :return: ? ? """ ? ? image = Image.open(filepath).convert('RGB') ? ? draw = ImageDraw.Draw(image) ? ? font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) ?# 設置水印字體 ? ? draw.text((80, 200), content,color, font) # 設置水印位置 ? ? image.save(savefilepath) # 橫向中間水印 def auto_make_watermark2(filepath,content,color,savefilepath): ? ? """ ? ? :param filepath: 圖片路徑 ? ? :param content: 水印文字 ? ? :param color: 水印顏色 ? ? :param savefilepath: 保存路徑 ? ? :return: ? ? """ ? ? image = Image.open(filepath).convert('RGB') ? ? draw = ImageDraw.Draw(image) ? ? font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) ?# 設置水印字體 ? ? draw.text((80, 400), content,color, font) ? # 設置水印位置 ? ? image.save(savefilepath) # 橫向最下方水印 def auto_make_watermark3(filepath,content,color,savefilepath): ? ? """ ? ? :param filepath: 圖片路徑 ? ? :param content: 水印文字 ? ? :param color: 水印顏色 ? ? :param savefilepath: 保存路徑 ? ? :return: ? ? """ ? ? image = Image.open(filepath).convert('RGB') ? ? draw = ImageDraw.Draw(image) ? ? font=ImageFont.truetype("simsun.ttc", 40, encoding="unic",index=1) ?# 設置字體 ? ? draw.text((80,image.size[1]-150), content,color, font) # 設置水印位置 ? ? image.save(savefilepath) # 橫向中間傾斜水印45度 def auto_make_watermark4(filepath,content,color,savefilepath,radio): ? ? """ ? ? :param filepath: 圖片路徑 ? ? :param content: 水印文字 ? ? :param color: 水印顏色 ? ? :param savefilepath: 保存路徑 ? ? :param radio: 水印傾斜角度 ? ? :return: ? ? """ ? ? im = Image.open(filepath) ? ? watermark = Image.new('RGBA', im.size) ? ? draw = ImageDraw.Draw(watermark, 'RGBA') ? ? font = ImageFont.truetype("simsun.ttc", 40, encoding="unic", index=1) ? ? # x y 坐標 ? ? draw.text((80, 400), content, font=font, ?fill=color) ? ? # 旋轉45度 ? ? watermark = watermark.rotate(radio, Image.BICUBIC) ? ? # 透明的 ? ? alpha = watermark.split()[3] ? ? alpha = ImageEnhance.Brightness(alpha).enhance(0.7) ? ? watermark.putalpha(alpha) ? ? # 合成新的圖片 ? ? image2 = Image.composite(watermark, im, watermark) ? ? image2.save(savefilepath) if __name__ == '__main__': ? ? time1=time.time() ? ? filepath=r'F:/img_spam/python添加水印/10064003738101263000320010013284.jpg' ? ? savefilepath='F:/img_spam/python添加水印/生成/' ? ? text="誠招發(fā)單人,vx:18668124728" ? ? colors=['red','green','black'] ? ? for color in colors: ? ? ? ? auto_make_watermark1(filepath,text,color,savefilepath+'10064003738101263000320010013285'+'_'+color+'_'+'top'+'.jpg') ? ? ? ? auto_make_watermark2(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color+'_' +'mmddle'+'.jpg') ? ? ? ? auto_make_watermark3(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color +'_'+ 'down' + '.jpg') ? ? ? ? auto_make_watermark4(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color+'_' + 'angle' + '.png',45) ? ? ? ? auto_make_watermark4(filepath, text, color,savefilepath + '10064003738101263000320010013285' + '_' + color + '_' +'angle_50' + '.png', -50) ? ? time2=time.time() ? ? print('總共耗時:' + str(time2 - time1) + 's')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。