Pixel Measure – Photoshop Script

Over the last couple of days I’ve been developing a few templates on Photoshop to help assist with the guitar designing process. One of the factors I needed to have was the ability to quickly add dimension labels to the drawings. Whilst Photoshop CS5.1 has a great measurement tool, it doesn’t display labels on the design.

Pixel Measure

So, through a variety of Google searches, I found Pixel Measure; a little script that can be added to Photoshop to create dimension labels.

Luckily, someone had already ported it to GitHub Gist, and so I made a copy to develop it a little more.

View the modified Pixel Measure

Development

The original script outputs the dimension in pixels using the foreground colour.

What I wanted to be able to do is to define what the dimension measurement should be. By default, the script outputs the dimension in pixels.

But what if I need to show the dimensions in inches, or millimeters? Surely this could be set by using the unit set on the rulers in Photoshop.

Within the first few lines of code is the description of my addition.

```//=====================================================
// by @alingham
// Adding Measurement options into the script. Still produces the dimension line in pixels, but changes the label to be in
// Inches or Millimeters or Pixels (default) depending on the original unit of measurement set on the ruler in Photoshop.
//=====================================================```

The Code

The code below is what makes the magic. Essentially it runs a little bit of maths using the document resolution. We take the width or height of the selected area in pixels, and divide it by the resolution of the document (usually 72, but does vary, so preferable to use the value set by the document). The resolution is given as DPI, or Dots Per Inch. The dots (or pixels) of the length we want measured is then divided as per the DPI, giving us the number of inches. We round that value to 2 decimal places.

To work out Millimeters, we just need to multiply this value by 25.4; the number of millimeters in an inch.

Line 124 onwards shows the added code:

```//Get Measurement from Ruler, and Resolution from Document
if (document === undefined) {
var document = app.activeDocument;
}

var resolution = document.resolution;
if (originalUnit == "Units.INCHES") {
var dimensionwidth = width/resolution;
var dimensionheight = height/resolution;
var unit = "in";
}
else if (originalUnit == "Units.MM") {
var dimensionwidth = (width/resolution)*25.4;
var dimensionheight = (height/resolution)*25.4;
var unit = "mm";
}
else if (originalUnit == "Units.CM") {
var dimensionwidth = (width/resolution)*2.54;
var dimensionheight = (height/resolution)*2.54;
var unit = "cm";
}
else {
//default pixels (PX)
var dimensionwidth = width;
var dimensionheight = height;
var unit = "px";
}
if(horizontal)
{
textItemRef.contents = dimensionwidth.toFixed(2) + " " + unit;
textItemRef.justification = Justification.CENTER;
textItemRef.position = Array(Math.floor(x1 + (width/2)),y1+21);
}
else
{
textItemRef.contents = dimensionheight.toFixed(2) + " " + unit;
textItemRef.position = Array(x1+15,Math.floor(y1 + 4 + (height/2)));
}```
SHARE THE LOVE:

You can use the spacebar or the arrow keys to navigate through my site. I hope you enjoy your journey.