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

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

Copy link to clipboard

Copied

OK. I have it all downloaded. I'll work on this as quickly as I can. Please be patient.

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

Copy link to clipboard

Copied

no problem, 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
Community Expert ,
Mar 22, 2023 Mar 22, 2023

Copy link to clipboard

Copied

Hi @heroleam , Is the .PSD you posted typical of all the artwork? If that’s the case what’s the purpose of converting the grayscales into a halftoned bitmap? If I split your channels into Grayscales, there are no gray values—all the pixels are either black or white—so if I convert to a halftoned bitmap there are no halftone dots created. In this case the conversion to a 600 PPC Bitmap is effectively a Nearest Neighbor upsample.

 

Screen Shot 9.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 22, 2023 Mar 22, 2023

Copy link to clipboard

Copied

we do this conversion to use in the RIP for photolithography production, if the art has half tone, the RIP converts it into half-tone points.

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

Copy link to clipboard

Copied

like this

heroleam_3-1679499518145.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
Engaged ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

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 algorithm to arrive at the pixel count shown and this isn't a simple ratio such as 4 to 1 or 8 to 1. If the selection is a rectangle, it works out to a simple ratio but with the selection being a shape, the combination of the pixels along the edge are not predictable without knowing their algorithm.

 

So with this in mind, I have to ask if this is the value you need to use to determine how much of each paste mixture will be needed for the print because this value will not exactly represent the number of pixels within the selected area. Even if you are resizing your print (I tried using the resizing/resampling methods) this pixel count won't match. I do get close when using a resizing with various resampling methods (within 1 percent and usually within 0.2 percent) but never consistently exact.

 

So I'm guessing that you already know how much paste you need to use for each square centimeter. You also know how many ACTUAL pixels are within the selection (using the histogram calculations and adding all pixels at all levels). You know how many pixels are in a centimeter (600) so a square centimeter would have 360000 pixels. Divide total pixels by 360000 and you will have square centimeters of print area for the selection. Multiply that value by the amount of paste needed for each square centimeter and you would have your value for the amount of paste needed.

 

The only thing I have left that I can look into is ActionScript which isn't really documented well for Photoshop. And so far I've been unable to find where they keep that value using ActionScript if they even do have it there.

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

Copy link to clipboard

Copied

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 how many pixels Photoshop shows in 1 cm2, taking into account caching, if we can accurately calculate the number of pixels using the original script!?

 

Perhaps @heroleam needs to know not the TOTAL number of pixels in the selection, but only the number of BLACK pixels? In this case, it is enough to modify the script as follows:

 

#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;

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

// Show an alert
alert("Black pixels: " + histogram[0]);

(the original script that the author generated via chatGPT calculates the sum of ALL pixels, both black and non-black)

 

2023-03-26_00-45-36.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 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

Thanks for your time, but we noticed here that the script was correct from the beginning, what was wrong is as @jugenjury  said in the cache issue, the initial script was already giving the correct selected pixel amount, thanks anyway, and this script of yours is now also interesting!

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

Copy link to clipboard

Copied

OK. So now that that's settled, are you needing the same information from all the original channels using the same process?

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

Copy link to clipboard

Copied

LATEST

it won't be necessary, because the script from the beginning was correct, the calculation they were doing here was wrong, that's why the values didn't match.

 

for example an area of 10x10 cm has 36000000px
we divide this value by 1 cm² which is 360000px
will give a value of 100cm²
then we must take 100cm² and multiply it by the value in grams of the material which is 0.005 g
at the end it will give the value of 0.5 grams of use in this 10x10cm area

if you can confirm this calculation 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
Community Beginner ,
Mar 25, 2023 Mar 25, 2023

Copy link to clipboard

Copied

@jugenjury @jazz-y 

Just confirm one thing, I must take the total number of pixels and divide by the number of pixels in a centimeter, then take this value and multiply it by the number of grams used in 1 square centimeter, that would be it, right?

total pixel / pixel² = x
x * grams paste = consumption in grams

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