我正在尝试调整一些图像的大小,其中大部分是 JPG。但在一些图像中,我收到了错误:
Traceback (most recent call last):
File "image_operation_new.py", line 168, in modifyImage
tempImage.save(finalName);
File "/Users/kshitiz/.virtualenvs/django_project/lib/python2.7/site- packages/PIL/Image.py", line 1465, in save
save_handler(self, fp, filename)
File "/Users/kshitiz/.virtualenvs/django_project/lib/python2.7/site- packages/PIL/JpegImagePlugin.py", line 455, in _save
raise IOError("cannot write mode %s as JPEG" % im.mode)
IOError: cannot write mode P as JPEG
我没有更改图像类型,而是使用 Pillow 库。我的操作系统是 Mac OS X。我该如何解决这个问题?
最佳答案
您需要将图像转换为 RGB 模式。
Image.open('old.jpeg').convert('RGB').save('new.jpeg')
关于python - 对 JPG 图像进行操作时获取 "cannot write mode P as JPEG",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669657/