Page 1 of 1

Need help converting PSD layers to individual images.

Posted: 2007-03-01T22:05:10-07:00
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!

Re: Need help converting PSD layers to individual images.

Posted: 2007-03-02T06:10:44-07:00
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.

Re: Need help converting PSD layers to individual images.

Posted: 2007-03-02T07:28:54-07:00
by NoMonkey
Well that's a bummer! I guess I'll check around a bit and see if I can find another solution.

Thanks!

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-14T23:22:01-07:00
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.

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-14T23:59:20-07:00
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

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T11:35:52-07:00
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

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T12:08:23-07:00
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.

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T13:57:27-07:00
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

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T14:03:26-07:00
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;
}

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T14:13:05-07:00
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.

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T14:48:35-07:00
by snibgo
Correct. "identify" says your file has three layers. The first has no label.

Re: Need help converting PSD layers to individual images.

Posted: 2013-09-15T15:53:30-07:00
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