Get selection using imaging.getSelection replicates the image several times

I would like to get the current selection in Photoshop and send it to a Python script where I will create an image to visualize it.

I have the following implementation:

selection = await imaging.getSelection({
                    "documentID": doc.id
                });
...
let mask = await imaging.encodeImageData({ "imageData": 
    selection.imageData, "base64": true });

After receiving the information in base64 in my Python application, I save it to an image and check that the selected object is shown three times in the same image and not in the expected place.
Any idea of whatĀ“s happening or how to solve it?

Selection and Image received:

Thanks.

1 Like

Can you be more specific about what you mean by ā€˜save it to an imageā€™? I.e. how do you turn the raw pixel data, returned by the imaging API into a .png file?

Maybe to avoid adding this extra phase of loading the data in python and save it to a file, it is OK to copy the value of ā€œmaskā€ in a page as: Base64 to Image | Base64 Decode | Base64 Converter | Base64 and I get the same result, the image painted three times.

Without actually knowing the problem youā€™re trying to solve eventually, have you considered not going through the imaging API and instead saving the document (or cropped document) directly as .png (I.e. let Photoshop do what itā€™s pretty great at)? That way you donā€™t have to deal with handling raw pixel data (and pixel channel ordering).

I cannot solve it that way, because I need to send the information and not saving the output locally.
What IĀ“m trying to do is: select an object in the image, get the selection and convert it to base64.

Ah, gotcha. In that case, think itā€™s just important to understand that what gets returned by the imaging API is raw pixel data, i.e. a flat array of just numbers in a particular order (e.g. all values of the red channel, followed by all values of the green etc. depending on the options you pass). So youā€™re on the hook to interpret that data correctly later on. The image on the right looks suspiciously like all the channels concatenated horizontally (i.e. misinterpreting the flattened pixel data). To debug I would check the dimensions (width/height) and number of channels (components) at every step in your process, to figure out where the error happens.

Thank you for your answers. I understand what you say but when I call this:

selection = await imaging.getSelection({
                    "documentID": doc.id
                });

I print some information regarding selection.imageData:

      W: 32 H: 32, Type: image/uncompressed
      Colour space: Grayscale, profile: Gray Gamma 2.2, has alpha: false
      Pixel format: Grayscale
      Components per pixel: 1, component size (bits): 8
      Is chunky: false

so I assume there is only one channel.
Also, I have checked that the size of the result of this:

const data = await selection.imageData.getData();

includes a 1024 Uint8Array (1024 = 32 x 32) so I assume that each element in the array corresponds to one pixel.
So I still donĀ“t understand why the image is repeated several times.
Thanks.

Would it do same with grayscale document as well?

hello,have you solved it?I encountered the same problem.

Me too,have you solved it?

Hi!
I couldnā€™t manage to get the expected result by directly using the one-channel image from the selection and the Grayscale profile so I replicated each value in the selection image three times in a new image and used the RGB profile. This way, I managed to obtain the expected result.
I add some code that maybe useful to identify the order of the replicated values:

...
    const pixelValue = selection[index];
    for (let channel = 0; channel < numChannels; channel++) {
        let index = ((row * numColumns) + column) * numChannels + channel;
        multiChannelBuffer[index] = pixelValue;
...