PSD-parser

09.07.2007 Alexey Kviring

Photoshop with its PSD files is a standart de-facto. The game wants to read all that additional parameters, like pivot point, slices and so on. We decided to use separate layers to store this info. Then is came to a need of PSD-parser. We did’t found any real quality parsers, so we made it by ourselves in a week.

Here is an example of using this neat thing:

PSDParser parser = new PSDParser(new FileInputStream("image.psd")); 
PSDLayerAndMask layerAndMask = parser.getLayerAndMask();    
 
List layers = layerAndMask.getLayers(); 
List images = layerAndMask.getImageLayers(); 
int i = 0; 
for (PSDLayerStructure layer : layers) { 
    PSDLayerPixelData pixelData = images.get(i); 
    BufferedImage image = pixelData.getImage(); 
    if (image != null) 
	ImageIO.write(image, "png", new File(layer.getName() + ".png")); 
	    i++; 
}

Sourse

Sourse can be downloaded freely under APACHE LICENSE.
Features: all layers export (incl. main), main file info, full slices support.
Use this on your own risc, and we’ll be glad to hear any comments too (bugs, ideas).

psd_parser.7z

16 Responses to “PSD-parser”

  1. Niha da PeaceKeeper Says:

    I was looking for a real FREE-OF-CHARGE PSD parser and finally I found it ! This is such a BIG gift which you guys ever created. This would save me and others from reinvent the wheel.

    I was using the ImageRO to deal with the PSD parser but it’s messages bout something like “liences”,”expired”… made me sick. I was intend to read and write my own parser but bad news is that I had no documents bout the PSD at all… Thank and double, triple thanks you again, you guys lightened up my cruel, miserable life. Keep up the good works, hope you all the best.

  2. makc Says:

    thanks Niha I would have never seen this without you

  3. SkyPainter Says:

    Niha… how did you download the source? I would like to give this a try, but I don’t know what that .7z file type is. Any help is welcome.

  4. Mikhail Fominykh Says:

    SkyPainter, 7z is an archive format. Check this site: http://www.7-zip.org/

  5. SkyPainter Says:

    Thanks for the info Mikhail.

    On another note, this is pretty cool. How can I apply layer effects to the parsed layer image? I see how the image is extracted in the Image Resource Blocks, however I can’t figure out how I could apply an effects layer to an image resource block. For example, create a text layer in a psd file and then apply a drop shadow effect to the text layer. The image that gets parsed out has the text, however, the drop shadow is not in the image itself. How can I apply the drop shadow to the image in the resource block? Any help is appreciated guys.

    SkyPainter

  6. Mikhail Fominykh Says:

    It seems like the only way is to merge (or rasterize, I’m not quite familiar with Photoshop) an effect with the layer. This parser is very simple, it just extracts layers from a psd-file.

  7. Carlos Says:

    Does anybody knows how to implement this “rasterize” ?
    I really need the image resource plus the layer effects…

  8. Web Developer Says:

    Error by loading large PSD file (10 Mb):

    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 786
    at com.alternativagame.resource.utils.psd.images.unpacker.RLEPackBits.unpack(RLEPackBits.java:45)
    at com.alternativagame.resource.utils.psd.images.RLEParser.(RLEParser.java:49)
    at com.alternativagame.resource.utils.psd.layer.PSDLayerPixelData.(PSDLayerPixelData.java:44)
    at com.alternativagame.resource.utils.psd.section.PSDLayerAndMask.parseLayerInfo(PSDLayerAndMask.java:65)
    at com.alternativagame.resource.utils.psd.section.PSDLayerAndMask.(PSDLayerAndMask.java:34)
    at com.alternativagame.resource.utils.psd.PSDParser.(PSDParser.java:40)

  9. Meir Says:

    How do I run it please?

  10. Dan T. Says:

    Excellent, thanks. BTW, I did get an error when parsing a 4-character layer name and fixed it by changing

    length = (length % 2) == 0 ? 1 : length;

    to

    length += 1 - length % 2;

    in the PSDImageResources constructor.

  11. Fabrice Says:

    Hello everybody, how can i read the size( Height and Widht) of a layer ?
    Thank you

  12. Fabrice Says:

    I need to read the contents of the psd file, create the XML description of each layer(position, height, widht, name,…)
    Thank u

  13. PS CS5 Rocks Says:

    I got my all new cs5 n its damn cool,thnx a lot for giving something more to play with.Although its bit advanced.

  14. Jorge Says:

    Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
    at types.VString.(VString.java:26)

    Just as soon as I load a file with 2 layers. Only 1.04 mb

    Thanks

  15. Miper Says:

    Awesome, thanks a lot!

  16. Vladimir Says:

    I think you should create a project on some free project hosting service. I’m sure there is developers who would like to participate in PSD reader development. Sorry for my bad english :)

Leave a Reply

Powered by WP Hashcash