求改为批量处理,增加打开保存路径
from PIL import Imageimage=Image.open("2.jpg")
image = image.convert('RGB')
pci = image.getpixel((10, 10))
w, h = image.size
background = Image.new('RGB', size=(max(w, h), max(w, h)), color = pci)
length = int(abs(w - h) // 2)
box = (length, 0) if w < h else (0, length)
background.paste(image, box)
image_data=background.resize((256,256))
background.s
程序在网络上找到,改了一下,搞不慬,理解不了,感谢帮忙。 本帖最后由 qq1151985918 于 2022-8-30 00:44 编辑
批量处理就封装进函数啊,然后循环读取文件不就好了
from PIL import Image
import os
def save(fp):
image=Image.open(fp)
image = image.convert('RGB')
pci = image.getpixel((10, 10))
w, h = image.size
background = Image.new('RGB', size=(max(w, h), max(w, h)), color = pci)
length = int(abs(w - h) // 2)
box = (length, 0) if w < h else (0, length)
background.paste(image, box)
image_data=background.resize((256,256))
path, filename = os.path.split(fp)
savefp = os.path.join(path, '副本-' + filename)
background.save(savefp)
if __name__ == '__main__':
path = r'C:\Users\Administrator\Desktop\image'
for fn in os.listdir(path):
save(os.path.join(path, fn)) qq1151985918 发表于 2022-8-30 00:08
批量处理就封装进函数啊,然后循环读取文件不就好了
感谢,因为不会就问一下,对比一下,容易理解
页:
[1]