Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Having trouble extracting the pixel dimensions of an image link in Indesign

New Here ,
Feb 27, 2020 Feb 27, 2020

Copy link to clipboard

Copied

I'm trying to write a script that, given an InDesign file, is able to compute the image dimensions of every image in the document. I need the original image dimensions, so the image link I believe. The problem is when I try to pull out tiff:ImageHeight and tiff:ImageWeight from the linkXmp, it never returns a value. Is there some easier way to do this without having to look into the image metadata?

TOPICS
How to, Scripting

Views

490

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Feb 28, 2020 Feb 28, 2020
Are you trying to get the actual (unscaled) pixel dimensions? An image has the effective ppi resolution property, so if the document units are set to inches, you can get the pixel dimensions. This gets the actual pixel (or inch) dimensions of a selected image: var doc = app.activeDocument; doc.viewPreferences.properties = {horizontalMeasurementUnits:MeasurementUnits.INCHES, verticalMeasurementUnits:MeasurementUnits.INCHES} //a selected image var sel = doc.selection[0]; //the actual pixel d...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 27, 2020 Feb 27, 2020

Copy link to clipboard

Copied

Hi Peter,

 

There seems to be no direct property that provides the actual dimensions of the placed image, also reading the xmp would work only if you can make sure that the data is added into it correctly in the first place. I tried to put together a few lines of code that should do the trick, the idea is to clear any transformations applied to the image, use visual bounds property to calculate the height and width and then reverting the tranformation clear operation.

var image = app.documents[0].links[0].parent
image.clearTransformations()
var width = image.visibleBounds[3] - image.visibleBounds[1]
var height = image.visibleBounds[2] - image.visibleBounds[0]
app.documents[0].undo()

I have used just the first link of the document, you can add in the logic to loop ovver every link

 

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 28, 2020 Feb 28, 2020

Copy link to clipboard

Copied

Hi Manan,

 

I tried this previously but did not get a return in pixels. I essentially need the same width x height numbers as the linked file. Is there a way to convert this?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 28, 2020 Feb 28, 2020

Copy link to clipboard

Copied

Are you trying to get the actual (unscaled) pixel dimensions? An image has the effective ppi resolution property, so if the document units are set to inches, you can get the pixel dimensions. This gets the actual pixel (or inch) dimensions of a selected image:

 

 

var doc = app.activeDocument;
doc.viewPreferences.properties = {horizontalMeasurementUnits:MeasurementUnits.INCHES, verticalMeasurementUnits:MeasurementUnits.INCHES}

//a selected image
var sel = doc.selection[0];

//the actual pixel dimensions
$.writeln("Pixel Dimensions: " + getPixels(sel))
//returns Pixel Dimensions: 1920,1280

//the actual dimension in inches
var appi = sel.actualPpi
var aw = getPixels(sel)[0]/appi[0];
var ah = getPixels(sel)[1]/appi[1];
$.writeln("Actual Inch Dimensions: " + aw + " x " + ah)

/**
* returns the pixel dimensions of an image
* @param i an image 
*/
function getPixels(i){

    var r = i.effectivePpi;
    var w = i.geometricBounds[3] - i.geometricBounds[1];
    var h = i.geometricBounds[2] - i.geometricBounds[0];
    var hpd = Math.round(w * (r[0]));
    var vpd = Math.round(h * (r[1]));

    return [hpd, vpd];
}

 

 

 

 

 

 

 

 

 

 

Screen Shot 11.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 28, 2020 Feb 28, 2020

Copy link to clipboard

Copied

This worked perfectly, thank you so much!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 29, 2020 Feb 29, 2020

Copy link to clipboard

Copied

Hi Peter,

 

If the result is needed in pixels you can easily set the mesurementunits of scriptpreferences to the desired unit. See the modified script that will get the height and width in pixels

app.scriptPreferences.measurementUnit = MeasurementUnits.PIXELS
var image = app.documents[0].links[0].parent
image.clearTransformations()
var width = image.visibleBounds[3] - image.visibleBounds[1]
var height = image.visibleBounds[2] - image.visibleBounds[0]
app.documents[0].undo()

 

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 29, 2020 Feb 29, 2020

Copy link to clipboard

Copied

Hi Manan, I think Peter is looking for the image’s real or actual pixel dimensions.

 

InDesign’s Pixel ruler unit will measure an object in pixels, but InDesign defines the Pixel unit as a static 1/72". So here the Link panel shows the image’s actual pixel dimensions as 1920 x 1280, but InDesign’s transform panel measures the placed image as 345.6px x 230.4px:

 

Screen Shot.png

 

When the rulers are set as inches the image’s width is 4.8", and 4.8*72=345.6.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 01, 2020 Mar 01, 2020

Copy link to clipboard

Copied

Hi Rob,

 

Yes i also was working to get the actual dimension of the image which you highlighted in your screenshot. While i have used an algorithm similiar to the one you posted in one of my projects but the new algo that i wrote also seems to work correctly. See the video below, looking at your screenshot i am not sure why it is not working for you. Can you see anything that i am doing wrong?

https://www.dropbox.com/s/1x14m4yad17684d/screen%20recording%202020-03-02%20at%2010.57.38%20am.mov?d...

 

-Manan

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

It's because the Effective resolution of your placed image is 72ppi, which happens to match InDesign’s 1/72" definition of a pixel dimension. I think if you try your script on a placed image that does not have an Actual Res of 72ppi, you will not get the image’s original pixel dimensions. The Effective resolution in my first example is 457ppi x 529ppi because of the scaling.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

When I trace your width and height on my image with an Actual Resolution of 400ppi, I get 345.6 x 230.4, and not the 1920 x 1280 pixel dimensions listed in Link Info

 

 

$.writeln("Dimensions: " + width + " x " + height) 
//Returns Dimensions: 345.6 x 230.4

 

 

 

Screen Shot 4.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

LATEST

 

 

 

// Select an image (NOT its frame) then run script.
var image = app.activeDocument.selection[0];
var hs = image.horizontalScale;
var vs = image.verticalScale;
var ppi = image.actualPpi;
var gb = image.geometricBounds;
var pixelsWide = Math.round((gb[3] - gb[1]) / (hs / 100) * ppi[0]);
var pixelsHigh = Math.round((gb[2] - gb[0]) / (vs / 100) * ppi[1]);
alert([pixelsWide, pixelsHigh]);

 

 

Some notes: flopped images will give negative results. Use Math.abs() on the results.

Also doesn't work for rotated images. For that, duplicate and clear rotation, take the measurment, then remove the duplicate.

 

I ran into this when wanting to add "pixel dimensions" to my script "Links Report." (https://www.marspremedia.com/software/indesign/links-report)

Not only rotated, but sheared throws it off, too, of course. I'll post here the function I came up with to handle both. For these cases I opted to make a new temp doc instead of duplicate the image in the current doc. I did this because I didn't want the dupe and clear showing in the undo menu of the target document. Here's my function...

 

function getPixelDimensions(image) {
    var gb; //  geometricBounds
    var h; //   height
    var hs; //  horizontalScale
    var ppi; // pixels per inch
    var td; //  temp document
    var tr; //  temp rectangle
    var tg; //  temp graphic
    var vs; //  verticalScale
    var w; //   width

    ppi = image.actualPpi;
    hs = image.horizontalScale;
    vs = image.verticalScale;
    if (image.rotationAngle !== 0 || image.shearAngle !== 0) {
        // Won't be correct if rotated or sheared.
        // Copy to a temp doc and clear rotation and shear.
        // Then measure, and dump the temp doc.
        td = app.documents.add();
        tr = image.parent.duplicate(td.pages[0]);
        tg = tr.graphics[0];
        tg.rotationAngle = 0;
        tg.shearAngle = 0;
        gb = tg.geometricBounds;
        td.close(SaveOptions.NO);
    } else {
        gb = image.geometricBounds;
    }
    w = Math.abs(Math.round((gb[3] - gb[1]) / (hs / 100) * ppi[0]));
    h = Math.abs(Math.round((gb[2] - gb[0]) / (vs / 100) * ppi[1]));
    return [w, h];
    // Returns array of two numbers, width and height.
    // Width = array[0], height = array[1]
}

 

William Campbell
Mars Premedia

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines