78

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

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
V A S
  • 3,338
  • 4
  • 33
  • 39

4 Answers4

120

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thanks for the edit Kurt. Still, my version of imagemagick starts counting from 1 :) Could be legacy though. – Arkadiusz 'flies' Rzadkowolski Oct 02 '12 at 05:46
  • 13
    By the way, you can also select *ranges*, e.g. using `source.pdf[0-3]`. – caw Nov 21 '16 at 22:07
  • @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. – Golar Ramblar May 21 '18 at 08:57
  • It's also possible to select multiple pages -- `[3,2,4]` -- documentation: "Selecting Frames" section of https://imagemagick.org/script/command-line-processing.php#input . – user202729 Nov 25 '20 at 06:25
  • With ImageMagick 7.0.10-62, it doesn't work, unless quotes are added around the source file: `convert 'source.pdf[0]' output.jpeg`. – Denis Bitouzé Mar 14 '22 at 16:19
16

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
KenS
  • 30,202
  • 3
  • 34
  • 51
  • To export as PNG, use this: ``-sDEVICE=png16m -r300 -dDownScaleFactor=4`` – OrangeSalty Nov 21 '16 at 12:07
  • 2
    Just heading off other people at the pass: If you go the ghostscript route, it [cannot maintain aspect ratios](https://stackoverflow.com/a/10022199/64911) while scaling images. So if you're trying to, say, make a thumbnail of a PDF...this ain't gonna work. – mlissner Feb 19 '18 at 19:27
  • 1
    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. – KenS Feb 19 '18 at 19:50
5

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.

kodybrown
  • 2,337
  • 26
  • 22
  • 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. – KenS Jan 31 '14 at 09:06
  • 1
    this answer could be improved by an explanation of the arguments you added and what they do. – But those new buttons though.. Jan 11 '19 at 22:38
0

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

  • 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](/help/how-to-answer). – Community Jul 26 '23 at 08:38