# #引入库 from PIL import Image,ImageDraw,ImageFont import os inf= 'tpin'# 原始文件夹路径 outf= 'tpout'# 保存的调整完毕后文件夹路径 n=int(input("请输入你需要的width:")) m=int(input("请输入你需要的height:")) # #批量更改图片大小 for file in os.listdir(inf): # 每一次循环完成后返回指定的文件夹 img = Image.open(os.path.join(inf, file))#拼接相对路径 # img_resized = img.resize((900, 800)) # 转换的尺寸 imgre = img.resize((n,m)) # 转换的尺寸 imgre.save(os.path.join(outf, file))# 保存到新文件 #加水印 # 添加文字水印只有文字水印会用到ImageDraw,ImageFont for newfile in os.listdir(outf): img1 = Image.open(os.path.join(outf, newfile)) width, height = img1.size draw = ImageDraw.Draw(img1) font = ImageFont.truetype("FZSTK.TTF", 80) draw.text((width - 160, height - 80), text="水印", font=font, fill="pink") img1.save(os.path.join(outf, newfile)) # 添加缩略图水印 # for newfile in os.listdir(outf): # 遍历循环新文件夹 img1 = Image.open(os.path.join(outf, newfile))#打开 # width, height = img1.size position = (width - 800, height - 700) logo= img1.resize((int(img1.width / 10), int(img1.height / 10))) # 获取小10倍缩略图 #透明度混合 box = (0, 0, int(img1.width / 10), int(img1.height / 10))#得到原图同样位置,同样大小的图片 img2= img1.crop(box) # 裁剪 mix = Image.blend(img2,logo, 0.5)#混合两张小的图片 ##无透明度合并 # img1.paste(logo,position)#粘贴 # img1.paste(logo, position) # # img1.paste(mix,position)#透明度粘贴 img1.save(os.path.join(outf, newfile)) # # #添加图片水印 for newfile in os.listdir(outf): img1 = Image.open(os.path.join(outf, newfile)) width, height = img1.size position = (width - 80, height - 700) # 选择粘贴位置 logo = Image.open('sy.jpg')#打开水印图片 lg = logo.resize((80, 40))#重置大小 img1.paste(lg, position)#图像合并 img1.save(os.path.join(outf, newfile))#保存