Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Get Image Size Without Opening File

New Here ,
Apr 27, 2016 Apr 27, 2016

I'd like to get the image size (width x height) of an image file without actually opening it in to memory. Is this possible with scripting? Maybe check the image's metadata or something? The goal is to determine if the image is in landscape or portrait layout. This will be done on a number of images, so opening each file and then checking wouldn't be very efficient.

Thanks!

TOPICS
Actions and scripting
5.2K
Translate
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
Guide ,
Apr 27, 2016 Apr 27, 2016

Yes it can be done, there is a script "Extract Metadata" in the Bridge Scripts section @ http://www.ps-bridge-scripts.talktalk.net/

Translate
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 ,
Apr 27, 2016 Apr 27, 2016

Thanks. This looks like something that runs within Bridge. I'm trying to do this using only Photoshop.

Translate
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
Guide ,
Apr 27, 2016 Apr 27, 2016

Good luck then, I do not think you can do it as the width and height is not in the files metadata but is available in the Bridge cache.

Translate
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
LEGEND ,
Feb 05, 2018 Feb 05, 2018

I just clicked http://www.ps-bridge-scripts.talktalk.net/ in this topic and found that site of Paul Riggottis back Would be fantastic Photoshop scripting collection if site of great scripter @xbytor2 worked again too: CVS Info for project ps-scripts

Translate
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
Community Expert ,
Feb 05, 2018 Feb 05, 2018

And there is also Paul’s GitHub:

GitHub - Paul-Riggott/PS-Scripts: Photoshop Scripts

Translate
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
LEGEND ,
Feb 05, 2018 Feb 05, 2018

That I knew, but not everyone, but what happened his old site is on? What I know he removed it as those old scripts doesn't work with new CC version and people mailed him overmuch to change them they were uptodated. I like that old site more!

Translate
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
Community Expert ,
Apr 27, 2016 Apr 27, 2016
The goal is to determine if the image is in landscape or portrait layout. This will be done on a number of images, so opening each file and then checking wouldn't be very efficient.

Does this have to be scripted? Later versions of Photoshop have conditional actions, and one of these conditions is “If current document is landscape, play action X, else play action Y. This provides basic functionality that would otherwise need to be scripted.

One can use Adobe Bridge to filter images by Orientation, which can also be used with other features such as Batch application of actions/scripts to the orientation filtered images, or perhaps using Collections or Smart Collections.

Then there is also the following script: Siva’s Photoshop Conditional Action ​if you are on an earlier version of Photoshop that does not have conditional actions.

Depending on the file format, width and height dimensions in pixels may be present in metadata (not, sure how valid or robust) however you would then need a method to compare these values to sort by orientation.

Example (File/File Info/Raw Data) for PSD, TIFF, JPEG from a new file in Photoshop:

<exif:PixelXDimension>1024</exif:PixelXDimension>

<exif:PixelYDimension>768</exif:PixelYDimension>

<tiff:ImageWidth>1024</tiff:ImageWidth>

<tiff:ImageLength>768</tiff:ImageLength>

While ExifTool lists the following tags:

[IFD0]      ImageWidth      : 1024

[IFD0]      ImageHeight     : 768

[ExifIFD]   ExifImageWidth  : 1024

[ExifIFD]   ExifImageHeight : 768

[Composite] ImageSize       : 1024x768

Translate
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
Community Expert ,
Apr 27, 2016 Apr 27, 2016

I had a dig around my backup and found the following:

// imageAspectConditional.jsx

// Copyright 2007

// Written by Jeffrey Tranberry

// Photoshop for Geeks Version 1.0

/*

Description:

This script demonstrates how to run an

action based on a condition

*/

// enable double clicking from the

// Macintosh Finder or the Windows Explorer

#target photoshop

// Make Photoshop the frontmost application

// in case we double clicked the file

app.bringToFront();

///////////////////////////

//       SET-UP          //

///////////////////////////

if (app.activeDocument.height > app.activeDocument.width)

  {

  // Run this action if the IF statement is true.

  doAction("My Great Action","Default Actions");

  }

else

  {

  // Run this action if the IF statement is false

  doAction("My Great Action2","Default Actions");

  }

Translate
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
Community Expert ,
Apr 27, 2016 Apr 27, 2016

I believe SuperMeriln is correct. You really need to open the files in PS to get that info. You can do a workaround based on what SuperMerlin said and use Bridgetalk to run a Bridge script to check the orientation before PS opens it.

Translate
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
Apr 29, 2016 Apr 29, 2016

For PSD files it is in the first few bytes of the binary data so it wouldn't be that hard. That is one file format. Photoshop can open many so you have our work cut out for you. Maybe imagemagick? But I doubt it can do all the formats Photoshop can.

I would try to use a Bridge script as it can get you that data and it caches it up.

Translate
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
Advisor ,
Apr 29, 2016 Apr 29, 2016

Here's how I would tackle this particular problem. The getXMPValue function is extracted from xtools/xlib/stdlib.js.

//
// getXMPValue(obj, tag)
//
// Get the XMP value for (tag) from the object (obj).
// obj can be a String, XML, File, or Document object.
//
// Some non-simple metadata fields, such as those with
// Seq structures are not handled, except for ISOSpeedRatings
// which is handled as a special case. Others can be added as needed.
//
// Based on getXMPTagFromXML from Adobe's StackSupport.jsx
//
// Examples:
// getXMPValue(xmlStr, "ModifyDate")
// getXMPValue(app.activeDocument, "ModifyDate")
// getXMPValue(xmlObj, "ModifyDate")
// getXMPValue(File("~/Desktop/test.jpg"), "ModifyDate")
//
function getXMPValue(obj, tag) {
  var xmp = "";

  if (obj == undefined) {
    Error.runtimeError(2, "obj");
  }

  if (tag == undefined) {
    Error.runtimeError(2, "tag");
  }

  if (obj.constructor == String) {
    xmp = new XML(obj);

  } else if (obj.typename == "Document") {
    xmp = new XML(obj.xmpMetadata.rawData);

  } else if (obj instanceof XML) {
    xmp = obj;

  } else if (obj instanceof File) {
    if (!ExternalObject.AdobeXMPScript) {
      ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
    }
    // Stdlib.loadXMPScript();

    if (tag == "CreateDate") {
      var cstr = obj.created.toISODateString();
      var mstr = Stdlib.getXMPValue(obj, "ModifyDate");
      return cstr += mstr.slice(mstr.length-6);
    }

    // add other exceptions here as needed

    var fstr = decodeURI(obj.fsName);
    var xmpFile = undefined;

    try {
      xmpFile = new XMPFile(fstr, XMPConst.UNKNOWN,
                            XMPConst.OPEN_FOR_READ);
    } catch (e) {
      try {
        xmpFile = new XMPFile(fstr, XMPConst.UNKNOWN,
                              XMPConst.OPEN_USE_PACKET_SCANNING);
      } catch (e) {
        Error.runtimeError(19, "obj");
      }
    }

    var xmpMeta = xmpFile.getXMP();
    var str = xmpMeta.serialize()
    xmp = new XML(str);
    xmpFile.closeFile();

  } else {
    Error.runtimeError(19, "obj");
  }

var s;

// Handle special cases here
if (tag == "ISOSpeedRatings") {
  s = String(eval("xmp.*::RDF.*::Description.*::ISOSpeedRatings.*::Seq.*::li"));

  } else {
    // Handle typical non-complex fields
  s = String(eval("xmp.*::RDF.*::Description.*::" + tag));
  }

  return s;
};

var file = File("~/Desktop/test.png");

var x = getXMPValue(file, "PixelXDimension");

var y = getXMPValue(file, "PixelYDimension");

Translate
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
Participant ,
Apr 30, 2016 Apr 30, 2016

Width and height of an image are in the Exifs of an image, when the image format has EXFs, of course.

You can extract these infos with a short program like this, but you must open the document first. May be other issues :

#target photoshop

app.bringToFront();

var exifsNumber = app.activeDocument.info.exif.length;

for (var i = 0; i < exifsNumber; i++)

{

    if (app.activeDocument.info.exif[2]==256) {var imgWidth = app.activeDocument.info.exif[1]};

    if (app.activeDocument.info.exif[2]==257) {var imgHeight = app.activeDocument.info.exif[1]};

}

alert (imgWidth + " - " + imgHeight);

Sorry, My text was not complete. It is not interesting to work with the EXIFs, as you must open your document first. You have the same infos with activeDocument width and height properties. Is there a serious reason not to open and close immediately your docs ?

Translate
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
LEGEND ,
Feb 05, 2018 Feb 05, 2018

If you want to do it with .jpg's and you're not sure they were rotated in Windows browser, beside checking width & height of images it is worth of checking their orientation. For example if you have Portrait / Landscape image and you check by script (before opening in Photoshop) its width & height, and then basing only on these data open by other script this image to do some action on, it may not be appriopate to image width & height seen in Ps, as Portrait may be opened as Landspace and et cetera. That situation occurs only when .jpg was rotated in Windows but not opened in Photoshop yet to be saved with chnaged orientation. More about this (script with description) you find in this topic: Re: Rotate a layer through a script?

Translate
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
People's Champ ,
Feb 05, 2018 Feb 05, 2018

ttry this

$.localize = true;

var g_StackScriptFolderPath = app.path + "/"+ localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts") + "/"

                                        + localize("$$$/private/LoadStack/StackScriptOnly=Stack Scripts Only/");

$.evalFile(g_StackScriptFolderPath + "StackSupport.jsx");

var doc = openViewlessDocument("C:\\123\\111.jpg");

alert(doc.width + " " + doc.height)

doc.close();

Translate
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
LEGEND ,
Feb 05, 2018 Feb 05, 2018
LATEST

Very interesting. I knew there are Terminology and StackSupport files, but never had time to browse them to see this all

Translate
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