为什么在变量中使用Image.save()(在Python中来自Pillow)不起作用?

2024-06-17 09:35:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python课程中,我必须编写一个脚本,将一堆图像(格式(.tiff)和大小错误)转换为“.jpeg”,并将它们以相同的名称保存到另一个文件夹中

问题是,它无法使用path+file变量+图像格式将其保存到我想要的目录(即使在同一个目录上)。我也使用了os.path.join()方法,但它也不起作用。我成功地做了几次,但没有在我想要的目录中。你能给我一些建议吗?谢谢大家!

#!/usr/bin/env python3

from PIL import Image
import os

files = os.listdir('/home/dani/images')
if not os.path.exists('/home/dani/images/opt/icons'):
    os.makedirs('/home/dani/images/opt/icons')
for file in files[1:]:
    if not os.path.isdir('/home/dani/images/'+ file): 
        im = Image.open('/home/dani/images/'+file)
        im.convert('RGB').rotate(-90).resize((128,\
    128)).save('/home/dani/images/opt/icons/'+file +'.jpeg')

1条回答
网友
1楼 · 发布于 2024-06-17 09:35:18

有时保存时会遇到权限问题。尝试将文件保存在与.py文件相同的文件夹中。 如果在同一文件夹中保存有效,则问题在于权限。试试os.umask()

相关问题 更多 >