Page 1 of 2

Convert section of PDF?

Posted: 2016-10-09T20:16:15-07:00
by pctechtv
I have a need to create a JPEG image from part of PDF file. I need to run this via a command line. It will be used in a script. I would like to know if this is possible with ImageMagick. I need the top of the page to be the width with a 16:9 ratio result. Is this possible in ImageMagick? Thanks.

Re: Convert section of PDF?

Posted: 2016-10-09T20:41:08-07:00
by fmw42
What is your IM version and platform? Please always provide that as syntax may differ.

I am not sure I understand your request. What do you mean by "I need the top of the page to be the width with a 16:9 ratio result"?

Can you explain further what part of the PDF file you want to make into a JPG image?

Is that 16:9 as width:height or height:width?

Do you mean take the top part of the PDF, such that your grab the full width and make the height=(9/16)*width?

A diagram might help! You can upload any diagram or your input PDF to some place such as dropbox.com and put the URL here.

Re: Convert section of PDF?

Posted: 2016-10-09T21:33:03-07:00
by pctechtv
Hi, and thanks for the help. You have it correct... grab the full width and make the height=(9/16). I need to process and use as a an image programmatically in script.
http://pctechtv.com/extra/forum/imageMa ... dfArea.png

Thanks!

Re: Convert section of PDF?

Posted: 2016-10-09T21:37:15-07:00
by pctechtv
Sorry the version I am using is ImageMagick-7.0.3-3-Q16-x64, I am on Window 10 64bit.

Re: Convert section of PDF?

Posted: 2016-10-09T21:58:32-07:00
by GeeMack
pctechtv wrote:Hi, and thanks for the help. You have it correct... grab the full width and make the height=(9/16). I need to process and use as a an image programmatically in script.
If the 16:9 section you want starts at the top left of the page, this command should extract that from the first page of a PDF...

Code: Select all

magick -density 300 mypdf.pdf[0] -crop %[w]x%[fx:w/16*9]+0+0 output.jpg
You can select which page to process by putting the page number in square brackets at the end of the filename as I did with "mypdf.pdf[0]" in that example. Remeber IM starts counting pages at 0.

You may need to specify a background color and maybe flatten the image onto the background if the background is transparent in the original PDF. Put "-background white -flatten" after the input file name.

You can change the input size and resolution by changing that "-density" setting that comes before the input filename. You can change the output size by doing a "-resize" operation before the output filename.

If you're including the command in a BAT script on Windows you'll need to use double percent signs "%%" anywhere you use single ones on the command line.

Re: Convert section of PDF?

Posted: 2016-10-09T23:07:53-07:00
by pctechtv
Thanks for the help. This community is great so fast to help. How should I be entering my file location I have entered with/without quotes and I keep getting something like this.
FailedToExecuteCommand `"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -... much longer saying no file found and other things. I am entering a full path like so "C:\CTestAC1\Pointer.pdf"

Code: Select all

magick -density 300 "C:\CTestAC1\Pointer.pdf"[0] -crop %[w]x%[fx:w*0.5625]+0+0 "C:\CTestAC1\School1.pdf"
Thanks

Re: Convert section of PDF?

Posted: 2016-10-09T23:46:58-07:00
by snibgo
You probably haven't installed Ghostscript.

Re: Convert section of PDF?

Posted: 2016-10-10T03:56:25-07:00
by pctechtv
You probably haven't installed Ghostscript.
That is exactly what the problem was. Thanks so much!

Re: Convert section of PDF?

Posted: 2016-10-13T11:46:37-07:00
by pctechtv
The code sample @GeeMack provided is really cool because it captures a 16:9 ratio no matter what the size of the pdf is. I now see that if my pdf is wider than 8.5 inches I want to scale it down. How could I make the output of the image scale to an 8.5 inch width but stay 16:9 after capture? Thanks

Re: Convert section of PDF?

Posted: 2016-10-13T12:26:22-07:00
by snibgo
pctechtv wrote:How could I make the output of the image scale to an 8.5 inch width but stay 16:9 after capture?
Do you want to change the number of pixels so that the density (dots per inch) remain the same but the number of inches changes to 8.5?

Or do you simply want to change the density so the number of pixels stays the same but they span 8.5 inches?

Re: Convert section of PDF?

Posted: 2016-10-13T14:57:48-07:00
by pctechtv
Because I want to sometimes print the image (on 8.5 x 11) paper, I want to keep the whole page width in view. so I think that is the first option. I want to scale the page down evenly. Evenly to 8.5 width but still see everything. Actually, I may have confused things by saying, "after capture." It can happen within the capture command. If the original command also scales the size to 8.5 inch if would be great. Thanks

Re: Convert section of PDF?

Posted: 2016-10-13T16:03:12-07:00
by snibgo
At 300 dpi, the width 8.5 inches will be 300*8.5=2550 pixels. The height will be 8.5*9/16 inches, or 300*8.5*9/16=1434 pixels.

So add "-resize 2550x1434" after the crop. (I would also add "+repage" between the crop and the resize.)

Re: Convert section of PDF?

Posted: 2016-10-13T18:50:18-07:00
by GeeMack
pctechtv wrote:If the original command also scales the size to 8.5 inch if would be great. Thanks
You're using ImageMagick 7, so you have some really handy options for doing just that. If you want to resize to 8.5 inches wide using the current resolution you can do the calculation right in the resize operation, something like this...

Code: Select all

magick -density 300 mypdf.pdf[0] ^
    -crop %[w]x%[fx:w/16*9]+0+0 +repage -resize %[fx:resolution.x*8.5]x output.jpg
That will make the width of your output 2550 pixels with the 300 pixel resolution (-density), and the height will change to whatever is needed to maintain the 16:9 aspect. If you were to change the density to 600 in the command, the output will resize to 5100 pixels/dots wide. That FX calculation just multiplies the resolution by your desired width of 8.5 inches.

The "-density" setting before the input file name can be important when processing a PDF because it determines the quality of the rest of the processing and the output.

As snibgo mentioned, it's usually a good idea to put a "+repage" after your "-crop" operation.

Re: Convert section of PDF?

Posted: 2016-10-13T20:31:53-07:00
by pctechtv
Wow thank you again @snibgo and @GeeMack super helpful. I have been able to do EXACTLY what I wanted to do. The other great part is your syntax examples are helping understand what everything is in the ImageMagick guides. :)

Re: Convert section of PDF?

Posted: 2016-10-15T17:41:26-07:00
by pctechtv
I'm thinking there must be an option to leave off the last part with the output directory and tell ImageMagick just to create the jpeg in the original file's directory. If so how is this done? Thanks