关于循环pillow批量操作

# #引入库
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))#保存

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值