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
需求:需要将一张小图放置到大图里面,遍历大图文件夹,将小图按照(325,160)的坐标进行放置。
note:是用于批量处理图像的,具体坐标可以根据实际情况进行变化。
后期可能会用在数据集的扩展上,扩充为复杂的数据集