Convert with resize and resample

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
coneybeare

Convert with resize and resample

Post by coneybeare »

I am creating thumbnails from much larger images and have been using this command successfully for some time:

Code: Select all

 convert FILE -resize "64x" -crop "64x64+0+16" +repage -strip OUTFILE
I also do some other processing that is not relevant to the question. I realized that this does not adjust the resolution at all, so if I use a 300dpi image, it ends up displaying really small on some devices. I want to resample it to 72dpi so I have been trying with this command:

Code: Select all

convert FILE -resize "64x" -crop "64x64+0+16" +repage -strip -resample 72 OUTFILE
And expected the 64x64 image at 300dpi to be resampled to a 64x64 image at 72dpi, but instead, I am getting a very funny size and density.



Here is "identify" output for the original and post-processed file WITHOUT the resample:

Code: Select all

coneybeare $ convert "aa.jpg" -crop "64x64+0+16" +repage -strip "aa.png"
coneybeare $ for image in `find . -type f`; do identify $image; identify -verbose $image | egrep "^  Resolution"; done
./aa.jpg JPEG 1130x1695 1130x1695+0+0 8-bit DirectClass 1.492MiB 0.000u 0:00.000
  Resolution: 300x300
./aa.png PNG 64x64 64x64+0+0 8-bit DirectClass 7.46KiB 0.000u 0:00.000
  Resolution: 118.11x118.11
And here is the "identify output for the command WITH the resample:

Code: Select all

coneybeare $ convert "aa.jpg" -crop "64x64+0+16" +repage -strip -resample 72 "aa.png"
coneybeare $ for image in `find . -type f`; do identify $image; identify -verbose $image | egrep "^  Resolution"; done
./aa.jpg JPEG 1130x1695 1130x1695+0+0 8-bit DirectClass 1.492MiB 0.000u 0:00.000
  Resolution: 300x300
./aa.png PNG 15x15 15x15+0+0 8-bit DirectClass 901b 0.000u 0:00.000
  Resolution: 28.34x28.34
So, the question is: What am I doing wrong and how can I fix it so the end result is a 64x64 cropped thumbnail image at 72dpi?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Convert with resize and resample

Post by el_supremo »

PNG only allows density to be specified in pixels per centimetre (28.34 ppcm = 72ppi). But -resample is the wrong thing to use here because it changes the size of the image whereas all you need is to change the specified density.
Try:

Code: Select all

convert FILE -resize "64x" -crop "64x64+0+16" +repage -strip -units PixelsPerInch -density 72 OUTFILE
IM will convert the specified 72dpi to ppcm.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
coneybeare

Re: Convert with resize and resample

Post by coneybeare »

I didn't realize that png's were not 72. In my cmd-line tests this would have made figuring out what was going on much easier. Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert with resize and resample

Post by fmw42 »

Note: if you specify pixelsperinch for a png, IM will automatically convert it to the equivalent pixelspercentimeter

convert -size 10x10 gradient: -resize "64x" -crop "64x64+0+16" +repage -strip \
-units PixelsPerInch -density 72 grad72dpi.png

identify grad72dpi.png
...
Resolution: 28.34x28.34
Units: PixelsPerCentimeter
Post Reply