Page 1 of 1

Using a formula for the value of -density

Posted: 2011-02-02T07:32:41-07:00
by elmimmo
Is there any way to give -density a formula instead of a fixed value?

I am trying to convert a PDF to JPEG by rendering with at 2x the intended output density, downsampling to get antialiased lines, and finally assigning the output JPG proper density (ppi) metadata so that its physical size in inches is the same as the original PDF (even if ppi is different).

Taking into accound Imagemagick considers all PDFs to be 72 ppi, consider this example:
  • The input PDF is 10x10 inches, i.e. 720x720px at 72 ppi
  • The output JPG should be 1440x1440px at 144ppi

Code: Select all

convert -density 288x -geometry 1440x input.pdf -density 144x output.jpg
The input file is first rendered with a density (288) twice the intended output density for better antialiasing. Output density (144) is then added to the output file to preserve physical size in inches:

Code: Select all

144 = output size in px / (input width in px / original dpi) = 1440 / (720 / 72)
Hence, if using a formula, I was thinking of something like:

Code: Select all

convert -density "%[fx:round(2*(1440/(w/resolution.x)))"x -geometry 1440x input.pdf -density "%[fx:round((1440/(w/resolution.x)))"x output.jpg
But apparently -density does not accept that kind of fx formulas. Any way to do it then?

Re: Using a formula for the value of -density

Posted: 2011-02-02T10:54:24-07:00
by fmw42
compute it in one command and save as variable, then use in the next command

densityval=`convert image -format "%[fx:round(2*(1440/(w/resolution.x)))" info:`
convert -density ${densityval}x -geometry 1440x input.pdf -density ${densityval}x output.jpg

Re: Using a formula for the value of -density

Posted: 2011-02-02T17:13:15-07:00
by anthony
This is something that is desperately wanted. But difficult to implement.

Here is one proposal, but something more radical may be needed for IM v7

http://www.imagemagick.org/Usage/bugs/future/#settings