Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Yes it can be done, there is a script "Extract Metadata" in the Bridge Scripts section @ http://www.ps-bridge-scripts.talktalk.net/
Copy link to clipboard
Copied
Thanks. This looks like something that runs within Bridge. I'm trying to do this using only Photoshop.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I just clicked http://www.ps-bridge-scripts.talktalk.net/ in this topic and found that site of Paul Riggott​ is back Would be fantastic Photoshop scripting collection if site of great scripter @xbytor2 worked again too: CVS Info for project ps-scripts
Copy link to clipboard
Copied
And there is also Paul’s GitHub:
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
Very interesting. I knew there are Terminology and StackSupport files, but never had time to browse them to see this all