谁能告诉我如何将本地文件夹中的所有 png 图像转换为 jpg 图像?我试过使用下面的代码
path1 = r'C:\Users\david.han\Desktop\COPY_images_files'
path2 = r'C:\Users\david.han\Desktop\JPG converter'
files_to_convert = (f for f in os.listdir(path1) if f.endswith(".png"))
for filename in files_to_convert:
im = Image.open(os.path.join(path1, filename))
root, _ = os.path.splitext(filename)
jpg_file = os.path.join(path2, f'{root}.jpg')
im.save(jpg_file)
我不断收到此错误“OSError:无法将模式 P 写入 JPEG”
最佳答案
我决定保留 JPG,但万一有人想知道如何将 png 更改为 jpg
enter code here
from PIL import Image
import os
path = r'C:\Users\david.han\Desktop\COPY_images_files'
for file in os.listdir(path):
if file.endswith(".jpg"):
img = Image.open(file)
file_name, file_ext = os.path.splitext(file)
img.save('/png/{}.png'.format(file_name))
关于python - 将 PNG 图像从文件夹转换为 JPG - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63607370/