在JPG图像上运行时,get“不能写入模式P为JPEG”。

时间:2021-12-30 16:18:50

I am trying to resize some images, most of which are JPG. But in a few images, I am getting the error:

我试着调整一些图像的大小,其中大部分是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

I am not changing the image type and I'm using the pillow library. My OS is Mac OS X. How can I resolve the issue?

我没有改变图像类型,我使用的是枕头库。我的操作系统是Mac OS x,我该如何解决这个问题?

1 个解决方案

#1


87  

You need to convert the image to RGB mode.

您需要将映像转换为RGB模式。

Image.open('old.jpeg').convert('RGB').save('new.jpeg')

#1


87  

You need to convert the image to RGB mode.

您需要将映像转换为RGB模式。

Image.open('old.jpeg').convert('RGB').save('new.jpeg')