Copy link to clipboard
Copied
Hi!
I want to create a script that centers an image shot against a white background. My question is can javascript be programed to scan across a line of pixels that read 245,245,245 for example and count the number of pixels at that value for use in an equation?
The script would scan across a line of pixels.
Stop when it meets a value not equal to 245,245,245 by 10% or more.
Sum the number of 245,245,245 pixels from edge of image to first incidence of change. Eg. 240,215,215
Store sum as a variable
Do the same on the other side of the image.
Stores sum as a variable
Add the two variables
divide sum of two variables by two
use that value to center image within the frame. (600ps + 400px)/2 = amount of 245,245,245 space to place on each side of image.
I left a few details out like final image sized for brevity.
Is this possible?
Yes, reading pixels is very slow with javascript. The only workaround that I can think of is a technique I use for creating some art in lines. You can make a selection of 1 px across the width of the image. Using scriptListener, put that selection on it's own layer ctrl/cmd-J. Then record the magic wand tool, and make a function out of it using the contiguous setting. Then with your script set the sample points for the magic wand tool to one end of the line, get the bounds of the selection, then
...Copy link to clipboard
Copied
If the image is on a layer you wouldn't want to do that for there may be may be some completely empty areas so the shape of the images will not be a rectangle. However, the layer's bounds will be and you can use that information to center the layer over ant canvas size. If the canvas is small it will act like a clipping mask.
Easy to do select all of the canvas and align the layer to the selection. You can also use canvas size to add and remove canvas
Copy link to clipboard
Copied
What programming language is best suited for that? Can javascript count the number of pixels in a straight line of pixels at an assigned value until it hits another pixel outside of those parameters and return a value?
Copy link to clipboard
Copied
Just wondering whether that would work:
• Use the Color Range tool to select the border
• Invert selection
• Get the bounding box of the selection with the bounds property
• Use those coordinates for centering the image
This is just a brain fart, and may not work at all, but would definitely simplify determining the border. Implementation would be Javascript.
Copy link to clipboard
Copied
Hi,
discussing your question on a higher level, scripting isn't well suited for pixel management. That's a shame because, say, with InDesign (which at the end of the day is an application managing text) you can script the "atoms" of text, i.e. characters.
Being Photoshop an application managing images, it's peculiar that you can't really target the "atoms" of images, i.e. pixels.
At least not directly, if memory doesn't fail me you could use Generator to extract an one dimension array of RGBA values out of your image, but the detour isn't trivial.
Davide Barranca
Copy link to clipboard
Copied
Yes, reading pixels is very slow with javascript. The only workaround that I can think of is a technique I use for creating some art in lines. You can make a selection of 1 px across the width of the image. Using scriptListener, put that selection on it's own layer ctrl/cmd-J. Then record the magic wand tool, and make a function out of it using the contiguous setting. Then with your script set the sample points for the magic wand tool to one end of the line, get the bounds of the selection, then do the same for the other end. You can use the histogram to get the average.