Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
My last solution is used Photoshop patameters and Photoshop factor, I'm happy ^-)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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'));
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
@Andy_Bat1,
Did you found the best solution for you?
Copy link to clipboard
Copied
I'm stopped on my self solution
Copy link to clipboard
Copied
Sorry, I was not trying to say your variant does not work. I was just pointing out how to use less code and take advantage of the UnitValue method.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now