Skip to content

solovev/gopsd

Repository files navigation

gopsd

Photoshop document parser in Golang

Example

package main

import (
	"fmt"
	"image/png"
	"os"

	"github.com/solovev/gopsd"
)

func main() {
	doc, err := gopsd.ParseFromPath("./test.psd")
	checkError(err)

	os.Mkdir("./images", 0777)

	for _, layer := range doc.Layers {
		fmt.Println(layer.ToString())

		saveAsPNG(layer)
	}
}

func saveAsPNG(layer *gopsd.Layer) {
	out, err := os.Create("./images/" + layer.Name + ".png")
	checkError(err)

	img, err := layer.GetImage()
	checkError(err)

	err = png.Encode(out, img)
	checkError(err)
}

func checkError(err error) {
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

test.psd

photoshop

Result

Background: [X: 0, Y: 0, Width: 384, Height: 512]
GrayRect: [X: -1, Y: 153, Width: 349, Height: 145]
RoundPinkRect: [X: 66, Y: 163, Width: 273, Height: 124]
Ellipse: [X: -58, Y: -89, Width: 338, Height: 212]
SOME TEXT: [X: 18, Y: 18, Width: 235, Height: 34]

Layers
Background
Background
GrayRect
GrayRect
RoundPinkRect
RoundPinkRect
Ellipse
Ellipse
SOME TEXT
SOME TEXT