鱼C论坛

 找回密码
 立即注册
查看: 2457|回复: 2

[已解决]求改为批量处理,增加打开保存路径

[复制链接]
发表于 2022-8-29 23:44:44 | 显示全部楼层 |阅读模式
10鱼币
from PIL import Image
image=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


程序在网络上找到,改了一下,搞不慬,理解不了,感谢帮忙。
最佳答案
2022-8-29 23:44:45
本帖最后由 qq1151985918 于 2022-8-30 00:44 编辑

批量处理就封装进函数啊,然后循环读取文件不就好了
  1. from PIL import Image
  2. import os

  3. def save(fp):
  4.     image=Image.open(fp)
  5.     image = image.convert('RGB')
  6.     pci = image.getpixel((10, 10))
  7.     w, h = image.size
  8.     background = Image.new('RGB', size=(max(w, h), max(w, h)), color = pci)  
  9.     length = int(abs(w - h) // 2)  
  10.     box = (length, 0) if w < h else (0, length)  
  11.     background.paste(image, box)
  12.     image_data=background.resize((256,256))
  13.     path, filename = os.path.split(fp)
  14.     savefp = os.path.join(path, '副本-' + filename)
  15.     background.save(savefp)

  16. if __name__ == '__main__':
  17.     path = r'C:\Users\Administrator\Desktop\image'
  18.     for fn in os.listdir(path):
  19.         save(os.path.join(path, fn))
复制代码

最佳答案

查看完整内容

批量处理就封装进函数啊,然后循环读取文件不就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-8-29 23:44:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 qq1151985918 于 2022-8-30 00:44 编辑

批量处理就封装进函数啊,然后循环读取文件不就好了
  1. from PIL import Image
  2. import os

  3. def save(fp):
  4.     image=Image.open(fp)
  5.     image = image.convert('RGB')
  6.     pci = image.getpixel((10, 10))
  7.     w, h = image.size
  8.     background = Image.new('RGB', size=(max(w, h), max(w, h)), color = pci)  
  9.     length = int(abs(w - h) // 2)  
  10.     box = (length, 0) if w < h else (0, length)  
  11.     background.paste(image, box)
  12.     image_data=background.resize((256,256))
  13.     path, filename = os.path.split(fp)
  14.     savefp = os.path.join(path, '副本-' + filename)
  15.     background.save(savefp)

  16. if __name__ == '__main__':
  17.     path = r'C:\Users\Administrator\Desktop\image'
  18.     for fn in os.listdir(path):
  19.         save(os.path.join(path, fn))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-8-30 08:15:40 | 显示全部楼层
qq1151985918 发表于 2022-8-30 00:08
批量处理就封装进函数啊,然后循环读取文件不就好了

感谢,因为不会就问一下,对比一下,容易理解
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 01:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表