Photoshop + MATLAB tools

Language: MATLAB

Last modified: 17 May 2009

As I discussed in a recent blog, I use the MATLAB integration features of Photoshop quite a bit. The two commands I use most often are psnewdocmatrix to send an image from MATLAB to Photoshop and psgetpixels to get the pixels of the current layer back into MATLAB. But these commands do not have all the features I need, so I wrote new versions with more features: psnew and psget. Get the files here:


I had two main goals for a better version of psnewdocmatrix: the ability to send an alpha channel and automatic scaling and conversion of double-precision images to an integer format. I had been previously sending the alpha channel to a separate image then using Photoshop to paste it on the original image; it was a several step process. The scaling/conversion issue is also convenient because the default image representation in MATLAB is double-precision, but many Photoshop tools are not available for floating-point images.

Here are some usage examples:

>> psnew(im)
>> psnew(im,alpha)

In this case, the variable im can be an image of the following types: logical, uint8, uint16, single, or double. The optional alpha channel will be set as the image’s transparency channel.

Floating-point images are automatically auto-scaled and converted to uint16. To open a floating-point image as 32-bit in Photoshop or to turn off the scaling, add the following optional arguments:

>> psnew(im,'float')
>> psnew(im,'noscale')

If the float option is given, a floating-point image will not be autoscaled. To enable scaling in this case, pass two options to the function:

>> psnew(im,'float','scale')


The psget function retrieves the image pixels from the current layer. The basic usage is simply:

>> im = psget();
>> [im,alpha] = psget();

The image can be converted to a different type by passing on of the following image types as an argument: logical, uint8, uint16, single, or double.

>> im = psget('double');

The psget function can get the current selection or a quick mask instead of the alpha channel. These options are specified as additional arguments and the special channel will be returned as the second output argument.

>> [im,mask] = psget('select');
>> [im,mask] = psget('quick');

Photoshop 16-bit

While MATLAB’s uint16 image format ranges from 0 to 65535, Photoshop’s 16-bit format is actually 15-bits: the image values range from 0 to 32768. To demonstrate this, open an image in Photoshop, convert it to 16-bits then save it as a PNG. Open the image in MATLAB in two different ways and look at the minimum and maximum values:

>> im1 = imread('test.png');
>> im2 = psgetpixels();
>> [min(im1(:)) max(im1(:)); min(im2(:)) max(im2(:))]
ans =
      0  65535
      0  32768
>> psnewdocmatrix(im1)

You can see that they are different and if you execute the last command, you will get a clipped image in Photoshop. To avoid these issues, both functions automatically scale the image appropriately when transferring uint16 images between MATLAB and Photoshop. The example from above now works as expected:

>> im1 = imread('test.png');
>> im3 = psget();
>> [min(im1(:)) max(im1(:)); min(im3(:)) max(im3(:))]
ans =
      0  65535
      0  65535
>> psnew(im1)