Need help converting PSD layers to individual images.

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
NoMonkey

Need help converting PSD layers to individual images.

Post by NoMonkey »

I'm not quite sure what I'm doing wrong here. I'm trying to load a PSD and save each of it's layers as individual images. If I attempt to output PSDs for each layer all I get is black but if I output in any other format, I sometimes get something that resembles the layer, but has diagonal stripes of colors all over.

Here is the code I am using:

Code: Select all

int main( int argc, char ** argv) 
{ 
	Magick::InitializeMagick(*argv);

	std::string filePath = "C:\\test.psd";
	std::string fileName = filePath;
	RemoveFileNamePath( fileName );
	RemoveFileNameExtension( fileName );

	ImageInfo* image_info = CloneImageInfo((ImageInfo *) NULL);
	strncpy_s(image_info->filename, filePath.c_str(), MaxTextExtent - 1);

	ExceptionInfo exception;
	GetExceptionInfo(&exception);

	printf( "Processing File: %s\n\n", filePath.c_str() );

	Image* image = ReadImage( image_info, &exception );

	// Loop through all the layers
	for ( ; image != (Image *)NULL; image = GetNextImageInList(image) )
	{
		// Get layer name
		const char* szLayerName = GetImageProperty( image, "label" );

		if( !szLayerName || szLayerName[0] != '.' )
			continue;

		std::string outFileName( fileName );
		outFileName += szLayerName;

		std::string outFilePath( "C:\\" );
		outFilePath += outFileName;
		outFilePath += ".tif";
		std::transform( outFilePath.begin(), outFilePath.end(), outFilePath.begin(), tolower ); 

		strncpy_s( image->filename, outFilePath.c_str(), MaxTextExtent - 1 );
		strncpy_s( image_info->filename, outFilePath.c_str(), MaxTextExtent - 1 );

		printf( "\tSaving layer %s as file %s... ", szLayerName, outFilePath.c_str() );

		// Write out layer.
		WriteImage( image_info, image );

		printf( "Complete\n" );
	}

	/* Clean up */

	DestroyImageInfo( image_info );
	(void)DestroyExceptionInfo( &exception );
	DestroyMagick();

	printf( "\nProcessing Complete.\n\n" );

	system("PAUSE");

	return 0;
}
All this does is search for any layers that begin with a period and attempt to save that layer out as <file name>.<layer name>.<extension>.

Any help would be appreciated!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Need help converting PSD layers to individual images.

Post by magick »

We suspect ImageMagick cannot properly read some of your PSD layers. We tried to get the PSD image specification from Adobe (about a year ago) to improve support within ImageMagick but they never got back to us.
NoMonkey

Re: Need help converting PSD layers to individual images.

Post by NoMonkey »

Well that's a bummer! I guess I'll check around a bit and see if I can find another solution.

Thanks!
tgraupmann
Posts: 5
Joined: 2013-09-14T23:19:21-07:00
Authentication code: 6789

Re: Need help converting PSD layers to individual images.

Post by tgraupmann »

I'm doing the same thing to get the PSD layer names but it just returns null. Has there been any progress on this?

GetImageProperty( image, "label" )

I'm using ImageMagick-6.7.8.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help converting PSD layers to individual images.

Post by snibgo »

You might try a more recent version of IM. This works from the command line, v6.8.6-9:

Code: Select all

identify -format "%[label]\n" testLayers.psd
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need help converting PSD layers to individual images.

Post by fmw42 »

snibgo wrote:You might try a more recent version of IM. This works from the command line, v6.8.6-9:

Code: Select all

identify -format "%[label]\n" testLayers.psd

Odd! %[label] is not listed in the list of string formats at http://www.imagemagick.org/script/escape.php
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help converting PSD layers to individual images.

Post by snibgo »

True, so perhaps the documentation could be updated. It does mention %l, which also works. Both %l and %[label] have worked (on a sample PSD file I have) since IM v6.5.8-8, which is the oldest version I have.
snibgo's IM pages: im.snibgo.com
tgraupmann
Posts: 5
Joined: 2013-09-14T23:19:21-07:00
Authentication code: 6789

Re: Need help converting PSD layers to individual images.

Post by tgraupmann »

No luck upgraded to 6.8.6 and the label still returns null.

while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
{
const char* label = GetImageProperty(image,"label"); // WHY NULL?????????
resize_image=ResizeImage(image,dstWidth,dstHeight,LanczosFilter,1.0,exception);
if (resize_image == (Image *) NULL)
MagickError(exception->severity,exception->reason,exception->description);
(void) AppendImageToList(&thumbnails,resize_image);

DestroyImage(image);
++index;
}

I'll link my reference image:

http://tagenigma.com/imagemagick/layertest.psd

The layers should be:

Background
Layer 2
tgraupmann
Posts: 5
Joined: 2013-09-14T23:19:21-07:00
Authentication code: 6789

Re: Need help converting PSD layers to individual images.

Post by tgraupmann »

There we go. Somehow resizing the image lost the layer name. If I skipped the resize, I can get the name just fine.

Thanks!

while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
{
if (layer < 0 ||
layer >= count ||
index == layer)
{
const char* label = GetImageProperty(image,"label");
if (label)
{
printf("Label: %s\r\n", label);
}
else
{
printf("Label: None\r\n");
}
(void) AppendImageToList(&thumbnails,image);
}
++index;
}
tgraupmann
Posts: 5
Joined: 2013-09-14T23:19:21-07:00
Authentication code: 6789

Re: Need help converting PSD layers to individual images.

Post by tgraupmann »

It's possible that the first layer is the composite layer which doesn't have a name. The other layers have names, I may have overlooked that.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help converting PSD layers to individual images.

Post by snibgo »

Correct. "identify" says your file has three layers. The first has no label.
snibgo's IM pages: im.snibgo.com
tgraupmann
Posts: 5
Joined: 2013-09-14T23:19:21-07:00
Authentication code: 6789

Re: Need help converting PSD layers to individual images.

Post by tgraupmann »

Thanks for a bump in the right direction. Everything I aimed to do is working like a charm.

Image
http://www.youtube.com/watch?v=LLKByhchOFc
Post Reply