【问题标题】:Saving to EPS not supported in Python Pillow?Python Pillow 不支持保存到 EPS?
【发布时间】:2017-11-20 17:58:00
【问题描述】:

我正在尝试使用 Pillow 将图像从 PNG 转换为 EPS。以下代码报错:

from PIL import Image

Image.open("Image1.png").save("Image1.eps", fmt='EPS')

内容如下:

Traceback (most recent call last):
  File "C:/Users/pbreach/Dropbox/Personal/FigureConversion/convert.py", line 15, in <module>
    convert_image(in_name, out_name, fmt='EPS')
  File "C:/Users/pbreach/Dropbox/Personal/FigureConversion/convert.py", line 4, in convert_image
    Image.open(in_name).save(out_name, fmt)
  File "C:\Users\pbreach\Continuum\Anaconda3\lib\site-packages\PIL\Image.py", line 1826, in save
    save_handler(self, fp, filename)
  File "C:\Users\pbreach\Continuum\Anaconda3\lib\site-packages\PIL\EpsImagePlugin.py", line 362, in _save
    raise ValueError("image mode is not supported")
ValueError: image mode is not supported

真的不支持EPS吗?在documentation EPS 在完全支持的格式列表中排名第二。如果不是这种情况,我需要做什么吗?

奇怪的是,如果我这样做 Image.open("Image1.png").save("Image1.jpg", fmt='EPS') 它可以工作,但会保存为 JPG。

【问题讨论】:

    标签: python image save pillow file-format


    【解决方案1】:

    Pillow 支持 EPS,但不能用 alpha 通道写入 (RGBA, LA) https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html?highlight=eps#eps:

    Pillow 识别包含图像数据的 EPS 文件,并且可以读取文件 包含嵌入式光栅图像(ImageData 描述符)。如果 Ghostscript 可用,也可以读取其他 EPS 文件。每股收益 司机也可以写EPS图像。 EPS 驱动程序可以读取 EPS 图像 在 L、LAB、RGB 和 CMYK 模式下,但 Ghostscript 可能会转换图像 RGB 模式,而不是将它们留在原始色彩空间中。这 EPS 驱动可以在 L、RGB 和 CMYK 模式下写入图像。

    帮助我在保存前将图像转换为RGB 模式

    from PIL import Image
    
    fig = Image.open("Image1.png")
    
    if fig.mode in ('RGBA', 'LA'):
        # https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html?highlight=eps#eps
        print('Current figure mode "{}" cannot be directly saved to .eps and should be converted (e.g. to "RGB")'.format(fig.mode))
        fig = fig.convert('RGB')
    
    out_fig = "Image1.eps"
    fig.save(out_fig)
    fig.close()
    

    但有时我遇到了问题:.eps 的背景为黑色,而不是透明的.png。对我来说,帮助https://stackoverflow.com/a/35859141/7444782 中的remove_transparency() 函数将透明背景替换为指定颜色(默认为白色)

    from PIL import Image
    
    def remove_transparency(im, bg_color=(255, 255, 255)):
        """
        Taken from https://stackoverflow.com/a/35859141/7444782
        """
        # Only process if image has transparency (http://stackoverflow.com/a/1963146)
        if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info):
    
            # Need to convert to RGBA if LA format due to a bug in PIL (http://stackoverflow.com/a/1963146)
            alpha = im.convert('RGBA').split()[-1]
    
            # Create a new background image of our matt color.
            # Must be RGBA because paste requires both images have the same format
            # (http://stackoverflow.com/a/8720632  and  http://stackoverflow.com/a/9459208)
            bg = Image.new("RGBA", im.size, bg_color + (255,))
            bg.paste(im, mask=alpha)
            return bg
        else:
            return im
    
    fig = Image.open("Image1.png")
    
    if fig.mode in ('RGBA', 'LA'):
        # https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html?highlight=eps#eps
        print('Current figure mode "{}" cannot be directly saved to .eps and should be converted (e.g. to "RGB")'.format(fig.mode))
        fig = remove_transparency(fig)
        fig = fig.convert('RGB')
    
    out_fig = "Image1.eps"
    fig.save(out_fig)
    fig.close()
    

    【讨论】:

      【解决方案2】:

      可能是您有一个带有 Alpha 通道的 png。 PIL 中的 EPS 不支持光栅图像的透明度。

      因此,如果您通过 im[:,:,0:2] 删除 alpha 通道,它可能会像一个魅力一样工作。 但是,它将不止一行。

      【讨论】:

        猜你喜欢
        • 2017-05-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-12
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多