• 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
Explorer ,
May 04, 2022 May 04, 2022

Copy link to clipboard

Copied

I would like have a script that works when the image is printed in Photoshop and roatates the landscape images 90 degrees before printing and does nothing to the portrait images.

 

#target photoshop
app.bringToFront();
if (activeDocument.layers.length === 1 && /\.(jpg|jpeg)$/i.test(activeDocument.name)) {
var doc = activeDocument;
if (doc.width.value > doc.height.value) {
activeDocument.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
LEGEND ,
May 04, 2022 May 04, 2022

Copy link to clipboard

Copied

Doesn't it rotate only landscape images?

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 wrote:
Thank you for finding my mistake, but the script still doesn't work with
the added
if (/jpg$/i.test(activeDocument.name))

 

You could also use .match() instead of .test() – which are both essentially doing the same thing in different ways:

 

Edit: Code corrected/updated –

 

if (activeDocument.name.match(/\.jpg|\.jpeg$/i)) {
    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
LEGEND ,
Apr 24, 2022 Apr 24, 2022

Copy link to clipboard

Copied

.test returns boolean while .match an array.

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

Copy link to clipboard

Copied

This is true... However, In the context of an if conditional check on the filename – it would appear to me that .match() and .test() are interchangeable.  

 

I didn't state that they were exactly the same, just that they were essentially doing the same thing in different ways. This is what your post and the following pages also indicate:

 

https://www.w3schools.com/jsref/jsref_regexp_test.asp

https://www.w3schools.com/jsref/jsref_match.asp

 

Am I missing something?

 

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

If you don't do anything with matched items then .test should be used as faster method.

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

Copy link to clipboard

Copied


@Kukurykus wrote:

If you don't do anything with matched items then .test should be used as faster method.


 

You are of course correct that one is faster than the other, not that a human would ever notice the difference in this use case:

 

if (activeDocument.name.match(/\.jpg|\.jpeg$/i)) {
// Run Time: 0.003 seconds
}

if (/\.jpg|\.jpeg$/i.test(activeDocument.name)) {
// Run Time: 0.001 seconds
}

 

As the OP was having problems with the .test() method, I suggested .match() as a viable alternative to try, just in case there was a different result (there shouldn't be though).

 

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

OP did not have problems with .test method.

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

Copy link to clipboard

Copied


@Kukurykus wrote:

OP did not have problems with .test method.


 

 

That's not what I'm reading:

 

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

 

 

To be clear, I can't reproduce this issue. Both .test() and .match() produce the same correct result in my tests. The script processes only files with a .jpg or .jpeg extension, ignoring others such as .psd

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

Replacing test to match can't help. That suggestion was redundant. User simply does some other thing wrong we don't see or refers to ScriptEventsManager functionality, where maybe old instance of script still is enabled.

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

Your script triggered by Events Script Manager can't be run on psds. It doesn't fulfil condition.

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

If that works then yes. Next time when posting codes use </> icon.

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

If the script manager can't determine between file extensions, the other thing that is unquie between the files, is the first script runs an action that places a layer on the image. Maybe there's a script that won't run the action if there's already a layer on the image. 

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

I don't understand what other first script, and what script manager you are talking about now.

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 wrote:

Maybe there's a script that won't run the action if there's already a layer on the image. 


 

Original post edited: If you wanted to test that the active layer was a Background layer and that the doc only had one layer (which meets the criteria for a JPEG without explicitly testing for the filename extension, so is not foolproof):

 

 

#target photoshop
app.bringToFront();
if (activeDocument.activeLayer.isBackgroundLayer && activeDocument.layers.length === 1) {
    var doc = activeDocument;
    if (doc.width.value > doc.height.value) {
        doAction("Landscape", "OTS Photos");
    } else {
        doAction("Portrait", "OTS Photos");
    }
}

 

 

Note: I have removed the original second example as although technically working, it was testing for a redundant impossible condition.

 

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 wrote:

If the script manager can't determine between file extensions, the other thing that is unquie between the files, is the first script runs an action that places a layer on the image. Maybe there's a script that won't run the action if there's already a layer on the image. 


 

Just to be clear on your wording and perhaps understanding of how things work. The Script Events Manager simply runs a script or action if an Event is triggered, it has no built-in logic.

 

It is up to the conditional or try/catch code used in the triggered script to determine the file extension or other required criteria.

 

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
New Here ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

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

 

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 ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

@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.

 

filter-rename.png

 

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:

 

size-rename.png

 

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 ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

@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:

 

orientation-title.png

 

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'

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 ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

LATEST

The following ExifTool code will move files to new folders inside the parent folder based on their orientation (portrait, landscape or square):

 

exiftool -if "$imagewidth > $imageheight" -directory=%d"Landscape Images" -execute -if "$imagewidth < $imageheight" -directory=%d"Portrait Images" -execute -if "$imagewidth == $imageheight" -directory=%d"Square Images" -common_args "C:\Users\username\Desktop\my folder full of images"

 

This example is for Windows, for the Mac, simply swap the double straight quote marks " for single straight quotes ' and use the Mac path to the root folder containing the mix of images. 

 

___________

 

Here is a script from @SuperMerlin for Adobe Bridge to do the same:

 

https://community.adobe.com/t5/bridge-discussions/photoshop-2020-script-for-sorting-photos-into-sepa...

 

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