【问题标题】:How to convert an .eps file into a .png in Python 3.6如何在 Python 3.6 中将 .eps 文件转换为 .png
【发布时间】:2018-01-31 09:10:29
【问题描述】:

使用 Python turtle 我试图将画布保存为 png。我研究了它,找到了一种将其保存为不带任何模块的 eps 文件的方法,但我发现很难将 eps 文件转换为 png。

有没有办法在不下载其他模块的情况下将 eps 转换为 png?如果没有,有人可以告诉我更多关于 ImageMagick 的信息,因为我看过它,但我很困惑如何使用它?我也看到它被链接到 linux 并且它已经过时了?

如果不将 eps 转换为 png,是否有更简单的方法将画布另存为 png?

顺便说一句,我看过这个,但我不明白:/ How to convert a .eps file to a high quality 1024x1024 .jpg?

【问题讨论】:

标签: python windows python-3.x


【解决方案1】:

从您显示的链接中,有这个 Imagemagick 命令:

convert -density 300 image.eps -resize 1024x1024 image.jpg

大多数 EPS 文件都是矢量图像。它们没有以像素为单位的物理大小,因为它是带有描述如何绘制每个对象的命令的矢量绘图。它不是包含像素的光栅图像,也没有任何特定的像素尺寸集。

因此,对于矢量文件,您可以设置打印密度以告诉 Imagemagick(将其传递给 Ghostscript 进行光栅化工作)将矢量数据转换为光栅数据,然后将其保存为光栅格式的输出图像。标称密度为 72 dpi(有时为 92 或 96)。因此,如果您将 -density 288 与以下命令一起使用:

convert -density 288 image.eps image.png

这将导致图像在每个维度上都比你刚刚做的大 4 倍

convert image.eps image.png

默认 dpi 为 72 与

convert -density 72 image.eps image.png

注意 72*4=288。

现在您有一个大的高质量光栅 png,尤其是当 eps 文件是带有细线(如蓝图)的线条画时。

但是,如果它太大并且您想将其减小到其标称尺寸 1/4,您可以这样做(注意 1/4 = 25%)

convert -density 288 image.eps -resize 25% image.png

这个过程有时被称为超级采样,它会产生比仅仅做更好看的结果

convert image.eps image.png

在原始命令中,他们决定获取高质量的光栅图像并将大小调整为 1024x1024。

因此,在从 EPS 矢量图像生成高清光栅图像后,您可以调整为所需的任何大小。

您使用的密度越大,PNG 的质量就越高,但处理时间会更长。因此,您必须权衡时间与质量,并选择在合理的时间内产生足够好的质量的最小密度。

不知道Python Wand是否支持设置密度,或者是否支持读取PDF文件,这需要Ghostscript。但是您可以使用 Python Subprocess 模块来调用 Imagemagick 命令行。见https://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=32920

【讨论】:

    【解决方案2】:

    ImageMagick 的安全策略已更改,因此无法与 Ghostscript 交互,我一直遇到问题。 (for good reason... 但值得怀疑的是,它不允许您在本地覆盖默认策略,因此可以保护 Web 应用程序,而白名单使用仍然可以工作。)

    对于其他猛烈抨击convert: not authorized 的人,以下是直接调用 Ghostscript 的方法:

    gs -dSAFER -dEPSCrop -r300 -sDEVICE=jpeg -o image.png image.eps
    
    • -dSAFER 将 Ghostscript 置于沙盒模式,以便您可以解释不受信任的 Postscript。 (它应该是默认的,但向后兼容。)
    • -dEPSCrop 要求 Ghostscript 不要将其填充到可打印页面的大小。 (details)
    • ImageMagick 的 -density 300 在调用 Ghostscript 时变为 -r300。 (details)
    • -sDEVICE 是您设置输出格式的方式(其他选择请参见手册的Devices section。)
    • -o-dBATCH -dNOPAUSE -sOutputFile= (details) 的简写

    然后您可以使用 ImageMagick 的 mogrify 命令调整其大小以适合精确的像素尺寸:

    mogrify -resize 1024x1024 image.png
    

    mogrify 类似于 convert,但替换输入文件而不是写入新文件。)

    更新:事后看来,我应该在发布第一个答案之前检查 Pillow 是否支持 EPS。

    原生 Python 解决方案是使用 Pillow 对调用 Ghostscript 的支持(在这方面类似于 ImageMagick,但使用原生 Python API)。

    但是,Pillow 的文档没有解释它们是如何得出以像素为单位的大小的,并且只采用一个可选的乘数 (scale) 作为默认大小,而不是绝对 DPI 值。

    如果这不打扰您,并且您已经安装了 Pillow 和 Ghostscript,那么以下是不使用 ImageMagick 的方法:

    #!/usr/bin/env python3
    
    from PIL import Image
    
    TARGET_BOUNDS = (1024, 1024)
    
    # Load the EPS at 10 times whatever size Pillow thinks it should be
    # (Experimentaton suggests that scale=1 means 72 DPI but that would
    #  make 600 DPI scale=8⅓ and Pillow requires an integer)
    pic = Image.open('image.eps')
    pic.load(scale=10)
    
    # Ensure scaling can anti-alias by converting 1-bit or paletted images
    if pic.mode in ('P', '1'):
        pic = pic.convert("RGB")
    
    # Calculate the new size, preserving the aspect ratio
    ratio = min(TARGET_BOUNDS[0] / pic.size[0],
                TARGET_BOUNDS[1] / pic.size[1])
    new_size = (int(pic.size[0] * ratio), int(pic.size[1] * ratio))
    
    # Resize to fit the target size
    pic = pic.resize(new_size, Image.ANTIALIAS)
    
    # Save to PNG
    pic.save("image.png")
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2018-04-07
      • 2013-09-09
      • 2017-07-26
      相关资源
      最近更新 更多