图像转换-无法将RGBA模式写入JPEG [英] Image Conversion - Cannot write mode RGBA as JPEG
问题描述
我正在尝试调整&在项目中上传之前降低图像质量。这是我尝试过的,
I'm trying to resize & reduce quality of image before upload in project. Here's what I tried,
def save(self):
im = Image.open(self.image)
output = BytesIO()
im = im.resize(240, 240)
im.save(output, format='JPEG', quality=95)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg', sys.getsizeof(output), None)
super(Model, self).save()
如果我上传 jpg
图像可以正常工作,但是如果我上传 png
或其他任何图像类型,则无法正常工作引发类似无法将RGBA模式写入JPEG
& 无法将模式P编写为JPEG
等。
It's working fine if I upload a jpg
image but if I upload a png
or any other image type, it's not working it's raising errors like cannot write mode RGBA as JPEG
& cannot write mode P as JPEG
etc.
我们该如何解决?谢谢!
How can we fix that? Thank You!
推荐答案
如果您的image.mode是 P或 RGBA,并且您想将其转换为jpeg,则您需要先转换 image.mode
,因为jpeg不支持以前的模式
If your image.mode is "P" or "RGBA" and you want to convert it to jpeg then you need to first convert the image.mode
because the previous modes aren't supported for jpeg
if im.mode in ("RGBA", "P"):
im = im.convert("RGB")
https:// github。 com / python-pillow / Pillow / issues / 2609
这篇关于图像转换-无法将RGBA模式写入JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!