Using a formula for the value of -density

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
elmimmo
Posts: 26
Joined: 2011-02-02T05:42:42-07:00
Authentication code: 8675308

Using a formula for the value of -density

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using a formula for the value of -density

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Using a formula for the value of -density

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply