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

Total Pixels of an Image

Explorer ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

Hi,

I wanted to find the total pixels of an image as I have some rejections from Apple for the images above 4 million pixels.

Is there Script or any Batch method we can use to process the images?

Arvind

TOPICS
Actions and scripting

Views

1.6K

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
Adobe
Advocate ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

For single images you can use something like this...

#target photoshop

if (activeDocument.width * activeDocument.height >= 4000000)

    alert("Image contains more than 4 mio pixels!");

else(alert("Image accepted!"));

If you want a batch method, we have to know what to do with the images with more than 4mio pixels (delete/move/open/crop/...?)

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
Guide ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

Shouldn't that figure be times by activeDocument.channels.length ?

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 ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

Thanks Tom for the suggestion.

I wanted to check Photoshop if the images are of more than 4 Million pixels.

Resize the Image in a proportion and the pixel of the should come to less than 4 Million (3.99 million)

If this is possible please help me with it.

regards

Arvind

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 ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

Working in Units Pixels Tom check the canvas size,  If its over your limit you can resize the image however you need to work out the math to account for the images Aspect Ratio  Current width/current height. My maths skills are not as good as they use to be it would take me some time to come up with the right formula come up the percent reduction needed to  reduce the size to just under 4mp. you need to reduce both the width and height by the same percentage to come.  You know the Aspect ratio and you know the limit is 4mp so you can calculate what the width and height need to be. Then you then calculate the percent the image width and high need to be reduced by.

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
Community Expert ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

I think my formula has the correct math. However I do not know JavaScript of its Math functions or  what type or precision  or how to handle  My resize function always resizes within 800 pixels of 4MP or less Square 1:1 Aspect ration resize exactly to 4MP however  3:2 and 4:3 common camera Aspect Ratio resize hundreds of Pixels less than 4MP and 16:9 aspect ratio image resize 500 pixel over 4MP.

Perhaps if you know Javascript and Photoshop you can fix my code.

/* ==========================================================

// 2019 John J. McAssey (JJMack)

// ======================================================= */

// This script is supplied as is. It is provided as freeware.

// The author accepts no liability for any problems arising from its use.

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// ensure at least one document open

if (!documents.length) alert('There are no documents open.', 'No Document');

else {

        // declare Global variables

        //main(); // at least one document exists proceed

        app.activeDocument.suspendHistory('4MP','main()');

}

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

//                            main function                                  //

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

function main() {

        // declare local variables

        var orig_ruler_units = app.preferences.rulerUnits;

        var orig_type_units = app.preferences.typeUnits;

        var orig_display_dialogs = app.displayDialogs;

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

        app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS

        app.displayDialogs = DialogModes.NO;                // Set Dialogs off

        try { code(); }

        // display error message if something goes wrong

        catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

        // catch(e){$.writeln(e)}

        app.displayDialogs = orig_display_dialogs;          // Reset display dialogs

        app.preferences.typeUnits  = orig_type_units;   // Reset ruler units to original settings

        app.preferences.rulerUnits = orig_ruler_units;  // Reset units to original settings

}

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

//                           main function end                               //

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

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

// The real code is embedded into this function so that at any point it can return //

// to the main line function to let it restore users edit environment and end      //

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

function code() {

        if (activeDocument.width.value * activeDocument.height.value >= 4000000)  {

                //alert("Image Size = " + activeDocument.width.value * activeDocument.height.value + " Pixels");

                var resizePercent=(Math.sqrt(4000000/(activeDocument.height.value/activeDocument.width.value)))/activeDocument.width.value;

                activeDocument.resizeImage(activeDocument.width.value*resizePercent, activeDocument.height.value*resizePercent); // user default interpolation

                alert("New Size = " + activeDocument.width.value * activeDocument.height.value + " Pixels");

        }

}

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

//                      Helper Functions                                        //

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

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
Guide ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

The code to resize is in the link I provided.

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 ,
Jul 12, 2019 Jul 12, 2019

Copy link to clipboard

Copied

The code I saw there was Paul's code that seem to deal with file sized not Canvas size. I tired setting 12MB in that code  4MP uncompressed in 8bit mode. The code always resize to a 12MB file but the images have more the 4MP.  And if It has a single channel like grayscale the canvas size produced is huge.like 12MP not 4MP.

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
Guide ,
Jul 12, 2019 Jul 12, 2019

Copy link to clipboard

Copied

The file size is dependant on the number of pixels.

Did you check the pixel size in Photoshop?

2019-07-12_125746.jpg

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 ,
Jul 12, 2019 Jul 12, 2019

Copy link to clipboard

Copied

The OP is not interested in File size in bytes they are interested in number of pixels the canvas has  X pixels wide Time Y pixel height = 4MP.   Not the image file File Size MB. They want to resize images the have more that 4MP to an image with 4MP.     My code come close to 4MP  I do not know JavaScript so I can not figure out why my formula is not working to resize to 4MPixels.

resizePercent=(Math.sqrt(4000000/(activeDocument.height.value/activeDocument.width.value)))/activeDocument.width.value; 

It only seem to work perfectly when the image's Aspect Ration is 1:1 where width = height images. Images larger than 4MP are resized to 2000x2000 4MP.

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
Community Expert ,
Jul 13, 2019 Jul 13, 2019

Copy link to clipboard

Copied

LATEST

Turns out my math is correct however  you can not have a row or columns of Partial pixels  Photoshop's resizeImage rounds width and height to an integer.  Therefore, the resize may miss the resize target of 4MP. The miss can be more or fewer pixel then the target 4MP canvas size.  You can force it to be less by forcing the rounding to be in the downward direction Math.floor.  But that my effect the image's aspect ratio more then normal rounding I would not recommend using floor.

Here is code that has an alert message the show the resizeImage rounding that takes place and how much thr target is missed by.

function code() {

var numberPixels = 4000000;              // Target Canvas size 4MP

    if (activeDocument.width.value * activeDocument.height.value >= numberPixels)  {

       var resizePercent=(Math.sqrt(numberPixels/(activeDocument.height.value/activeDocument.width.value)))/activeDocument.width.value;

       alert("Image Size " + activeDocument.width.value + " * " + activeDocument.height.value + " = "  + activeDocument.width.value * activeDocument.height.value

       + " Pixels\nResize to\n"

       + resizePercent*activeDocument.width.value

       + " Wide\n"

       + resizePercent*activeDocument.height.value

       + " High\n"

       + resizePercent*activeDocument.width.value*resizePercent*activeDocument.height.value

       + " Pixels\nRounded ReSize "

       + Math.round(resizePercent*activeDocument.width.value)

       + " * "

       + Math.round(resizePercent*activeDocument.height.value)

       + " = "

       + Math.round(resizePercent*activeDocument.width.value)*Math.round(resizePercent*activeDocument.height.value)

       + " Pixels\nOff by "

       + (Math.round(resizePercent*activeDocument.width.value)*Math.round(resizePercent*activeDocument.height.value)-numberPixels)

       + " Pixels" );

       activeDocument.resizeImage(activeDocument.width.value*resizePercent , activeDocument.height.value*resizePercent); // user default interpolation

       //activeDocument.resizeImage(Math.floor(activeDocument.width.value*resizePercent) , Math.floor(activeDocument.height.value*resizePercent)); // force lower than target

    }

}

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
Guide ,
Jul 11, 2019 Jul 11, 2019

Copy link to clipboard

Copied

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