How to get access to pixel data of the active layer?

Hello,
I am going to write the image filter with use of JavaScript. Is there any examples with extracting the individual pixel values to be able to iterate over them in two for loops?

If you are wanting to get an array of all RGB data, I’m pretty sure that can’t be done with UXP, at least not yet.

I guess you’ll have to use color sampler and batchPlay. At least that’s what I do in my jsx script - get the dimensions and loop through each row and column adding color sampler, getting info and removing sampler (IIRC only 10 samplers are allowed on a document)

I have done that in JSX but it is was really slow to sample all pixels and then to also fill the pixels one at a time with new values. So I just figured it wasn’t much of an option.

Yes, it’s slow :frowning: If you find better solution, let us know too :slight_smile: I plan to refactor jsx to UXP at some point :slight_smile:

This was discussed briefly here in the comments section on the bottom of this blog post. Davide Barranca knows a way to get the values through Generator. I’m not sure how that would tie into UXP because I’ve never messed with Generator myself. It sounds like there may be a way in the future to set the values through the “imaging” API as mentioned in the comments section too.

It would be so awesome to have something in the UXP DOM to get an array of pixels and also be able to set the pixels too. Just getting the array doesn’t do much good if you can’t upload new values after doing some math. This is top on my wish list for sure :slight_smile:

Thank you for your answers. Could you be so kind to share the batchScript?

@DavideBarranca, you mention in you blog post comments, that we already have a getter. Where can I find more info? For now only getter is what I need and color samplers solution, as it was already mentioned, is really very slow

The thing is not very well documented – the only actual public examples (besides the source-code that you can find in the Adobe Generator and Generator Assets repositories) come from tutorials by then-Adobe’s Tom Krcha, now available only through Archive.org. Those can get you started.

If you need more, I’ve got ~60 pages on Adobe Generator plugins, including various examples of the getPixmap() function, in my Professional Photoshop Scripting book.

HTH!
Davide

2 Likes

Following Davide’s links I found this JSX to extract pixels from a layer (not a document). But not sure how this should be refactored to UXP and batchPlay :confused:

Interesting. I’ve never seen that method. I wonder if there is a way to set the pixels too? In most use cases, just getting the pixel map doesn’t do much good without being able to set the pixel map to something new.

It would be really awesome if there would be a future feature added for a single line command in the DOM to get and set pixels to a layer :slight_smile:

Thanx, for you answers.
This topic look like only part of me problem (I will be faced with set pixels to the layer after the application of my filter).
Is it possible to do this with the workaround:

  1. Save active layer content to .png file to the local storage
  2. Read it from local storage with javascript
  3. Transform it to the image array (y_size, x_size, channels)
  4. Save the result to the *.png
  5. Load the *.png into the new layer

Can it be the solution?

What are you trying to do to the layer? The only reason you would need to get/set pixel data would be if you had your own special algorithm you needed to apply to the pixel data that was different from what Photoshop could already do. For anything else that Photoshop can do to the layer you could just use batchPlay to perform whatever Photoshop commands are needed directly on the layer. Almost all (but not all) Photoshop comnands can be done through batchPlay.

I’d like to automate the algorithms like this one https://youtu.be/sT52WfCNIgw.
It uses some features from machine learning. To do this I vitally need the access to the raw pixels.

There probably isn’t a simple way directly in UXP to do this right now. However, if you figure out how to do it then please post an update. I am interested in this myself because I also have a UXP plugin I want to get/set pixels for. However, I don’t want to use a slow and clunky work-around like the color sampler and the fill tool to do pixels 1 at a time either.

It was very easy for other Photo editors https://youtu.be/n_1ZSGy9zkA. It look like I will ber forced to use native C++ plugin to achieve my goal. Working on it…

Easier getting/setting of pixels will be coming in a future release. In the meantime, workarounds are the only option, although I know none of them are great.

3 Likes