【发布时间】:2021-05-05 00:00:02
【问题描述】:
我正在尝试使用 Pillow 将文件夹中的所有图像文件转换为 jpg。
我是枕头新手,所以我不是 100% 掌握这些概念。
以下是我正在使用的功能:
def convert_jpg(file, folder = 'flag_images/', delete = False):
filepath = folder + file
print(file)
img = Image.open(filepath, mode = 'r')
if delete:
img.save(folder + 'backup/' + file)
os.remove(filepath)
if img.mode != 'RGB':
img.convert('RBG')
filepath = filepath[0:-3] + 'jpg'
img.save(filepath)
def convert_all(folder = 'flag_images/'):
for filename in os.listdir(os.path.abspath(os.getcwd()) + '/flag_images'):
if filename[-3:] != 'jpg':
convert_jpg(file = filename, folder = folder, delete = True)
在运行 convert_all 时,当我进入模式为“P”的文件时出现以下错误:
ValueError Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/PIL/Image.py in convert(self, mode, matrix, dither, palette, colors)
1025 try:
-> 1026 im = self.im.convert(mode, dither)
1027 except ValueError:
ValueError: conversion not supported
如何成功将模式转换为RGB,以便保存为jpg?
【问题讨论】:
标签: python image python-imaging-library rgb