Convert image to 200 dpi

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
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Convert image to 200 dpi

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert image to 200 dpi

Post 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
snibgo's IM pages: im.snibgo.com
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Convert image to 200 dpi

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert image to 200 dpi

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert image to 200 dpi

Post 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
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Convert image to 200 dpi

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

Re: Convert image to 200 dpi

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

Re: Convert image to 200 dpi

Post 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.
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Convert image to 200 dpi

Post 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.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert image to 200 dpi

Post 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.
Post Reply