Page 1 of 1

Convert image to 200 dpi

Posted: 2015-05-08T11:30:37-07:00
by myspacee
Hello,
think this task is simple but can't figure how use switches.

I'm must convert an image and final output must have DPI 200.
This image must not change height and width after this operation.

I can do this (200 DPI) using photoshop/irfanview:
Image

starting from 100 DPI:
Image

Try this without luck, but i understand is not right way:

Code: Select all

convert units PixelsPerInch input.jpg -resample 200 output.jpg
Onother question, -resize can influence output ?

Thank you for any help,
m.

Re: Convert image to 200 dpi

Posted: 2015-05-08T11:41:48-07:00
by snibgo
Perhaps you want "-density XX". This changes the metadata without changing the pixels, so seems to be what you want.

"-resample XX" changes the metadata but also resamples the image, changing the width and height in pixels, so the width and height in inches remains constant.

"-resample" is like "-density" plus "-resize".

See http://www.imagemagick.org/script/comma ... p#resample and http://www.imagemagick.org/script/comma ... hp#density

Re: Convert image to 200 dpi

Posted: 2015-05-08T12:43:14-07:00
by myspacee
Hello,
thank you for reply. I believe my above example is wrong ...

Explain better my task:
- i receive an image from a web upload.
- This image can be very large
- via web i cut image portion and resize to 119x184 px ( 4,2 cm x 6,5 cm ? )

Code: Select all

convert input.jpg -resize 119x184 output.jpg
- after resize i try to 'up' DPI to 200,

Code: Select all

convert output.jpg -density 200 output_200dpi.jpg
Now starting some oddities :
- output.jpg has print size from DPI = 4.2 x 5.8 cm; 1.7 x 2.3 inches
- output_200dpi.jpg has print size from DPI = 1.5 x 2.1 cm; 0.6 x 0.8 inches
- open them with a viewer display same size at video

Is possible to obtain real DPI changes and not change metadata only ?

I must obtain as output an image :
- height 6,5 cm
- width 4,2 cm
- 200 DPI

I'm confused,
m.

Re: Convert image to 200 dpi

Posted: 2015-05-08T13:28:07-07:00
by snibgo
I must obtain as output an image :
- height 6,5 cm
- width 4,2 cm
- 200 DPI
This would be 512x331 pixels. So:

Code: Select all

convert in.png -resize 512x331! -density 200 -units PixelsPerInch out.png

Re: Convert image to 200 dpi

Posted: 2015-05-08T13:39:08-07:00
by fmw42
snibgo wrote:convert in.png -resize 512x331! -density 200 -units PixelsPerInch out.png
If he is using the same image as posted, that has a very different aspect than 512x331 and the input is not large enough to just crop. So the resize with exclamation (!) will distort the image.

I think one should do it as:

Code: Select all

convert in.png -resize 512x331 -gravity center -crop 512x331+0+0 -density 200 -units PixelsPerInch out.png

Re: Convert image to 200 dpi

Posted: 2015-05-08T13:39:40-07:00
by myspacee
Ok,
how convert cm to pixel keeping in mind DPI ?
(why 6,5 Centimeters = 512px | online converter say 6,5 Centimeters = 245,669291339 px)

Find a nice article about : http://www.dallinjones.com/2008/07/how- ... llimeters/
mm = (pixels * 25.4) / dpi
pixels = (mm * dpi) / 25.4

Can i use these to pass parameters to IM ?

I need to write a PHP function to make this conversion for 200 DPI,
m.

Re: Convert image to 200 dpi

Posted: 2015-05-08T13:44:56-07:00
by fmw42
6.5 cm / 2.54 cm per inch * 200 pixels/inch = 512

Your want cm size image, but you are asking for 200 DPI. DPI is pixels per inch, not per cm.

Do you want 200 pixels/cm or 200 pixels/in?

Re: Convert image to 200 dpi

Posted: 2015-05-08T13:51:14-07:00
by fmw42
What Print size do you actually want in inches or centimeters? And 200 dpi or 200 dpc?

We need to know that to compute the proper image size in pixels to resize the image properly according to the print size and whether the 200 is dpi or dpc.

Re: Convert image to 200 dpi

Posted: 2015-05-09T09:20:51-07:00
by myspacee
Hello,
ask to my boss more info and finally i understand that tool need an output, with Print Size (from DPI) 4.2 x 6.5 cm for this request :
- width 4,2 cm
- height 6,5 cm
- 200 DPI

This formula solve the riddle :

Code: Select all

mm = (pixels * 25.4) / dpi
and fmw42 suggest right way to apply pixel obtained values:

Code: Select all

convert in.png -resize 512x331 -gravity center -crop 512x331+0+0 -density 200 -units PixelsPerInch out.png
thank you again for support and lesson about DPI,
m.

Re: Convert image to 200 dpi

Posted: 2015-05-09T11:49:30-07:00
by glennrp
Be aware that PNG stores resolution (density) in metric units only (pixels per meter), so

Code: Select all

convert ... -density 200 -units PIxelsPerInch out.png
identify -verbose out.png
will report 78.74 PixelsPerCM which is 199.9996 DPI. This is close enough for all practical purposes, but has been known to upset some people.