I would like to open an image in Photoshop, use any of the selection tools to make a selection, and save the selection to a file which I can then open in another application, Matlab, to identify which pixels were selected. Several non-touching regions could be included in the selection. I know you can save a Photoshop selection to a PNG file, but unfortunately the file has the smaller size of the selected region so you can't easily ID the pixels in the original image. How can I get Photoshop to save a file containing a 'mask' where selected pixels are either white or their original color, and unselected pixels are either black or transparent?

(I am flexible on the type of saved file and its contents as long as I can use it to figure out the pixel locations. It's possible to do selections directly in Matlab, but for this particular task I must select in Photoshop then bring the results into Matlab. I could look for pixels in the selection-sized PNG file which match those in the original image, but is there is a better way?)

  • FYI using the selection mask in Matlab is discussed here, in case someone wants to do that.
    – KAE
    Sep 19, 2019 at 12:11

2 Answers 2


You could record this as an Action, but it's pretty quick to do manually…

Load picture, make selections - here I just did a couple of random rectangular selections with the marquee

enter image description here

Add a new layer then invert your selection using Cmd ⌘ Shift ⇧ I [Ctrl/Shift/I on Windows, I presume]

Switch to the Paint Bucket, G & click anywhere in the 'outside' of your original selection.
The new layer means the paint bucket won't hesitate to fill the whole area, no tolerance values required.

enter image description here

[Black border is just so you can see where the image ends]

Save as…
The selection ants [& border] will obviously not be there in the final exported image.

Edit: I misread the original request - but if you invert the selection twice & also flip the paint colour, X , you can have black & white instead with a couple more clicks. Switch off 'Contiguous' in the Bucket controls to fill multiple selections in one go.

If you want either black or white to be transparent instead, don't paint-fill it, then before you Save As… switch off your original layer.

  • Interesting solution. MATLAB can't recognize alpha channels or even layers, so he won't want to use transparency here. Upon importing, MATLAB will ignore every layer apart from the background. Sep 18, 2019 at 16:02
  • Use black & white then & don't save layers. PNG, btw doesn't use true alpha it uses a transparency layer [32bit vs 24bit files] - idk whether that would make a difference to Matlab [never used it].
    – Tetsujin
    Sep 18, 2019 at 16:25
  • 1
    I've read a few things that actually contradict what I thought about MATLAB not being able to handle alpha channels! However, for the OP's question, a black/white layer mask type still seems to be the best way to go. He's just trying to indicate which pixels are to be included in a selection. Sep 18, 2019 at 16:45
  • I don't know Matlab at all, but just as a Photoshop exercise, it's select, bucket, click, save - or probably 'export for web' to get a wysiwyg png file… 'cos Save As makes you discard layers before you can flatten it, which is just a bit fiddly,
    – Tetsujin
    Sep 18, 2019 at 16:59
  • @Tetsujin Thanks for mentioning this could be made an Action if I want to automate this later
    – KAE
    Sep 19, 2019 at 12:13

The most elegant solution would be to create a layer mask-type image (exported as PNG, to avoid compression artifacts that JPEG would introduce), that MATLAB can easily understand.

A simple, single-layer image which clearly shows the state of each pixel (i.e. either "selected" or "not selected") using black or white pixels would work.

My answer is similar to Tetsujin's, except I would suggest using only black and white here (no image data necessary.)

Using the same selected area of the canvas:

enter image description here

Our exported PNG would look like this (minus the red border):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.