Skip to main content
Inspiring
July 22, 2013
Question

how to convert pixels in mm ?

  • July 22, 2013
  • 4 replies
  • 51110 views

Hello everybody!

I'm making a tool in which the user specifies the size of the frame, I want to give him the opportunity to choose between units of measurement. How do I convert one unit to another? Centimeters to millimeters as I know 🙂

The calculations in my instrument produced in pixels. A user wants to place on a sheet of paper photos of a certain size in order to then cut them after printing and pasted into the frame.

This part of the script produces the  selection of the pixel size, but calculation was conducted in millimeters.

app.preferences.rulerUnits = Units.MM;



var newSelect = Array(

                   Array(Left, Top),

                   Array(Right, Top),

                   Array(Right, Bottom),

                   Array(Left, Bottom),

                   Array(Left, Top)

);

doc.selection.select(newSelect);

Came up with a solution, I do not know how much it is optimal - use factor

app.preferences.rulerUnits = Units.MM;

rmm = doc.width;

app.preferences.rulerUnits = Units.PIXELS;

rpix = doc.width;

var propPixMm = rpix.value/rmm.value;

4 replies

AngelOfLight-Australia
Participant
February 16, 2016

How to Convert from Pixels to Millimeters from How to convert from pixels to millimeters « DallinJones.com

In chemistry, we studied something called dimensional analysis. Dimensional analysis is a method for converting from one unit to another. So first off we multiply the number of pixels by 1 over our DPI (in this case 300 DPI) then we multiply that by 25.4 by one inch (number of millimeters in an inch) Once everything cancels out we are left with a fairly nice, simple and easy to use formula.

mm = (pixels * 25.4) / dpi

In order to convert back from millimeters to pixels we simply reverse the formula using simple algebra.

pixels = (mm * dpi) / 25.4

There you have it. A very simple way to convert from pixels to millimeters. And then from millimeters back to pixels.

Inspiring
July 22, 2013

When using UnitValue objects you can have it convert between unit types.

app.preferences.rulerUnits = Units.MM;// or any other unit except percent

var rmm = doc.width;

alert(rmm.as('px'));

Andy_Bat1Author
Inspiring
July 22, 2013

my variant (all correct):

#target photoshop

var doc = app.activeDocument ;

app.preferences.rulerUnits = Units.MM;

rmmW = doc.width.value;

app.preferences.rulerUnits = Units.PIXELS;

rpixW = doc.width.value;

var PixMm = rpixW/rmmW;

alert(rmmW +"mm / "+rpixW +"pix\n factor " + PixMm);

pixxxelschubser
Community Expert
Community Expert
July 22, 2013

var doc = app.activeDocument;

app.preferences.rulerUnits = Units.MM;

rmmW = doc.width.value;

app.preferences.rulerUnits = Units.PIXELS;

rpixW = doc.width.value;

var PixMm = rpixW/rmmW;

// :-)

var WidthMM= rpixW/doc.resolution*25.4;

alert(rmmW +"mm / "+rpixW +"pix\n factor " + PixMm);

// :-)

alert(doc.resolution+"\n"+WidthMM+" mm");

Known Participant
July 22, 2013

Resolution is what connects pixels with real world. Printing standard of resolution is 300ppi, that means the 'density' of pixels is 300 pixels per inch.

gener7
Community Expert
Community Expert
July 22, 2013

Pixels are not physical units of measurement. They are computer data units that define a raster image.

You can have 1000 pixels and decide whether they print to 1 mm, 1 cm, or 1 meter in length. It will still be 1000 pixels.

The best way to see this is to open a file, open the Image Size box and turn resampling off.

Change the physical size and you will see the amount of pixels remain the same.

Andy_Bat1Author
Inspiring
July 22, 2013

My last solution is used Photoshop patameters and Photoshop factor, I'm happy ^-)