||
python pillow库完成数据批量盖章 打包成相应的exe
盖章前 改章后
代码如下
#coding = utf-8
import os
import shutil
from PIL import Image
png=[]
imagewater =Image.open("awt.gif")
for root,dirs,files in os.walk(os.getcwd()):
for file in files:
fe=os.path.join(root,file)
if fe.endswith("png"):
png.append(fe)
if not os.path.exists("png"):
os.mkdir("png")
else:
print("存在文件夹")
newpath=os.path.join(os.getcwd(),"png")
#读取文件
for i in png:
pngobj=Image.open(i)
#将图片转换为rgba值
rgbawater=imagewater.convert("RGBA")
rgbapng=pngobj.convert("RGBA")
#尺寸赋值
imagewater_x,imagewater_y=rgbawater.size
pngobj_x,pngobj_y=rgbapng.size
#缩放图片尺
scale=7
watermark_scale = max(pngobj_x / (scale * imagewater_x), pngobj_y / (scale * imagewater_y))
new_size = (int(imagewater_x * watermark_scale), int(imagewater_y * watermark_scale))
rgbawater = rgbawater.resize(new_size, resample=Image.ANTIALIAS)
#印章与图片融合
rgbapng.paste(rgbawater,(pngobj_x-imagewater_x-1550,pngobj_y-imagewater_y+1300))
out=Image.composite(rgbapng,pngobj,rgbapng)
b=i.split(".png")[0]+"new"+".png"
out.save(b)
print("*"*30)
print("%s 完成盖章扫描 "%(i))
if "new" in b:
shutil.move(b,newpath)
for root ,dirs, files in os.walk(newpath):
for file in files:
oldFnme=os.path.join(root,file)
g=file.split("new")[0]+".png"
newFnme=os.path.join(root,g)
os.rename(oldFnme,newFnme)
print("%s 完成创建"%(g))
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-1 16:18
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社