Discussion:
convert PDF to PNG with given pixel size
Sven Köhler
2011-02-10 11:51:38 UTC
Permalink
Hi,

first, here's what I'm not looking for:

convert [-density XYZ] some.pdf -resize 250x250 some.png

This is "suboptimal" because the PDF is rendered at some density chosen
by imagemagick (or at the give one, which some people on the net just
seem to chose randomly), and then scaled to the right pixel size
afterwards. In particular, this gives bad looking results if the PDF
contains very thin lines (which tend to disappear when you scale down to
250x250 from large pixel images) or if you want to render the PDF at a
very high resolution (in which case imagemagick internally scales up a
pixel image rendered at the ).

It would be much cleverer, to render the PDF at the right density which
yields an image with the desired pixel size. Manually, I figured out
that the desired density should be 56.8. So I used:

convert -density 56.8x56.8 some.pdf some.png

However, it kind of sucks that you have to perform a manual computation
before rendering a PDF. Does imagemagick have a better way of doing
this, in particular without a manual compuation? Can I render a PDF at a
given pixel size, instead of a given density?

Some googling revealed nothing.


Regards,
Sven
Wolfgang Hugemann
2011-02-10 17:00:50 UTC
Permalink
You could do something along the lines of http://www.imagemagick.org/Usage/windows/#calc_fx, i.e. first Identify the dimensions of the PDF (using a standard resolution), then calculate the adequate resolution based on the result of this and than hand over this result to Convert.

The above link leeds to a Windows example, but the approach is similar (and probably even easier) under other operation systems.

Wolfgang Hugemann
Sven Köhler
2011-02-15 11:21:40 UTC
Permalink
Post by Wolfgang Hugemann
You could do something along the lines of http://www.imagemagick.org/Usage/windows/#calc_fx, i.e. first Identify the dimensions of the PDF (using a standard resolution), then calculate the adequate resolution based on the result of this and than hand over this result to Convert.
The above link leeds to a Windows example, but the approach is similar (and probably even easier) under other operation systems.
Hmm. Not quite sure whether the output of "identify" is ment to be
easily parsable, but that should be doable.

However, it seems a bit strange that imagemagick can't do that out of
the box. I think, I will file an RFE.


Regards,
Sven
Michael Howard
2011-02-15 13:51:56 UTC
Permalink
On Tue, Feb 15, 2011 at 6:21 AM, Sven Köhler <***@gmail.com> wrote:
[snip]
Post by Sven Köhler
Hmm. Not quite sure whether the output of "identify" is ment to be
easily parsable, but that should be doable.
Sven,

The identify command takes a -format parameter that allows you to
select the image attributes that you want to select. For example, to
get only WIDTHxHEIGHT you can use something like:

identify -format '%wx%h\n' myImage.png

Using this you can make something that is more easily parsable.


Michael

Loading...