Setting auto-height/width for converted Jpeg from PDF using GhostScript
Asked Answered
S

3

8

I am using GS to do conversion from PDF to JPEG and following is the command that I use:

gs -sDEVICE=jpeg -dNOPAUSE -dBATCH -g500x300 -dPDFFitPage -sOutputFile=image.jpg image.pdf

In this command as u can see -g500x300 is to set the converted image size (Width x Height).

Is there a way to just set the Width without having to input the Height so it will base on the width to scale the height using its original aspect ratio? I know it can be achieved by using ImageMagick convert where you simply put 0 on the height parameter i.e. -resize 500x0. I tried with GhostScript but I don't think that is the correct way to do it.

I decided not to use ImageMagick convert reason why because it is very slow when it comes to converting a big sized multiple page PDF.

Thanks for the help!

Sufi answered 4/4, 2012 at 4:18 Comment(0)
M
5

This post explains why ghostscript is faster - https://serverfault.com/questions/167573/fast-pdf-to-jpg-conversion-on-linux-wanted, and the only workaround to fix it would involve modifying the imagemagick code.

Unfortunately, autodetermined output size is not supported by ghostscript. This is primarily because the -g option used is actually determining the device size that will hold the rendered output, and not the rendered output itself. That output size is changing because of the -dPDFFitPage switch which then tries to match the device size. And although you can define just the height of the jpeg 'device' using -dDEVICEHEIGHT=n, that will leave the device width at the unchanged default.

Although a somewhat tedious workaround, you can use ghostscript or imagemagick to get the width and height of the pdf page(s). To do this using ghostscript, see the answer to Using GhostScript to get page size. You can then calculate the proper width to set the -g flag to hold the aspect ratio. Bonus points if you can figure out a single set of commands to do all this :)

Musil answered 5/4, 2012 at 3:13 Comment(0)
W
4

You could write a PostScript program to do this readily enough. Here is a start:

%!
% usage: gs -sFile=____.pdf  scale.ps

/File where not {
  (\n   *** Missing source file. \(use -sFile=____.pdf\)\n) =
  Usage
} {
  pop
}ifelse

% Get the width and height of a PDF page
%
/GetSize {
  pdfgetpage currentpagedevice
  1 index get_any_box 
  exch pop dup 2 get exch 3 get
  /PDFHeight exch def
  /PDFWidth exch def
} def


%
% The main loop
% For every page in the original PDF file
%
1 1 PDFPageCount 
{
  /PDFPage exch def
  PDFPage GetSize

% In here, knowing the desired destination size
% calculate a scale factor for the PDF to fit
% either the width or height, or whatever
% The width and height are stored in PDFWidht and PDFHeight
  PDFPage pdfgetpage
  pdfshowpage
} for

pdfgetpage and pdfshowpage are internal Ghostscript extensions to the PostScript language for handling PDF files.

Wed answered 5/4, 2012 at 7:37 Comment(2)
Can you explain in a bit detailed way on how to use this program? Like do I need to make a shell script and run that?Pulpwood
Its PostScript, so you feed it to the PostScript interpreter. TO be precise, you pass it as the input file command line argument to Ghostscript. The comment as the first line of the program even gives you the usage. Note that this is not a complete working implementation.Wed
D
0

To resize image with Ghostscript, use -dDownScaleFactor e.g. gs -dBATCH -dNOPAUSE -r300 -dDownScaleFactor=3 -sDEVICE=png16m -sOutputFile=/tmp/26a0e9f7-3f26-437d-9a97-1653074e819a_%d.png,/tmp/temp.pdf

  • -r300 here will produce a huge image

  • I can drop the size by scaling down by 3, aspect ratio maintained.

You can use this if it is not important to set an exact width dimension. Which works for most use cases.

Daguerreotype answered 20/9, 2021 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.