Sukumar
Sukumar

Reputation: 31

JPEG to PNG conversion with 300 DPI

Unable to convert a JPEG image into a 300 DPI PNG image using ImageMagick.

After conversion the PNG image is 72 DPI only. I'm using ImageMagick 6.9.0-0 Q16 x86 and Ghostscript v9.15.

Below is the line I use in my Perl script:

 system("\"$imagemagick\" -set units PixelsPerInch -density 300 \"$jpg\" \"$png\"");

Upvotes: 2

Views: 1299

Answers (2)

G. Cito
G. Cito

Reputation: 6388

I'm wondering where the 72dpi is coming from. Assuming you are using X and some kind of Unix, ImageMagick defaults to using the screen resolution (72 dpi). I'm not sure what it does under OSX/XQuartz but it's likely similar. Is your screen resolution set to 72dpi (!?).

I'm with @emcconville @ikegami - just do this straight from ImageMagick on the commandline - passing the right options to be sure.

There are image manipulation modules that you can use from perl without having to resort to system commands as well such as Imager::Transformations, Image::Magick, and GD. Here's how to convert with GD.

perl -MGD -E 'my $imgjpg = GD::Image->newFromJpeg("img.jpg"); 
open my $imgpng, ">", "img.png" or die; print $imgpng $imgjpg->png();'

With most image manipulation packages the original resolution show be maintained during conversion - though some (including GD) will default to lower color depths (8 bit) unless passed a Truecolor flag.

e.g. GD::Image->newFromJpeg("img.jpg", 1);

Upvotes: 0

emcconville
emcconville

Reputation: 24439

Adjusting the units & density will not alter the underlining image data, but updates meta info for rendering libraries. Important for vector to raster, but not very useful for raster to raster. To adjust the DPI of an image, use the -resample operation.

convert source.jpg -resample 300 out.png

You verify the DPI resolution with the following...

identify -format "%[resolution.x] %[resolution.y]\n" out.png

Upvotes: 1

Related Questions