views:

1356

answers:

4

I have an requirement that asks for an image with 10 X 6,88 cm. I know that I can't simple convert from cm to pixels, cause one pixel size depends on the user display resolution. I would like to know if there is a way to resize an image to have that size in cm. (I need to keep the image extension also. e.g.: can't convert it to a pdf or other extension)

+2  A: 

It really depends on in which resolution the user will print the image (sizes in cm makes little sense other than when printed). If the user wants to make a print in, say 200 dpi, then the image would need to be (10 / 2.54 * 200) by (6.88 / 2.54 * 200) pixels (the division with 2.54 is needed to convert between cm and inches). Which resolution that is needed is highly dependent on what kind of image it is, and the quality requirements of the user.

So just saying "I want to resize to X by Y cm" does not really make sense.

For a code sample on how to make the actual resize once you have figured out the needed size of the image, this SO answer should cover your needs.

Fredrik Mörk
A: 

Kind of what Fredrik is saying: I would take a nice DPI and require the image to be that resolution or bigger (but is the same aspect ratio) and when exporting/printing the image, resize the image to the DPI used by the other program/printer...

Zenuka
A: 

It might be as simple as this: most images store the number of pixels per inch in them. Figure out the number of pixels in each dimension of your image, and divide that by the number of inches (convert from cm). Then use the original bits, just modify the field for the number of pixels per inch (or, more commonly, dots per inch).

So your picture needs to be 3.93" x 2.71". If your image is 393px x 271px, you would set the dpi to 100x100. If your image is 39px x 27px, you would set the dpi to 10x10.

Though probably you'll have to do some resizing, as explained by other answers. :)

Anthony Mills
A: 

Image file formats like JPG and TIFF have an EXIF header which has information like horizontal and vertical DPI.

Thus if you get an image that has this metadata, you could verify the printable size.

double DPC = Image_DPI * 0.393700787;

double widthInCm = Image_Width * DPC;
double heightInCm = Image_Height * DPC;

if (widthInCm <= 10 && heightInCm <= 6.88) // do stuff

If you need to resize images to never exceed these printable dimensions, you could do it the other way around, and calculate a DPI ratio that lets the image of dimensions W x H fit within 10cm x 6.88cm bounds.

Yannick M.