Page 1 of 1

Create a big image croping top and bottom of pdf

Posted: 2017-12-07T20:05:40-07:00
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

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

Posted: 2017-12-07T20:28:31-07:00
by fmw42
Please always provide your IM version and platform, since syntax may differ. Things may be easier with IM 7.

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

Posted: 2017-12-07T20:29:11-07:00
by jauson
IM 6.9, OS linux/windows.

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

Posted: 2017-12-07T21:02:14-07:00
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

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

Posted: 2017-12-07T21:34:45-07:00
by jauson
Version: ImageMagick 6.9.9-10

I was hoping to avoid scripts. Can this be done nativly with IM.

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

Posted: 2017-12-07T21:42:53-07:00
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:

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

Posted: 2017-12-07T22:03:51-07:00
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

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

Posted: 2017-12-07T22:22:06-07:00
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.

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

Posted: 2017-12-07T22:30:43-07:00
by jauson
I think the last one works well. thanks.

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

Posted: 2017-12-08T00:09:52-07:00
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.

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

Posted: 2017-12-08T07:56:36-07:00
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.

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

Posted: 2017-12-08T08:05:17-07:00
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.

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

Posted: 2017-12-08T14:25:08-07:00
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