81

我正在尝试调整一些图像的大小,其中大部分是 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 OS X。我该如何解决这个问题?

4

5 回答 5

161

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

Image.open('old.jpeg').convert('RGB').save('new.jpeg')
于 2014-02-10T05:37:35.437 回答
18

这个答案很老了,但是,我想我会通过在进行转换之前检查模式来采取更好的方法来做同样的事情:

if img.mode != 'RGB':
    img = img.convert('RGB')

这是将图像保存为 JPEG 格式所必需的。

于 2019-12-25T09:24:11.080 回答
6

摘要12

  • 背景
    • JPG不支持alpha = transparency
    • RGBA,Palpha = transparency
      • RGBA=Red Green Blue Alpha
  • 结果
    • cannot write mode RGBA as JPEG
    • cannot write mode P as JPEG
  • 解决方案
    • 在保存为 JPG 之前,丢弃alpha = transparency
      • 如:转换ImageRGB
    • 然后保存到JPG
  • 你的代码
if im.mode == "JPEG":
    im.save("xxx.jpg")
    # in most case, resulting jpg file is resized small one
elif rgba_or_p_im.mode in ["RGBA", "P"]:
    rgb_im = rgba_or_p_im.convert("RGB")
    rgb_im.save("xxx.jpg")
    # some minor case, resulting jpg file is larger one, should meet your expectation
  • 为您做更多:

关于调整 imgae 文件的大小,我已经实现了一个功能,供您参考:

from PIL import Image, ImageDraw
cfgDefaultImageResample = Image.BICUBIC # Image.LANCZOS

def resizeImage(inputImage,
                newSize,
                resample=cfgDefaultImageResample,
                outputFormat=None,
                outputImageFile=None
                ):
    """
        resize input image
        resize normally means become smaller, reduce size
    :param inputImage: image file object(fp) / filename / binary bytes
    :param newSize: (width, height)
    :param resample: PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, or PIL.Image.LANCZOS
        https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.thumbnail
    :param outputFormat: PNG/JPEG/BMP/GIF/TIFF/WebP/..., more refer:
        https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
        if input image is filename with suffix, can omit this -> will infer from filename suffix
    :param outputImageFile: output image file filename
    :return:
        input image file filename: output resized image to outputImageFile
        input image binary bytes: resized image binary bytes
    """
    openableImage = None
    if isinstance(inputImage, str):
        openableImage = inputImage
    elif CommonUtils.isFileObject(inputImage):
        openableImage = inputImage
    elif isinstance(inputImage, bytes):
        inputImageLen = len(inputImage)
        openableImage = io.BytesIO(inputImage)

    if openableImage:
        imageFile = Image.open(openableImage)
    elif isinstance(inputImage, Image.Image):
        imageFile = inputImage
    # <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=3543x3543 at 0x1065F7A20>
    imageFile.thumbnail(newSize, resample)
    if outputImageFile:
        # save to file
        imageFile.save(outputImageFile)
        imageFile.close()
    else:
        # save and return binary byte
        imageOutput = io.BytesIO()
        # imageFile.save(imageOutput)
        outputImageFormat = None
        if outputFormat:
            outputImageFormat = outputFormat
        elif imageFile.format:
            outputImageFormat = imageFile.format
        imageFile.save(imageOutput, outputImageFormat)
        imageFile.close()
        compressedImageBytes = imageOutput.getvalue()
        compressedImageLen = len(compressedImageBytes)
        compressRatio = float(compressedImageLen)/float(inputImageLen)
        print("%s -> %s, resize ratio: %d%%" % (inputImageLen, compressedImageLen, int(compressRatio * 100)))
        return compressedImageBytes

最新代码可以在这里找到:

https://github.com/crifan/crifanLibPython/blob/master/python3/crifanLib/thirdParty/crifanPillow.py

于 2020-12-08T01:43:43.473 回答
2

JPEG 不支持 alpha = 透明度 所以在保存到 JPEG 之前丢弃 alpha = 透明度

如果您使用不同的格式,您可以检查扩展名并专门为 JPEG 使用隐蔽。

extension = str(imgName).split('.')[-1]

if extension == "png":
    bg_image.save(imgName, "PNG")
else:
    if bg_image.mode in ("RGBA", "P"):
        bg_image = bg_image.convert("RGB")
    bg_image.save(imgName, "JPEG")
于 2021-06-24T13:45:42.470 回答
0

面临同样的错误,但在quantize()方法之后。

解决方法相同:转为RGB

image.quantize(colors=256, method=2).convert('RGB')

于 2021-06-15T09:05:14.320 回答