How to execute ImageMagick to convert only the first page of the multipage PDF to JPEG?
Asked Answered
C

4

82

How do I execute ImageMagick's convert if I want a JPEG from the first page only of a multi-page PDF?

Contractive answered 27/9, 2012 at 5:45 Comment(0)
B
127

If you are using a convert command line you can execute it with these parameters:

convert  source.pdf[0]  output.jpeg

Note that the page count of ImageMagick is 0-based. So [0] means 'page 1'. To select, say the 4th page, you'd have to use [3].

This syntax does not only work for PDF input. It also works with other multi-page or mult-frame formats, such as multi-page TIFF or animated multi-frame GIFs and PNGs.

Bashkir answered 27/9, 2012 at 5:48 Comment(6)
Thanks for the edit Kurt. Still, my version of imagemagick starts counting from 1 :) Could be legacy though.Olivares
By the way, you can also select ranges, e.g. using source.pdf[0-3].Jiggermast
@Arkadiusz'flies'Rzadkowolski: For me, ImageMagick and GraphicsMagick take the second page if I use [1], so it starts counting at 0. ImageMagick version 7.0.7-33, GraphicsMagick version 1.3.29.Boneset
It's also possible to select multiple pages -- [3,2,4] -- documentation: "Selecting Frames" section of imagemagick.org/script/command-line-processing.php#input .Balakirev
With ImageMagick 7.0.10-62, it doesn't work, unless quotes are added around the source file: convert 'source.pdf[0]' output.jpeg.Shakedown
@DenisBitouzé Works just fine for me without the quotes on ImageMagick 7.1.1-11. In fact, the quotes will be stripped by the shell and are only needed if the path contains whitespace.Sherillsherilyn
D
17

Don't use ImageMagick, use Ghostscript. ImageMagick calls Ghostscript to do the work anyway...

gs -sDEVICE=jpeg -sOutputFile=<output-filename> -dLastPage=1 <input filename>

You can also change the device to jpegcmyk (for CMYK output) or jpeggray for gray output, you can change the resolution using -r, use -dFirstPage and -dLastPage to extract a continuous range of pages, etc.

Dower answered 27/9, 2012 at 7:25 Comment(3)
To export as PNG, use this: -sDEVICE=png16m -r300 -dDownScaleFactor=4Vestiary
Just heading off other people at the pass: If you go the ghostscript route, it cannot maintain aspect ratios while scaling images. So if you're trying to, say, make a thumbnail of a PDF...this ain't gonna work.Annisannissa
Ghostscript is perfectly capable of maintaining aspect ratios. However if you use the -g switch (which specifies pixels) and the FitPage option, then it fits the page to the specified number of pixels. Clearly that doesn't maintain the aspect ratio. Of course you can avoid that too, but that takes some programming on your own part.Dower
R
6

To further the answer by @KenS, Here are a more few details, particularly for Windows users.

You can download GhostScript for Windows here: http://www.ghostscript.com/download/gsdnld.html. The default installation path for the executable is "C:\Program Files\gs\gs910\bin\gswin64c.exe".

The command-line arguments listed above are correct in Windows too, but here are a few more that I found useful:

gswin64c.exe -dNOPAUSE -dBATCH -r96 -sDEVICE=jpeg -sOutputFile="<out-file.jpg>"
             -dFirstPage=1 -dLastPage=1 "<input-file.pdf>"

I also created a batch file that wraps this up nicely and posted it to my GitHub account. It makes it a lot easier to create thumbnails for multiple .pdf files too. Check it out at pdf2jpg.bat.

Raisaraise answered 31/1, 2014 at 7:45 Comment(2)
I'm not really a Windows Shell script expert, so possibly I missed this in your (extensive!) script, but you can use format specifiers in the OutputFile. So if you wanted to produce a range of pages you could say -sOutputFile=out%d.jpg and you'll get multiple files named out1.jpg, out2.jpg... If you use %s with a separating device then the %s is replaced by the ink name for that plate. I'm not sure if that helps any.Dower
this answer could be improved by an explanation of the arguments you added and what they do.Debouchment
L
0

%d and some other qualifiers to nummerate output using ghostscript wont work with latest versions as they did with older versions.

Lizarraga answered 20/7, 2023 at 9:10 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Pistil

© 2022 - 2024 — McMap. All rights reserved.