urway
urway

Reputation: 93

How to Convert PSD into PNG, using Python Wand / ImageMagick?

I tried converting PSD to PNG, but as I save the PNG, Wand (wand-0.4.0-py2.7) spits out all layers inside the PSD as separate PNG files (the last file saved is the one I want). So how to correctly convert from PSD to PNG?

# Convert a large PSD to small PNG thumbnail
myImage = Image(filename="test.psd")
myImage.resize(100, 100)
myImage.format = "png"
myImage.save(filename="test.png")

found related solution:

but not sure how it can be done in Python (I'm new to Python)

Any help is appreciated.

Upvotes: 2

Views: 5880

Answers (1)

urway
urway

Reputation: 93

This would work just well and fast for both read and write:

# Convert a large PSD to small PNG thumbnail
myImage = Image(filename="test.psd[0]")
myImage.format = "png"
myImage.sample(100, 100)
myImage.save(filename="test.png")

Because Wand is only reading 1 layer instead of all the layers inside the PSD.

  • the key is to append [0] to the filename string.
  • after testing, [0] will get the final composited image from PSD.
  • and all other numbers don't seem to make a clear sense as to which layers they represent. in my case, [1] is the 2nd layer from the bottom, and [2] the 3rd, etc. so probably a good idea not to extract layers that way.

For more complicated operations with PSD files, you may consider Python psd-tools (https://pypi.python.org/pypi/psd-tools). but from my initial testing, it reads PSD rather slow (maybe because it reads the entire PSD).


Additional Application with Qt

It'd be useful if you want to load PSD (or other image formats) into Qt UI. The concept is to convert image loaded by Wand into image string, and send that image string to QImage.

# load the image first
myImage = Image(filename=r"c:\test.psd[0]")
myImage.format = "png"
myImage.sample(100, 100)
aBlob = myImage.make_blob()

# load the image data into QImage
myQImage = QtGui.QImage.fromData( aBlob )
myQPixmap = QtGui.QPixmap.fromImage( myQImage )
target_QLabel.setPixmap( myQPixmap )

Upvotes: 2

Related Questions