【发布时间】:2022-09-25 01:48:35
【问题描述】:
我有一个包含 JFIF 照片的文件夹,我需要将它们全部转换为 JPEG 格式,我认为问题出在我的 if 语句中,因为文件夹中有其他照片不需要转换或 for 循环.谢谢
from PIL import Image
import os
root = r\"(my computer path to the folder)\"
count = 0
for dirs, subdir, files in os.walk(root):
for file in files:
lastChar = file[-1:]
if(lastChar == \'f\'):
img = Image.open(file)
#file ends in .jfif, remove 4 characters
fileName = file[:-4]
#add jpg and save
img.save(fileName + \"jpg\")
我现在收到此错误, FileNotFoundError:[Errno 2] 没有这样的文件或目录:\'IMG_4242_1615254307.jfif\'
-
img = Image.open(file)- 这将在 CWD 中查找file,而不是在dirs文件夹中 -
@buran 我想知道
glob是否会更好地解决这个问题? -
我认为您可以将文件从
.jfif重命名为.jpg,而无需阅读它们、解压缩它们并重新压缩它们,并且可能会花费很长时间并降低质量。也许尝试重命名一个。
标签: python