GraphicsMagic is unable to identify background transparency of PDF

和自甴很熟 提交于 2021-02-18 18:54:59

问题


GM is unable to identify background transparency of PDF and PNG created using "gm convert" gets white background while same PDF is converted to PNG with transparent background by IM.

$convert -verbose /var/tmp/abc.pdf /var/tmp/abc.png
/var/tmp/magick-16370Tq7WYv5U54Pa1 PNG 288x720 288x720+0+0 8-bit sRGB 20.7KB 0.000u 0:00.009
/var/tmp/abc.pdf PDF 288x720 288x720+0+0 16-bit sRGB 20.7KB 0.000u 0:00.000
/var/tmp/abc.pdf=>/var/tmp/abc.png PDF 288x720 288x720+0+0 8-bit sRGB 17c 16.6KB 0.010u 0:00.009
[ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" "-sOutputFile=/var/tmp/magick-16370Tq7WYv5U54Pa%d" "-f/var/tmp/magick-16370CVWmPbzBmjpF" "-f/var/tmp/magick-16370khy6Y-G3TgtO"
$gm convert -verbose /var/tmp/abc.pdf /var/tmp/abc.png
gm convert: "gs" "-q" "-dBATCH" "-dMaxBitmap=50000000" "-dNOPAUSE" "-sDEVICE=pnmraw" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4" "-r72x72" "-sOutputFile=/var/folders/6d/n_hv45rs1jv17nxwfjwj776cspn_3c/T/gmoCp6rG" "--" "/var/folders/6d/n_hv45rs1jv17nxwfjwj776cspn_3c/T/gmBEgWnK" "-c" "quit".
/var/tmp/abc.pdf PDF 288x720+0+0 DirectClass 8-bit 607.6K 0.000u 0:01
/var/tmp/abc.pdf=>/var/tmp/abc.png PNG 288x720+0+0 DirectClass 8-bit 0.000u 0:01
Upon further investigation, it seems "identify" from IM can correctly identify background in PDF but "gm identify" from GM cannot.
$identify -verbose abc.pdf
Image: abc.pdf
Format: PDF (Portable Document Format)
Type: Bilevel
Colorspace: Gray
Depth: 16/4-bit
Channel depth:
gray: 1-bit
alpha: 4-bit
Alpha: graya(255,0) #FFFFFFFFFFFF0000
Colors: 16
Background color: graya(255,1)
Transparent color: graya(0,0)
Version: ImageMagick 6.8.9-1 Q16 x86_64 2014-07-01 http://www.imagemagick.org
$gm identify -verbose abc.pdf
gm identify: "gs" "-q" "-dBATCH" "-dMaxBitmap=50000000" "-dNOPAUSE" "-sDEVICE=pnmraw" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4" "-r72x72" "-sOutputFile=/var/folders/6d/n_hv45rs1jv17nxwfjwj776cspn_3c/T/gmzhBEIk" "--" "/var/folders/6d/n_hv45rs1jv17nxwfjwj776cspn_3c/T/gmAPm2Po" "-c" "quit".
Image: abc.pdf
Format: PDF (Portable Document Format)
Type: grayscale
Depth: 4 bits-per-pixel component
Channel Depths:
Gray: 4 bits
Background Color: white
Comment: Image generated by GPL Ghostscript (device=pnmraw)
Signature: 215f1c08ec575526ce398d193c4df22faaea100c10255e0db747641bdaaeac49
Tainted: False

回答1:


The reason why your (ImageMagick) convert and your (GraphicsMagick) gm convert commands produce different output is this:

  1. Both utilities are NOT able to process PDF input files directly, both can only handle rater image formats.

  2. In order to process PDF input files, both utilities resort to a 'delegate' program: in both cases this is Ghostscript (which CAN process PDF input files).

  3. Both utilities however do use different 'delegate command lines' (as can be directly seen in your quoted -verbose commandline outputs:

    i. convert employs as its Ghostscript output device pngalpha.

    ii. gm convertemploys as its Ghostscript output device pnmraw.

  4. Both utilities then process the output of their delegate's command into the final (raster) format file.

The problem is: the raster format 'pnmraw' does not support transparency (an alpha channel), but 'pngalpha' does. Hence, the utility which first converts PDF input to pnmraw has lost the transparent page backgrounds and replaced them by (opaque) white backgrounds.

Unless you modify your GraphicsMagick setup to make it use pngalpha in its delegate command (the same as ImageMagick uses) your gm convert will not show transparent background.




回答2:


Just wanted to add to Kurt Pfeifle answer since it pointed me to this solution. The configuration that he is referring to is found in the delegates.mgk file (graphicsmagick/1.3.19_1/lib/GraphicsMagick/config).

For me the issue was this line:

<!-- Read color Postscript, EPS, and PDF  -->
<delegate decode="gs-color" stealth="True" command='"gs" -q -dBATCH -dMaxBitmap=50000000 -dNOPAUSE -sDEVICE=ppmraw -dTextAlphaBits=%u -dGraphicsAlphaBits=%u -r%s %s "-sOutputFile=%s" -- "%s" -c quit' />

I changed it to:

  <delegate decode="gs-color+alpha" stealth="True" command='"gs" -q -dBATCH -dMaxBitmap=50000000 -dNOPAUSE -sDEVICE=pngalpha -dTextAlphaBits=%u -dGraphicsAlphaBits=%u -r%s %s "-sOutputFile=%s" -- "%s" -c quit' />

and my pngs come out with transparent backgrounds!



来源:https://stackoverflow.com/questions/24686623/graphicsmagic-is-unable-to-identify-background-transparency-of-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!