converting PDF nth to last page

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
arpeggio
Posts: 7
Joined: 2013-01-07T06:13:17-07:00
Authentication code: 6789

converting PDF nth to last page

Post by arpeggio »

Hi Guys,

to convert say a range of say the 1st to 5th page of a multipage pdf into single images is fairly straight forward using:
convert file.pdf[1-5] file.jpg

But how do i convert say the 5th to the last page when i dont know the number of pages in the pdf? Is there a way of doing this easily or i need to find the number of pages?

I hope someone can help. I've been trying multiple ways but to no avail. Thank you in advance.

Windows XP
ImageMagick-6.8.1-9-Q16-x86-dll
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting PDF nth to last page

Post by fmw42 »

Code: Select all

convert file.pdf[5--1] file.jpg


The -1 is the last image in the sequence. see http://www.imagemagick.org/Usage/basics/#list_ops
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: converting PDF nth to last page

Post by pipitas »

fmw42 wrote:

Code: Select all

convert file.pdf[5--1] file.jpg


The -1 is the last image in the sequence. see http://www.imagemagick.org/Usage/basics/#list_ops
To do fifth to last, you'd actually have to specify file.pdf[4--1] -- with file.pdf[5--1] you'd get sixth to last page.
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: converting PDF nth to last page

Post by pipitas »

arpeggio wrote:
to convert say a range of say the 1st to 5th page of a multipage pdf into single images is fairly straight forward using:
convert file.pdf[1-5] file.jpg
Actually, page counting is zero-based. So 1st to 5th page will require to use [0-4].

[1-5] will give you the second to sixth page.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting PDF nth to last page

Post by fmw42 »

User pipitas is correct. The indices are zero base. I had assumed you were actually wanting index 5 to last. It really depends upon what you meant.
arpeggio
Posts: 7
Joined: 2013-01-07T06:13:17-07:00
Authentication code: 6789

Re: converting PDF nth to last page

Post by arpeggio »

Hi guys,
Thank you very much for this. (Zero based, yes my bad!)

When i try: convert file.pdf[0-4] file.jpg, It Works fine.

But
When I try: convert file.pdf[4--1] file.jpg, i get, No pages will be processed (FirstPage > LastPage).

That is, Python says that because 5 is greater than -1, it doesnt understand. very strange.

Any ideas? Thank you in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: converting PDF nth to last page

Post by fmw42 »

That is a python issue. IM works fine. Try in commandline mode without python:

This works fine for me:

# create multipage image

Code: Select all

convert rose: rose: rose: rose: rose: rose: rose: rose: rose: rose: rose: rose: rose.gif
identify rose.gif
rose.gif[0] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[1] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[2] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[3] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[4] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[5] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[6] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[7] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[8] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[9] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[10] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
rose.gif[11] GIF 70x46 70x46+0+0 8-bit sRGB 256c 40.4KB 0.000u 0:00.000
# select 4 to last

Code: Select all

convert rose.gif[4--1] rose2.gif
identify rose2.gif
rose2.gif[0] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[1] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[2] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[3] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[4] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[5] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[6] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
rose2.gif[7] GIF 70x46 70x46+0+0 8-bit sRGB 256c 27.2KB 0.000u 0:00.000
Post Reply