Generathing PNG thumb from PDF without temporarily file (output format, fd:X, errors)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jmto
Posts: 2
Joined: 2018-12-12T05:07:33-07:00
Authentication code: 1152

Generathing PNG thumb from PDF without temporarily file (output format, fd:X, errors)

Post by jmto »

Hi,

I'm puzzled how one should be able to that. I've tried many ways and always there is something weird.
Let's start with the obvious:
https://imagemagick.org/script/command- ... essing.php
When needed, explicit image formats can be given as mentioned earlier, as in the following.
magick gif:fd:3 jpg:fd:4 -append tif:fd:5

Code: Select all

$ convert --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

$ cat test.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" png:fd:42 42>thumb.png
$ ls -la thumb.png fd\:42
-rw------- 1 jmto jmto 24372 Dec 12 14:13 fd:42
-rw-rw-r-- 1 jmto jmto     0 Dec 12 14:13 thumb.png
What? That should have told create png to file description 42.. no. It created file fd:42.
This would work if it would do as the documentation says.

Well, what's the default output then? It's that ok.

Code: Select all

$ cat Jukola.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" fd:42 42>thumb.png; ls -la fd:42 thumb.png ; file thumb.png
ls: cannot access 'fd:42': No such file or directory
-rw-rw-r-- 1 jmto jmto 20145 Dec 12 14:17 thumb.png
thumb.png: PNG image data, 100 x 147, 8-bit/color RGBA, non-interlaced
$ cat test.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" fd:42 42>thumb.png; ls -la fd:42 thumb.png ; file thumb.png
ls: cannot access 'fd:42': No such file or directory
-rw-rw-r-- 1 jmto jmto 41715 Dec 12 14:17 thumb.png
thumb.png: Netpbm image data, size = 100 x 139, rawbits, pixmap
Oh, netpbm?! What, so there is no constant default output format, that's weird.
This would work if there would be default output format, like PNG.

At this point you might think fd:42 what's wrong with just using stdout? Let's try that then.

Code: Select all

$ cat Jukola.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" png:- 1>thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 20145 Dec 12 14:19 thumb.png
thumb.png: PNG image data, 100 x 147, 8-bit/color RGBA, non-interlaced
$ cat test.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" png:- 1>thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 24372 Dec 12 14:19 thumb.png
thumb.png: PNG image data, 100 x 139, 8-bit/color RGBA, non-interlaced
$ cat Keskuspuisto18.pdf | convert -quiet pdf:-[0] -thumbnail "100x>" png:- 1>thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 27024 Dec 12 14:20 thumb.png
thumb.png: data
$ head -5 thumb.png
   **** Error: Ignoring changes to the graphic state after operator 'W'.
               Output may be incorrect.
   **** Error reading a content stream. The page may be incomplete.
               Output may be incorrect.
   **** Error: Form stream has unbalanced q/Q operators (too many q's)
Oh, there are errors, that are written to STDOUT?! Not STDERR?! Ok, can I suppress the errors? No. Didn't find any way of doing that.
This would work if I would suppress the errors, or the errors would be written to STDERR.

So how to do this so that it works? There are multiple ways, but they all have problems. One how should solve this simple problem?

Task is: I want to generate thumbnails of PDF documents by writing the PDF to convert with pipe, and by reading the thumbnail with some file description (42 or stdout). How to do this?
jmto
Posts: 2
Joined: 2018-12-12T05:07:33-07:00
Authentication code: 1152

Re: Generathing PNG thumb from PDF without temporarily file (output format, fd:X, errors)

Post by jmto »

So I found to solutions.

#1 The bad one.
Use gif format, gif:fd:42 seems to work with my convert version. GIF is so 90's, so just no.

#2 The better one.
Use stdout, png:-, but edit delegates.xml to contain -sstdout=/dev/null for ghostscript.

Code: Select all

sudo perl -pi -e 's#-q -dQUIET#-q -sstdout=/dev/null -dQUIET#g' /etc/ImageMagick-6/delegates.xml
So example clip from delegates.xml:

Code: Select all

<delegate decode="ps:alpha" stealth="True" command="&quot;gs&quot; -q -sstdout=/dev/null -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
And then the three test files:

Code: Select all

$ rm thumb.png; cat Keskuspuisto18.pdf | convert pdf:-[0] -thumbnail "100x>" png:- >thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 25581 Dec 13 11:11 thumb.png
thumb.png: PNG image data, 100 x 69, 8-bit/color RGBA, non-interlaced
$ rm thumb.png; cat Jukola.pdf | convert pdf:-[0] -thumbnail "100x>" png:- >thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 20145 Dec 13 11:11 thumb.png
thumb.png: PNG image data, 100 x 147, 8-bit/color RGBA, non-interlaced
$ rm thumb.png; cat test.pdf | convert pdf:-[0] -thumbnail "100x>" png:- >thumb.png; ls -la thumb.png ; file thumb.png
-rw-rw-r-- 1 jmto jmto 24372 Dec 13 11:11 thumb.png
thumb.png: PNG image data, 100 x 139, 8-bit/color RGBA, non-interlaced
Post Reply