setColorSpace RGB results in negative image

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
adrianj

setColorSpace RGB results in negative image

Post by adrianj »

I am having some weird results trying to ensure that my PDF to JPG conversion results in RGB JPGs. If the PDF is RGB, then everything is fine, but if it is CMYK, then the resulting JPG looks like a negative. Without the setImageColorSpace, the JPG works fine (although it has a pinkish hue to it), but IE complains and won't display CMYK images, so I need to get them as RGB. If I do a "mogrify -colorspace RGB" on the CMYK JPG that was created via imagick, then it works fine, so I am thinking there is something wrong with imagick. This is what I am doing:

$im = new imagick();
$im->setOption("pdf:use-cropbox","true");
$im->setResolution(288,288);
$im->readImage($pdf_filepath);
$geometry=$im->getImageGeometry();
$width = ceil($geometry['width'] / ($resolution/72));
$height = ceil($geometry['height'] / ($resolution/72));

$im->setImageColorSpace(imagick::COLORSPACE_RGB);
$im->setImageFormat("jpg");

$im->scaleImage($width, $height);
$im->writeImage($jpg_filepath);




I even tried creating the CMYK JPG, and then simply doing this:

$im = new imagick($jpg_filepath);
$im->setImageColorSpace(imagick::COLORSPACE_RGB);
$im->writeImage($jpg_filepath);

but this has the same result - the image looks like a negative.

Any ideas please? Do I have something wrong with my approach?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: setColorSpace RGB results in negative image

Post by fmw42 »

I don't use any API's but in command line, when reading a cmyk pdf, you need to set the colorspace to RGB before reading the pdf.
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

Thanks for the suggestion, but I tried that as one of my many combinations and it results in this error message:

PHP Fatal error: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object' in .....

I almost wonder whether there needs to be setColorSpace rather than the setImageColorSpace so that it can set the color space of the object that the PDF will be placed into.

Surely there must be someone out there who has dealt with this :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: setColorSpace RGB results in negative image

Post by fmw42 »

did you do

$im = new imagick();
$im->setImageColorSpace(imagick::COLORSPACE_RGB);
...
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

This is the order of what I tried

$im = new imagick();
$im->setOption("pdf:use-cropbox","true");
$im->setResolution($resolution,$resolution);
$im->setImageColorSpace(Imagick::COLORSPACE_RGB);
$im->readImage($pdf_filepath);
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: setColorSpace RGB results in negative image

Post by fmw42 »

I really don't know the Imagick API well, but it looks reasonable. Sorry I am out of suggestions.

check out http://us2.php.net/manual/en/function.i ... rspace.php

Looks like it wants an integer for the colorspace

$im->setImageColorSpace(1);
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

I really appreciate all your thoughts - it is a shame that there is not a more active support forum for imagick.

I have tried the integer option already - from what I can tell, it can accept both options). Unfortunately it results in the negative looking image as well. I even tried 'negate' in an effort to convert the output back!

Guess I'll wait and hope someone else stumbles across this. I am beginning to think it is a bug in imagick. I tried upgrading to the new RC, but no change.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: setColorSpace RGB results in negative image

Post by fmw42 »

Only other thing is to try sending PM directly to the developer (mkoppanen?)
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

I am always hesitant to do that, but in this case I think you're right - I just PM's Mikko - if he gets back to me by PM, rather than here I'll post the resolution.
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: setColorSpace RGB results in negative image

Post by mkoppanen »

Hello,

looks like set/getColorSpace is missing from Imagick which are needed for setting colorspace before reading an image. I'll add these methods tomorrow. Can someone confirm in which version of ImagickMagick they were added or have I just missed them from the start?
Mikko Koppanen
My blog: http://valokuva.org
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

Mikko,

Thanks for taking care of this so quickly. Glad to hear I wasn't missing something and that does definitely explain things - before reading the image made sense, and I figured setColorspace would be needed for that.

Not sure when it was made available, but -colorspace is referenced in the changelog back in June 2008:

2008-06-03 6.4.1-6 Cristy <quetzlzacatenango@image...>
# The -colorspace option is an operator, not a setting.

Thanks again,
Adrian
olwildcory723

Re: setColorSpace RGB results in negative image

Post by olwildcory723 »

I too am in need of changing colorspaces. I receive JPEG images in CMYK, ProPhoto etc. colorspaces and I need to convert them to sRGB colorspace. Has there been a resolution to this? Do I need to re-install anything? Thanks in advance :)

Cory
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

Looks like we are still waiting on Mikko to implement this. I actually just took a look at MagicWand and they don't appear to have implemented setColorspace yet either (at least according to the docs). This is definitely making me thing I am going to have to start running exec to get this working.I could live with cmyk, but IE won't display them!
adrianj

Re: setColorSpace RGB results in negative image

Post by adrianj »

Thanks to Mikko, setColospace has been added to the SVN and is working great for me now!
Post Reply