【问题标题】:Converting ImageMagick command to GraphicsMagick将 ImageMagick 命令转换为 GraphicsMagick
【发布时间】:2020-03-23 22:52:59
【问题描述】:

为了提高转换速度并避免出现错误,我想将以下 ImageMagick 命令转换为 GraphicsMagick 命令。

这会将任何大小的 PDF 向上转换为 300 DPI,然后在 ImageMagick 中将该 PDF 转换为高质量的 8.5x11 PDF。

convert -density 300 ~/Desktop/10x11.pdf -density 300 -resize 2550x3300 -gravity center -extent 2550x3300 -colorspace Gray ~/Desktop/8.5x11.pdf

在 GraphicsMagick 中运行相同的命令会生成 35x45 英寸的 PDF。这是因为由于某种原因,最终 PDF 的解释密度为 72(而不是 300)。

gm convert -density 300 ~/Desktop/10x11.pdf -density 300 -resize 2550x3300 -gravity center -extent 2550x3300 -colorspace Gray ~/Desktop/35x45.pdf

以下生成(模糊的)8.5x11 英寸 PDF。

gm convert -density 300 ~/Desktop/10x11.pdf -density 300 -resize 612x792 -gravity center -extent 612x792 -colorspace Gray ~/Desktop/8.5x11.pdf

关于我在这里做错了什么有什么想法吗?旨在使用 GraphicsMagick 生成清晰的 8.5x11 英寸 PDF。

【问题讨论】:

    标签: pdf imagemagick graphicsmagick


    【解决方案1】:

    我知道您要求的是 graphicsmagick 解决方案,但如果您对其他库开放,libvips 通常在执行此类任务时更快。

    Imagemagick 和 Graphicsmagick 使用 Ghostscript 进行 PDF 渲染。 libvips 使用 poppler 代替:这个库可以从任何大小的 PDF 生成高质量的抗锯齿位图。您无需先以高分辨率渲染然后缩小尺寸。

    在这台笔记本电脑上,我看到了:

    $ /usr/bin/time -f %M:%e convert -density 300 ISO_12233-reschart.pdf -density 300 -resize 2550x3300 -gravity center -extent 2550x3300 -colorspace Gray x.pdf
    250460:2.51
    

    因此需要 250MB 的内存和 2.5 秒的时间来生成您的 PDF。

    libvips 等价物是:

    #!/bin/bash
    
    vips thumbnail $1 t1.v 2550 --height 3300 
    vips colourspace t1.v t2.v b-w
    # drop any alpha channels
    if [ $(vipsheader -f bands t2.v) -gt 1 ]; then
            vips extract_band t2.v t3.v 0
            mv t3.v t2.v
    fi
    vips gravity t2.v t3.v centre 2550 3300 --extend white
    vips magicksave t3.v $2
    

    我明白了:

    $ /usr/bin/time -f %M:%e ./process.sh ISO_12233-reschart.pdf x.pdf
    110168:1.05
    

    110MB 内存和 1.05 秒。

    您确定需要 PDF 输出吗?您生成的 PDF 不是“真正的”PDF,它们是带有 PDF 包装的位图。我会改用 PNG:

    #!/bin/bash
    
    vips thumbnail $1 t1.v 2550 --height 3300 
    vips colourspace t1.v t2.v b-w
    # drop any alpha channels
    if [ $(vipsheader -f bands t2.v) -gt 1 ]; then
            vips extract_band t2.v t3.v 0
            mv t3.v t2.v
    fi
    vips gravity t2.v $2 centre 2550 3300 --extend white
    

    我现在明白了:

    $ /usr/bin/time -f %M:%e ./process.sh ~/pics/ISO_12233-reschart.pdf x.png
    58324:0.59
    

    60MB 内存和 0.6 秒。

    如果您能够使用 Python 之类的东西来代替 bash,那么您可以更快地获得它。

    #!/usr/bin/python3
    
    import sys
    import pyvips
    
    x = pyvips.Image.thumbnail(sys.argv[1], 2550, height=3300)
    x = x.colourspace("b-w")
    if x.bands > 1:
        x = x.extract_band(0)
    x = x.gravity("centre", 2550, 3300, extend="white")
    x.write_to_file(sys.argv[2])
    

    现在 0.5 秒,因为没有临时文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-20
      • 2015-06-16
      • 2023-03-25
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2018-01-11
      • 2012-10-30
      相关资源
      最近更新 更多