鱼C论坛

 找回密码
 立即注册
查看: 1878|回复: 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 编辑

批量处理就封装进函数啊,然后循环读取文件不就好了
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))

最佳答案

查看完整内容

批量处理就封装进函数啊,然后循环读取文件不就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-29 23:44:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

感谢,因为不会就问一下,对比一下,容易理解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-15 18:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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