对JPG图像进行操作时获得“无法将模式P作为JPEG写入”

发布于 2021-01-29 17:40:08

我正在尝试调整一些图像的大小,其中大多数是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

我没有更改图像类型,而是使用了枕头库。我的操作系统是Mac OSX。如何解决该问题?

关注者
0
被浏览
48
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看