Image 组合图片

创建日期: 2023-04-14 18:39 | 作者: 风波 | 浏览次数: 13 | 分类: Python

来源:https://blog.csdn.net/xiaoqtbai/article/details/125567436

注意:如果 .convert("RGBA"),那么保存的格式需要是 .png

little_pic = Image.open('xx.png')
big_pic = Image.open('yy.png')
big_pic.paste(little_pic, (10, 20), mask=little_pic)

代码

import os

from PIL import Image
dirpath=r"D:/630/image"#大图文件夹
goatpath=r"D:/630/11"
if not os.path.exists(goatpath):
    os.mkdir(goatpath)
# Opening the primary image (used in background)

img1 = Image.open("zi1.png")#小图路径
img1 = img1.convert("RGBA")

i = 1
for curDir,dirs,files in os.walk(dirpath):
    for file in files:
        img2 = Image.open(curDir+"\\"+file)
        img2 = img2.convert("RGBA")
        img2.paste(img1, (325,160), mask=img1)
        img2.save(goatpath+"\\"+str(i)+".png")
        print(goatpath + "\\" + str(i) + ".png" + " has done")
        i=i+1
13 浏览
14 爬虫
0 评论