Skip to main content
Inspiring
December 22, 2011
Answered

Identify the orientation of the image

  • December 22, 2011
  • 3 replies
  • 7407 views

Hi

I have a folder with hundreds of images...some in vertical (portrait) and some in horizontal orientation (landscape). I want to standardize all to vertical !!!

So after opening manually the image (or by using the Batch command) I´d need a script that does it:

1) Look at the current active image and if the image is in horizontal orientation (landscape) then rotate it 90º to become vertical (portrait)

2) If the image is already vertical then do not do anything. Leave as it.

End.

It´s just a script to identify which images are horizontal and rotate it.

The process of opening images and saving I can do manually or by using the batch Photoshop command (so I attach the script with an normal action). These taks do not need to be in the script.

Could anyone help me to write this script?

Thank you a lot

Gustavo.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

// 2011, use at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

if (myDocument.width > myDocument.height) {

          myDocument.rotateCanvas(90)

          };

};

3 replies

Participant
June 7, 2022

I need to do something similar, but for all images in a directory. I just want to either renal the image with 'portrait or landscape' in the title (p or l would do or better still save the image with it's dimentions in the name to a directory for landscape and a directory for portrait.
Any pointers would be very helpful as I've never written a script for Photoshop

 

Stephen Marsh
Community Expert
Community Expert
June 7, 2022

@pete24761500hl6l – There are different methods, however, I think that the easiest way is to use Adobe Bridge's Filter panel > Orientation. Select untick landscape and or square and only have portrait ticked, then select all and use Tools > Batch Rename. Then repeat for landscape and or square.

 

 

Another method would be ExifTool, for me, this is much faster than creating a script for Photoshop. The following is a conditional to rename to Portrait, Landscape or Square:

 

exiftool -if '$imagewidth > $imageheight' '-filename=%f_Landscape.%e' -execute -if '$imagewidth < $imageheight' '-filename=%f_Portrait.%e' -execute -if '$imagewidth == $imageheight' '-filename=%f_Square.%e' -common_args 'mac os system path/to/files or folder'

 

This example is for the Mac, Windows would swap the single straight quotes ' for double straight quotes " and obviously use a valid Windows path to the files or directory. 

 

You can also use Bridge's Batch Rename to rename with the width and height metadata:

 

 

Stephen Marsh
Community Expert
Community Expert
June 7, 2022

@pete24761500hl6l – Oops did you mean title as in metadata, for some reason I thought filename when I read title?!

 

Here is the Bridge method using the filter panel and the Metadata panel for all selected/filtered files:

 

 

And here is the ExifTool code:

 

exiftool -if '$imagewidth > $imageheight' -XMP-dc:Title='Landscape' -IPTC:ObjectName='Landscape' -execute -if '$imagewidth < $imageheight' -XMP-dc:Title='Portrait' -IPTC:ObjectName='Portrait' -execute -if '$imagewidth == $imageheight' -XMP-dc:Title='Square' -IPTC:ObjectName='Square' -common_args -overwrite_original 'mac os system path/to/files or folder'
JJMack
Community Expert
Community Expert
December 23, 2011

You can also use scripts in actions.  Sometimes you may rotate an image to one landscape if its not then do some work and then rotate it back after doing the work.  I have written a script that can do that. It what I call a run twice script.  The first time you run it on a document it will rotate Portrait images and when it is run a secont time within the action it will rotate document it rotated back to portrait.

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

//

// Copyright 2002-2003. Adobe Systems, Incorporated. All rights reserved.

// This scripts demonstrates how to rotate a layer 90 degrees clockwise.

// Original file came from PSCS scripting\samples\javascript\RotateLayer.js

//

// Variation Copyright(c)Douglas Cody, 2004, All Rights Reserved.

// http://www.clikphoto.com

//

// Updataed John J McAssey 2008 - 2009 http://mouseprints.net

//

// This script is designed to be used by a Photoshop Action twice

// A good pratice to use when creating an actions that use this scipt is for the action

// not to do a save or play some other action between its two useages of this Script.

//

// This script will look at the document orientation (portrait vs landscape)

// On the first execution, if the document is a portrait, it will be rotated

// to a horizontal.

// On the second execution, a rotated document will be

// restored to a vertical. This effectively toggles the orientation ONLY if

// the original document started out as a portrait.

//

// NOTE: Meta-data Info Instructions field is modified to hold an interim state.

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

/*

<javascriptresource>

<about>$$$/JavaScripts/orient/About=JJMack's Orient^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^rFirst Run records orintation and rotate Protrait to Landscape^rSecond Run removes orintation recorded and rotates Portrats back.</about>

<category>JJMack's Action Run Twice Utility</category>

</javascriptresource>

*/

if (app.documents.length > 0) {

        var orintation = '';

          if (app.activeDocument.info.instructions.indexOf("<orient>") == -1 ) { // No Footprint

                    //alert("first")

                    var orig_ruler_units = app.preferences.rulerUnits;          // Save ruler units

                    app.preferences.rulerUnits = Units.PIXELS;                    // Set ruler units to PIXELS

                    // Add Foot Print to  metadata info instructions and rorate protrait documents

                    // alert( " Width = " + app.activeDocument.width + " Height = " + app.activeDocument.height );

                    if (app.activeDocument.width < app.activeDocument.height) { // portrait 

                              app.activeDocument.rotateCanvas(-90.0);

                              app.activeDocument.info.instructions = app.activeDocument.info.instructions += "<orient>portrait</orient>";

                    }

                    else { app.activeDocument.info.instructions += "<orient>landscape or square</orient>"; } // not portrait

                    // Reset units to original settings

                    app.preferences.rulerUnits = orig_ruler_units;                    // Restore ruler units

          }

          else {

                    //alert("second")

                    // Retreive saved orintation and rotate portrait back up

                    orientOffset = app.activeDocument.info.instructions.indexOf("<orient>") + "<orient>".length;

                    orientLength = app.activeDocument.info.instructions.indexOf("</orient>") -orientOffset;

                    orintation = app.activeDocument.info.instructions.substr(orientOffset, orientLength);

                    if ( orintation == "portrait" ) { app.activeDocument.rotateCanvas(90.0); }

                    // Remove footprint from metadata info instructions

                    before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexOf("<orient>"));

                    afterOffset = app.activeDocument.info.instructions.indexOf("</orient>") + "</orient>".length;

                    after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);

                    app.activeDocument.info.instructions = before + after;

          }

}

else { alert("You must have at least one open document to run this script!"); }

There are 12 script I have written to be used in Actions in my

Crafting Actions Package

  • Action Actions Palette Tips.txt
  • Action Creation Guidelines.txt
  • Action Dealing with Image Size.txt
  • Action Enhanced via Scripted Photoshop Functions.txt
  • CraftedActions.atn Sample Action set includes an example Watermarking action
  • Sample Actions.txt Photoshop CraftedActions set saved as a text file.
  • 12 Scripts for actions

Download



JJMack
Inspiring
December 23, 2011

Hi JJMark

Wow thank you a lot for the immense help. It will be very useful...

Let me ask one more thing? Is it possible to write this kind of script:

I´m needing a script that does it:

If the image is horizontal (landscape) then run the action called "Action X" that is in the set "My folder" in the Actions panel.

If the image is vertical (portrait) then run the action called "Action Y" that´s in the set "My folder" in the Actions panel.

End.

Sure "Action X", "Action Y" and "My folder" are variables that I´ll change in the script.

I want it because I want to run a Batch Photoshop command to a folder with lot´s of images. So I record this script in an base action for the batch. And then batch can run different actions based on the orientation of the document.

Thank you very much.

Gustavo.

JJMack
Community Expert
Community Expert
December 24, 2011

Like Actions can use scripts,  scripts can use actions.  For example the Image Processor script let you specify scripts to include in its processing.  Scripts may or may not have dialogs. Scripts can also be Photoshop plug-in like Fit Image.  A Plug-in script can record variables into action when the action is being recorded and the plug-in is used.  Action are easy to record and Photoshop Plug-in Scriptlistner can record script code but like Actions this script code just step step step (Action Manager code) no logic, however logic can be added and functions can be made from the recorded script code.  Both Scripts an Actions have limits neither can automate thing like brush strokes.

I can not type or spell and don't know javascript or object programming.  I'm a fearless hacker.

Though Scripting is more power then Action I use actions more because recording them is so easy to do.  However unlike most Action creators I have a programming background and carefully craft most of my actions.  Most who record actions do not and they have no problem with their actions for they created them for their work-flow, know how the work and when to use their actions.  These action may not work well for others for they may have a different kind of work-flow they don't know how the action works and don't know when to use them.  Many actions have built in dependencies they may select layers by name a select layers they did not create. Also Layer names need not be unique so a wrong layer may get select or a layer may not exist.  Many this can go wrong in both Actions and Script Scripts can catch errors and do something else.

Download my package read both the text file and the short utilities scripts skip reading the larger scripts in the package.

JJMack
c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
December 22, 2011

// 2011, use at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

if (myDocument.width > myDocument.height) {

          myDocument.rotateCanvas(90)

          };

};

Inspiring
December 22, 2011

Hi c.pfaffenbichler

That´s what I need. I inserted some more lines so it suit other of my needs.

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

if (myDocument.width < myDocument.height) {

          myDocument.close()

          };

else     

      if (myDocument.height < myDocument.width) {

          myDocument.rotateCanvas(90)

          };

};

Best Regards and thank you a lot again

Gustavo.