Photoshop Toolbox examples

Example -- Open and modify a document

The power of Photoshop is the ability to open many different file formats. The PSD file format is in particular a difficult nut to crack. In this example we will: open a PSD file (Native Photoshop file format), get the pixels into MATLAB, run an average on each channel, and finally set the new pixels back to Photoshop.

     psopendoc('C:\Program Files\Adobe\Adobe Photoshop CS4\Samples\Vanishing Point.psd');
     p = psgetpixels();
     p(:,:,1)=mean(mean(p(:,:,1)));
     p(:,:,2)=mean(mean(p(:,:,2)));
     p(:,:,3)=mean(mean(p(:,:,3)));
     pssetpixels(p);

Example -- Layered documents

I picked an easy PSD in the example above. The Vanishing Point file only contains one layer. Run the same example above only use 'Fish.psd' for the file name. It appears that nothing happened because the file opens on a layer with not much data on it. The image looks the same. One option is to flatten the document. psjavascript('activeDocument.flatten()'); Another, more interesting, option is to move to the layer with some interesting pixels.

     psopendoc('C:\Program Files\Adobe\Adobe Photoshop CS4\Samples\Fish.psd');

     ln = pslayernames();
     pssetactivelayer(ln{length(ln)});

     p = psgetpixels();
     p(:,:,1)=mean(mean(p(:,:,1)));
     p(:,:,2)=mean(mean(p(:,:,2)));
     p(:,:,3)=mean(mean(p(:,:,3)));
     pssetpixels(p);

Example -- Transparency

The 'Fish.psd' example has a lot of layers with little information on them. We will use that example to show how to deal with transparency. Run the above example with one minor change. pssetactivelayer(ln{length(ln)-1}); I will pick the layer above the background layer, 'PurpleFish'.

     psopendoc('C:\Program Files\Adobe\Adobe Photoshop CS4\Samples\Fish.psd');

     ln = pslayernames();
     pssetactivelayer(ln{length(ln)-1});

     p = psgetpixels();
     p(:,:,1)=mean(mean(p(:,:,1)));
     p(:,:,2)=mean(mean(p(:,:,2)));
     p(:,:,3)=mean(mean(p(:,:,3)));
     pssetpixels(p);

Running the code above gets us white fish! I was expecting an average purple color of fish. What happened? The 'p' matrix we got back was for the width x height of the document. Most of the layer is transparent and the values we averaged are not actually visible. We need to only average the pixels that we see. We need the transparency channel. t = psgetpixels(16); Read the documentation on how I got the magic '16' number. Now we do it again but only average pixels that are not transparent.

     psopendoc('C:\Program Files\Adobe\Adobe Photoshop CS4\Samples\Fish.psd');

     ln = pslayernames();
     pssetactivelayer(ln{length(ln)-1});

     p = psgetpixels();
     t = psgetpixels(16);
     index=find(t(:)>0);

     c=p(:,:,1);
     c(index)=mean(mean(c(index)));
     p(:,:,1)=c;

     c=p(:,:,2);
     c(index)=mean(mean(c(index)));
     p(:,:,2)=c;

     c=p(:,:,3);
     c(index)=mean(mean(c(index)));
     p(:,:,3)=c;

     pssetpixels(p);

Good! Nice boring purple fish.

Also note the matrix we get back from pssetpixels(). The return value changes depending on the document. For our first example it was 540x360x3 uint8 as the document has three channels and a bit depth of 8. For the second example it was 257x400x3 uint16 as the document was three channels and a bit depth of 16. CMYK and Grayscale images will return 4 and 1 for the third size respectively. The class will be uint8, uint16, or double depending on the image depth.

© 2008 Adobe Systems, Inc.