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

Identify the orientation of the image

Advocate ,
Dec 22, 2011 Dec 22, 2011

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

4.3K

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

Community Expert , Dec 22, 2011 Dec 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)

          };

};

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 22, 2011 Dec 22, 2011

Copy link to clipboard

Copied

// 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)

          };

};

Votes

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
Advocate ,
Dec 22, 2011 Dec 22, 2011

Copy link to clipboard

Copied

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.

Votes

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
Community Expert ,
Dec 22, 2011 Dec 22, 2011

Copy link to clipboard

Copied

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

Votes

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
Advocate ,
Dec 23, 2011 Dec 23, 2011

Copy link to clipboard

Copied

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.

Votes

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
Community Expert ,
Dec 23, 2011 Dec 23, 2011

Copy link to clipboard

Copied

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

Votes

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
Advocate ,
Dec 24, 2011 Dec 24, 2011

Copy link to clipboard

Copied

Hi JJMack

I tried some codes and finally got it yesterday. Here´s what I was looking for:

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

if (myDocument.width > myDocument.height) {

          myDocument = doAction ("H1", "Actions")

         

          };

else

if (myDocument.width < myDocument.height) {

          myDocument = doAction ("H2", "Action")

         

          };

};

That´s it

I´m very happy I was sucessful

Best Regards, merry christmas !!!

Gustavo.

Votes

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
Dec 24, 2011 Dec 24, 2011

Copy link to clipboard

Copied

You didn't handle the case where width == height.  (which will let some square images through without processing)

Votes

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
Community Expert ,
Dec 24, 2011 Dec 24, 2011

Copy link to clipboard

Copied

That could well be what he wants. He could of course add a third if if he want to do a do somthing special for a 1:1 aspect ratio case.  He may be using the adtions H1 and H2 to use canvas size to crop the document to a 1:1 aspect ratio using the heigh for landscape and width for protrait documents.  The code does not let some square images through without processing it lets all square image through.

JJMack

Votes

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
Guru ,
Dec 25, 2011 Dec 25, 2011

Copy link to clipboard

Copied

I think Chris Cox was right in pointing out the gap in the script logic. Even if the OP meant to skip square image( which I doubt ), others read these threads and use the snippets posted. I would have pointed out that because an image can't be both landscape and portrait that only the else or second if statement is needed, not both. If he just had one if statement to handle portrait and one to handle landscape it would be clearer that square images were meant to be skipped.

And I understand what he meant by 'some square images'. If the width is within a few pixels of the height it still looks square even if it doesn't pass the width == height test.

Votes

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
Community Expert ,
Dec 25, 2011 Dec 25, 2011

Copy link to clipboard

Copied

Michael I think your reading to much between the lines.  I did not write anything to infer Chris was wrong in pointing out that square images will fall through. To me the code looks like its designed to do that. The original question seemed to be Landscape or Portrait this could be done with a single If statement which would cause square image fall into else clause putting Square into the Landscape or Portrait category depending oh the if.   Image that are not square but are close to being square and look square still will be processed by action H1 or H2 by the code shown. Though the code looks like that to me.  Chris was perfectly correct in making the append for new programmers often make that type of error. Heck I not that new to programming and have made that type of error.  One learns from making errors. I've made so many errors I must have learned a lot unfortunately I think I have forgotten more then I ever learned.

JJMack

Votes

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
Advocate ,
Dec 26, 2011 Dec 26, 2011

Copy link to clipboard

Copied

Hi Chris, JJMack and Michael

Thank you a lot for the insight. I´m very beginner on programming JavaScript. At true that code to differ horizontal and vertical actions suits perfectly my needs because the photos I was intended to process was originated from a digital camera (so no image file is square, has the exactely width and height size).

But you are true Chris. The code should have an "if" for squares. It´s a gap in the script !!!!! Thank you. I assume I have not thought about this.

Well...would be the code like it:

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

if (myDocument.width > myDocument.height) {

          myDocument = doAction ("H1", "Actions")

        

          };

else

if (myDocument.width < myDocument.height) {

          myDocument = doAction ("H2", "Actions")

        

          };

else

if (myDocument.width == myDocument.height) {

         

          myDocument = doAction ("H3", "Actions")

    

          {

};

Please fell free correcting the code if it´s wrong !!

Best Regards

Gustavo.

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

I'm using Apple Script Editor to save the below script. How do I save the script for photoshop 2022? I'm don't see jsx?

 

var doc = activeDocument;

if(doc.width.value>doc.height.value){//Landscape

doAction("Landscape", "Photos");

}else{//Portrait

doAction("Portrait", "Photos");

}

 

Votes

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
LEGEND ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

Don't use that editor but maxOS text editor with plain text.

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

Thank you. That worked. I use this script when photoshop opens a jpg image file and then saves ot as a psd.  Another issue I'm having is the script runs everytime I open the same photo in photoshop. I need to only run the script on jpgs and not psd files. Any thoughts?

Votes

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
LEGEND ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

if (/jpg$/i.test(activeDocument.name)) {
	/*your code*/
}

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

Thank you. So it would be

 

if (/jpg$/i.test(activeDocument.name))

{/*var doc = activeDocument;
if(doc.width.value>doc.height.value){//Landscape
doAction("Landscape", "OTS Photos");
}else{//Portrait
doAction("Portrait", "OTS Photos");*/}

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

I can't seem to get it to work with the code. I'm sorry, but I'm probably
missing something. This is what I have.

if (/jpg$/i.test(activeDocument.name)) {
/*var doc = activeDocument;
if(doc.width.value>doc.height.value){//Landscape
doAction("Landscape", "OTS Photos");
}else{//Portrait
doAction("Portrait", "OTS Photos");*/
}


*Johnny Miller*

On The Spot Photos | Founder
518-424-3986 (m) | otsjohnny@gmail.com
otsphotos.com

Votes

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
LEGEND ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

Remove /**/ part from the code.

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

so it should look like this? I'm still having problems.

 

if (/jpg$/i.test(activeDocument.name)) {
var doc = activeDocument;
if(doc.width.value>doc.height.value){//Landscape
doAction("Landscape", "OTS Photos");
}else{//Portrait
doAction("Portrait", "OTS Photos");
}

Votes

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
Community Expert ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

@otsphotos â€“ You were missing a closing bracket at the end:

 

if (/jpg$/i.test(activeDocument.name)) {
    var doc = activeDocument;
    if (doc.width.value > doc.height.value) { //Landscape
        doAction("Landscape", "OTS Photos");
    } else { //Portrait
        doAction("Portrait", "OTS Photos");
    }
}

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

Thank you for finding my mistake, but the script still doesn't work with
the added
if (/jpg$/i.test(activeDocument.name))

I just think it's not a good overall script. The following script works
fine:

var doc = activeDocument; if (doc.width.value > doc.height.value) {
//Landscape doAction("Landscape", "OTS Photos"); } else { //Portrait
doAction("Portrait", "OTS Photos"); }

The problem is once I run it on the jpegs and my actions place a layer on
the jpg and save as a psd file, if I open the image again as the psd file,
the script runs again since it's triggered by the open file command in the
events scripts manager.

Maybe there's a script that sees the .psd file and doesn't run the action.
I don't really know the solution.

Your below script works but nothing is different, it runs on both psd and
jpg...

#target photoshop app.bringToFront(); if
(activeDocument.activeLayer.isBackgroundLayer
&& /\.(jpg|jpeg)$/i.test(activeDocument.name)) { var doc = activeDocument;
if (doc.width.value > doc.height.value) { doAction("Landscape", "OTS
Photos"); } else { doAction("Portrait", "OTS Photos"); } }

*Johnny Miller*

On The Spot Photos | Founder
518-424-3986 (m) | otsjohnny@gmail.com
otsphotos.com

Votes

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
Community Expert ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

 

@otsphotos 

 

I have tested all three scripts, in isolation and also with the Script Events Manager Open Event and all three work as expected.

 

Please use the following button in the forum reply toolbar when posting code:

 

</>

 

Votes

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
Explorer ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

thank you

 

Votes

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
LEGEND ,
Apr 24, 2022 Apr 24, 2022

Copy link to clipboard

Copied

He most likely answers via E-Mail.

Votes

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