Create a big image croping top and bottom of pdf

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
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Create a big image croping top and bottom of pdf

Post by jauson »

I have a pdf with multiple pages, I want to take a top crop, and bottom crop, and make one big image of all of them combined vertically.

Code: Select all

convert \
  \( -density 144 my.pdf -gravity north -crop x200+0+0 +repage -colorspace gray -alpha remove \)  \
  \( -density 144 my.pdf -gravity south -crop x200+0+0 +repage -colorspace gray -alpha remove  \) -append \ 
   output.png
This works but instead of creating a top and bottom sections, it creates all tops, and then appends all bottoms after. I want top/bottom sections.


Sample of what works but I have to call for each page, but I want a simple line like above...

Code: Select all

convert \
  \( -density 144 my.pdf[0] -gravity north -crop x200+0+0 +repage -colorspace gray -alpha remove \)  \
  \( -density 144 my.pdf[0] -gravity south -crop x200+0+0 +repage -colorspace gray -alpha remove \) \
  \( -density 144 my.pdf[1] -gravity north -crop x200+0+0 +repage -colorspace gray -alpha remove \)  \
  \( -density 144 my.pdf[1] -gravity south -crop x200+0+0 +repage -colorspace gray -alpha remove \) -append \
  output.png
Last edited by jauson on 2017-12-07T20:45:00-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a big image croping top and bottom of pdf

Post by fmw42 »

Please always provide your IM version and platform, since syntax may differ. Things may be easier with IM 7.
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Create a big image croping top and bottom of pdf

Post by jauson »

IM 6.9, OS linux/windows.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a big image croping top and bottom of pdf

Post by fmw42 »

In the future, please give your full IM version 6.9.x.x.

Here is one way in unix using a script loop over each page:

Code: Select all

numpages=`convert test.pdf -format "%n\n" info: | head -n +1`
for ((i=0; i<numpages; i++)); do
convert -density 144 test.pdf[$i] -colorspace gray -alpha off +write mpr:img +delete \
\( mpr:img -gravity north -crop x200+0+0 +repage \) \
\( mpr:img -gravity south -crop x200+0+0 +repage \) \
-append result_$i.png
done
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Create a big image croping top and bottom of pdf

Post by jauson »

Version: ImageMagick 6.9.9-10

I was hoping to avoid scripts. Can this be done nativly with IM.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a big image croping top and bottom of pdf

Post by fmw42 »

I do not know a way. Perhaps one of the other experts can provide a better solution. IM 7 may be more flexible to do that but may require more memory usage.

You could try this, but you have to make a pair of parenthesis commands for each page.

Code: Select all

convert \
\( -density 144 test.pdf[0] -gravity north -crop x200+0+0 +repage -colorspace gray -alpha remove \) \
\( -density 144 test.pdf[0] -gravity south -crop x200+0+0 +repage -colorspace gray -alpha remove \) \
-append +write page_0.png \
\( -density 144 test.pdf[1] -gravity north -crop x200+0+0 +repage -colorspace gray -alpha remove \) \
\( -density 144 test.pdf[1] -gravity south -crop x200+0+0 +repage -colorspace gray -alpha remove \) \
-append +write page_1.png \
...
null:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create a big image croping top and bottom of pdf

Post by snibgo »

If all the inputs are the same size, calculate the height minus 2*200, and "-chop" that out of every image, then append.

If they are different heights, you need to calculate the chop height, which you can do in v7:

Code: Select all

magick image.pdf -gravity Center -chop x%[fx:h-400] -append out.png
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Create a big image croping top and bottom of pdf

Post by GeeMack »

jauson wrote: 2017-12-07T21:34:45-07:00I was hoping to avoid scripts. Can this be done nativly with IM.
If the images are all the same size, snibgo's suggestion above might be the simplest approach. Here's another idea that should work even if the pages of the PDF are different sizes...

Code: Select all

convert -density 144 \
   my.pdf -crop 0x200+0+0 -extent 0x400 null: -gravity south \
   ( my.pdf -crop 0x200+0+0 ) +repage -layers composite -append output.png
That reads in all the pages, crops the top 200 pixels of each page, and extends all the canvases by another 200 pixels below. Then it reads in the PDF again and crops the bottom 200 pixels of each page. Now you have two stacks separated by the special image "null:". Use "-layers composite" to composite those bottom pieces over the lower half of the extended top pieces. Finish with "-append" to attach them all vertically.
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Create a big image croping top and bottom of pdf

Post by jauson »

I think the last one works well. thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a big image croping top and bottom of pdf

Post by fmw42 »

convert -density 144 \
my.pdf -crop 0x200+0+0 -extent 0x400 null: -gravity south \
( my.pdf -crop 0x200+0+0 ) +repage -layers composite -append output.png
On my system it seems to make one big page appending all the pairs into one page. I thought the OP wanted separate pages.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Create a big image croping top and bottom of pdf

Post by GeeMack »

jauson wrote: 2017-12-07T22:30:43-07:00I think the last one works well. thanks.
There are several ways to accomplish most tasks with ImageMagick. Here's another way to approach this one...

Code: Select all

convert -density 144 my.pdf -roll +0+200 -extent 0x400 -roll +0+200 -append output.png
That reads in all the pages of the PDF and rolls them downward so the bottom 200 pixels are now at the top and the top 200 pixels are immediately below that. Then it uses "-extent" to crop that top 400 pixels of each image. Now roll them all downward 200 pixels again to essentially slide the upper and lower halves back to their original positions.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Create a big image croping top and bottom of pdf

Post by GeeMack »

fmw42 wrote: 2017-12-08T00:09:52-07:00On my system it seems to make one big page appending all the pairs into one page. I thought the OP wanted separate pages.
The examples had an "-append" just before the output image name.
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Create a big image croping top and bottom of pdf

Post by jauson »

Just discovered page ranges...

Code: Select all

convert -density 144  my.pdf[1-10] -roll +0+200 -extent 0x400 -roll +0+200 -append output_roll.png
Post Reply