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

Script that copies the selected number of pixels

Community Beginner ,
Mar 16, 2023 Mar 16, 2023

Copy link to clipboard

Copied

Hi guys, I've been trying to make this script for a while and I can't get the result I need.

I'm trying to make a script that copies the amount of selected pixels to the clipboard, but it doesn't give the same value that appears in the histogram, can anyone help me?

heroleam_0-1678991934320.png

#target photoshop

// Get the current Photoshop document
var doc = app.activeDocument;

// Get the brightness (Y) channel of the image
var channel = doc.activeChannels[0];

// Get the histogram of the luminosity channel
var histogram = channel.histogram;

// Get the total sum of histogram values
var totalPixelCount = 0;
for (var i = 0; i < histogram.length; i++) {
  totalPixelCount += histogram[i];
}

// Get the selected pixel amount value from the histogram
var pixelCount = totalPixelCount - histogram[0];

// Copy the value to the clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), totalPixelCount);
executeAction(stringIDToTypeID("textToClipboard"), d, DialogModes.NO);

// Show an alert
alert("Pixels: " + totalPixelCount);

 

TOPICS
Actions and scripting , macOS , Windows

Views

5.5K

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 5 Correct answers

Engaged , Mar 18, 2023 Mar 18, 2023

For cache level 2, divide totalPixelCount by 4.

For cache level 3, divide totalPixelCount by 16.

For cache level 4, divide totalPixelCount by 64.

 

This won't match what's shown exactly because they use some fuzzy logic algorithm around the edges, but it will be close.

 

I'm trying to figure out a way to determine with javascript which cache level the histogram is using on the current pixel count but it has been a bit elusive.

 

Not sure if that helps, but so far that's the best I can do. I know you wan

...

Votes

Translate

Translate
Engaged , Mar 19, 2023 Mar 19, 2023

Also, as an FYI, the histogram cache level is based on the sample size (the size of your selection). Once you get around 400,000 pixels selected (slightly less than that) it shifts to the next higher cache. Level 2 combines 4 pixels into one so the number of pixels you see in the histogram is 1/4 the total number selected. Level 3 combines 4 of level 2 pixels together into one so the histogram shows 1/16 of the total number selected and Level 4 combines 4 of level 3 pixels so it shows 1/64 of th

...

Votes

Translate

Translate
Engaged , Mar 19, 2023 Mar 19, 2023

I'm also confused just like jazz-y is. Mathmatically this makes no sense. If you're trying to figure out how much ink or whatever for your print job based on the pixel count and the pixels per cm (or per in, whichever it is) you would probably want to know the actual number of pixels in the selection and not the combined pixels as shown in the Histogram window based on cache level. As I mentioned above, the cache level is based on the size of your selection and anything other than level 1 is not

...

Votes

Translate

Translate
Engaged , Mar 25, 2023 Mar 25, 2023

So, after spending a lot of time trying different methods to arrive at the value shown in the histogram dialog for Cache Level 4, I have come to the conclusion that there is no real way to get the number mathmatically without knowing the exact algorithm they use to get this value.

 

The Pixels count shown in any cache level other than 1 is NOT representative of the number of pixels in the selection. At least not an exact representation. Cache levels other than Level 1 use a combined pixels algorit

...

Votes

Translate

Translate
Guide , Mar 25, 2023 Mar 25, 2023

I re-read this thread over and over again. It seems to me that we have not come close to understanding the @heroleam's task.

 

1 cm2 at a resolution of 600 pixels/cm contains 360,000 pixels. This is an objective reality. In the table, we see approximately the same value - 359400, perhaps this is due to the fact that for a printing device 1 pixel is not equal to 1 point (that is, the resolution of the printing device is slightly less than the resolution of the file).

 

Why do we need to know ho

...

Votes

Translate

Translate
Adobe
Advisor ,
Mar 16, 2023 Mar 16, 2023

Copy link to clipboard

Copied

Hello @heroleam,

 

I ran your script and it seems to be working as intended...

Screen Shot 2023-03-16 at 11.14.52 PM.png

Regards,

Mike

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 Beginner ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

sorry, I forgot to mention that the channel needs to be grayscale.

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
Advisor ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

@heroleam 

I ran your script again on a grayscale image and it seems to be working as intended...

Screen Shot 2023-03-17 at 10.09.17 AM.png

Regards,

Mike

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 Beginner ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

that's strange, try this file of mine here then

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 ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

You see the cached histogram. Caching levels (which you can set in the performance settings) allow you to speed up the application due to the fact that Photoshop divides the bitmap into small tiles (something like a reduced copy of the image in memory), in particular, this approach allows you to speed up the display of histograms.

 

2023-03-17_17-39-07.png

2023-03-17_17-39-29.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
Advisor ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

@heroleam,

 

As @jazz-y pointed out, you need to hit the refresh in the histogram panel to get the correct calculation "pixel count". I found I only needed to do that if the area selected included any of the white background.

 

Regards,

Mike

 

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 Beginner ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

I understand, but I needed to get the value that is there without updating the cache.

I'll explain why, I work with serigraphy, we need to split the channel in the channels because we separate each color into a channel for recording the frame to print, after splitchannel we convert the channel to bitmap at 600 dpi per cm and then convert it to grayscale, we do the selection of the channel and we invert it to stay only where there is color, and the result of the pixel is used in a formula here of ours to measure the amount that needs to be used to print, all because of costs, so I can't update the cache because if you are not going to break the cost of the material to be used, that is why I need you to copy exactly the value that appears there in the histogram

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
Engaged ,
Mar 18, 2023 Mar 18, 2023

Copy link to clipboard

Copied

For cache level 2, divide totalPixelCount by 4.

For cache level 3, divide totalPixelCount by 16.

For cache level 4, divide totalPixelCount by 64.

 

This won't match what's shown exactly because they use some fuzzy logic algorithm around the edges, but it will be close.

 

I'm trying to figure out a way to determine with javascript which cache level the histogram is using on the current pixel count but it has been a bit elusive.

 

Not sure if that helps, but so far that's the best I can do. I know you wanted it copied to the clipboard but with a simple script like this, it would have to copy all of them or a predetermined cache level which you would have to input. Another option would be to have the script pop up a window with 4 selections and you select the current cache level from there and it will copy that value into the clipboard.

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
Engaged ,
Mar 18, 2023 Mar 18, 2023

Copy link to clipboard

Copied

OK. I figured out the "fuzzy logic algorithm" part and it isn't so fuzzy afterall. To get the histogram pixel counts for each cache level...

 

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
var startDisplayDialogs = app.displayDialogs
// Set Adobe Photoshop to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
app.displayDialogs = DialogModes.NO


// Get selection bounds
var sel=app.activeDocument.selection.bounds;

// Get selection width and height
var w = Number(sel[2]) - Number(sel[0]);
var h = Number(sel[3]) - Number(sel[1]);

// Get Pixel count for each histogram cache level
var l1 = w * h;
var l2 = Math.ceil(w/2) * Math.ceil(h/2);
var l3 = Math.ceil(w/4) * Math.ceil(h/4);
var l4 = Math.ceil(w/8) * Math.ceil(h/8);

//Restore prior preferences.
app.preferences.rulerUnits = startRulerUnits
app.preferences.typeUnits = startTypeUnits
app.displayDialogs = startDisplayDialogs

I still haven't figured out how to get the current histogram cache level.

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 Beginner ,
Mar 18, 2023 Mar 18, 2023

Copy link to clipboard

Copied

how would it look in the complete code?
Just to point out, I didn't make this script with my own knowledge, I made it with the help of chatgpt, so could you show me how the code would look completely?

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
Engaged ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

I can. Let me try to work out a bit more on this. I think I've come close to figuring out how to determine the cache level. I'll have some time later.

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
Engaged ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

Also, as an FYI, the histogram cache level is based on the sample size (the size of your selection). Once you get around 400,000 pixels selected (slightly less than that) it shifts to the next higher cache. Level 2 combines 4 pixels into one so the number of pixels you see in the histogram is 1/4 the total number selected. Level 3 combines 4 of level 2 pixels together into one so the histogram shows 1/16 of the total number selected and Level 4 combines 4 of level 3 pixels so it shows 1/64 of the total number selected. Is this what you want to get? Mathmatically this make sense to me if your end goal is to resize smaller to stay within a certain print size based on your pixels per cm. If that's what you're trying to do, there may be an easier and more accurate way to do this.

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 Beginner ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

I just need it to save the value of the pixel that appears there in the histogram, and not what is behind the code, according to my print at the beginning

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 ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

I'm sorry, but this sounds absurd. If your image has ACTUALLY 95534348 pixels, then why do you need to know how many of them Photoshop uses in its calculations? If you change Photoshop's performance settings, the numbers will be completely different. How can you base your calculations on these values if they have nothing to do with the physical process of printing?

 

By the way, I noticed one mistake that probably affects your results - above you write that you change the file resolution to 600 dpi. At the same time, the file you attached has a resolution of 600 pixels/cm (!!!). This is 1524 pixels/inch. If we change the file resolution to 600 pixels/inch, we get a value of 14808800 pixels in the image, which is (apparently) much closer to the result you expect. Please check it.

 

2023-03-19_22-03-56.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 Beginner ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

it's because you're seeing it in inches, if you change it to centimeters it will give you 600 dpi.

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 ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

DPI = Dots Per Inch (in digital files, dimensions are indicated not as DPI, but as PPI, but, at the user level, we do not distinguish between these terms)

 

600 pixels/cm not equal 600 pixels per inch

600 pixels/cm equal 1524 pixels per inch

 

You increase the number of pixels in the file by 2.54 times and then wonder what you get from a script that counts them as incorrect (in your opinion) 🤷‍

 
 

 

 

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 Beginner ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

it's just that here in Brazil we call it dpi in general, we don't make this division, the people here understand each other.
And that's what I said there before, 1054 dpi/in would be 600 dpi/cm

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 Beginner ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

1524 dpi/in***

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
Engaged ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

Try this. The only thing you might have to change is the first line. You have to set the var cacheLevels to whatever is set in your preferences. I put it at 4 since that seems to be the Photoshop default value.

 

 

#target photoshop
// Set cacheLevels to the number as set in preferences (performance tab)
var cacheLevels = 4;

// Get selection bounds
var sel=app.activeDocument.selection.bounds;

// Get selection width and height
var w = Number(sel[2]) - Number(sel[0]);
var h = Number(sel[3]) - Number(sel[1]);

// Create dialog window
var win = new Window('dialog', 'Histogram Pixels');
win.size = [200,200];
win.button = new Array();
win.pixels = new Array();
var u = undefined;
var cLevel = 0;
var totalPixelCount;
pix = new Array();
var p = 20;
for (i=1; i<=cacheLevels; i++) {
pl = Math.pow(Math.pow(4, i), .5)/2;
pix[i] = Math.ceil(w/pl) * Math.ceil(h/pl);
win.button[i] = win.add('button',[20, p, 60, p+20],'Level '+i);
win.pixels[i] = win.add('statictext',[80, p, 150, p+20],pix[i]);
win.button[i].value=i;
win.button[i].onClick = function() { cLevel = this.value; win.close(); }
p+=35;
}
win.cancel=win.add('button',[50, p, 70, p+20],"Cancel");
win.cancel.onClick=function() { win.close(); };
win.center();
win.show();
if (cLevel > 0) totalPixelCount = pix[cLevel];
else totalPixelCount = "Not Selected";
// Copy the value to the clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), totalPixelCount);
executeAction(stringIDToTypeID("textToClipboard"), d, DialogModes.NO);

alert(totalPixelCount);

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
Engaged ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

I'm also confused just like jazz-y is. Mathmatically this makes no sense. If you're trying to figure out how much ink or whatever for your print job based on the pixel count and the pixels per cm (or per in, whichever it is) you would probably want to know the actual number of pixels in the selection and not the combined pixels as shown in the Histogram window based on cache level. As I mentioned above, the cache level is based on the size of your selection and anything other than level 1 is not your actual pixels.

 

Example. Let's say you select an area that is 400 x 999. It will most likely use cache level 1 and give you 399,600 pixels. If you moved over 1 more pixel in your selection to 400 x 1000, you would drop to cache leve 2 and get a pixel count of 100,000 even though you actually added 400 pixels to your selection.

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 Beginner ,
Mar 19, 2023 Mar 19, 2023

Copy link to clipboard

Copied

it's still giving a value out, my cache is at 4

heroleam_1-1679274246519.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 Beginner ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

Guys, I talked to the people here and we are going to evaluate the information that you passed on.

 

So how could we do it right? follow the process we do:
We split the channels
convert to bitmap 600dpi per cm

heroleam_0-1679329650384.png

frequency 25

heroleam_1-1679329695012.png

after that we convert to grayscale

we select the channel and invert it so that the selection is only where there is color.

this would be the process to store the value of the pixels generated there

after that, the pixel value is placed in a spreadsheet to calculate consumption

Follow the link to the spreadsheet to consult: 

https://docs.google.com/spreadsheets/d/14OKhSHX4NjkrBvdsCnKzQfdV_lbwngszKdtGOyzbBBo

 

Now knowing that, what would be the most correct way to make the calculation work?

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
Engaged ,
Mar 20, 2023 Mar 20, 2023

Copy link to clipboard

Copied

Are you only doing this on one channel or all channels one at a time?

 

And FYI, the abbreviation "dpi" stands for dots per inch. Dots per cm would be "dpc". 

 

Are you using RGB channels? CMYK channels? Lab channels? The example you posted only has the one channel. I see 3 mixes on your spreadsheet so I'm guessing each mix is for a particular channel (color). Please correct me if I'm wrong.

 

Is the end goal here to determine how many pixels of each color are present in the image? Are you looking for just a straight pixel count of an overall value that determines how much of that color is present. For example, a single pixel if using RGB would have some Red, some Green, and some Blue but not always 100% of each (unless it's white). Of course for printing that would be opposite but same principle applies. What I'm getting at is are you looking for a totalizer for the pixels values or assuming all pixels are 100% and just counting them? Either way, I could see about coming up with a better method now that I'm more understanding of what you're doing.

 

Do you have an example file you can share? One that has the color and let me know which colors you use for your inks/pastes so I know how to break it down? If you're allowed to, that is.

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 Beginner ,
Mar 21, 2023 Mar 21, 2023

Copy link to clipboard

Copied

The mix there in the spreadsheet does not mean mixture, it's just that for you "mix" is to mix, but here "mix" is the name of the product.

The objective is to know how much product we will use per area of the drawing.

 

I will send you the file here with the colors, each channel will be a frame to print.

You need to use splitchannel on the channels and use the action on each channel to then do the pixel count.

 

Action: https://we.tl/t-oiFanfHUx7

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