RGB to CMYK inverts?

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
shmk
Posts: 3
Joined: 2011-01-28T08:47:01-07:00
Authentication code: 8675308

RGB to CMYK inverts?

Post by shmk »

I'm trying to convert a RGB .gif to a CMYK .gif using IMagick.

I've wrote this piece of code

Code: Select all

$i = new Imagick('mosaique.gif');
$i->setImageColorspace(Imagick::COLORSPACE_CMYK);
$i->setImageFormat('gif');
$i->writeImage('mosaique-cmyk.gif');
But the resultant "mosaique-cmyk.gif" still a RGB... but with inverted colors :shock:

EXAMPLE FILES ATTACHED!

What am I doing wrong?
Attachments
CMYK
CMYK
mosaique-cmyk.gif (11.2 KiB) Viewed 18302 times
RGB
RGB
mosaique.gif (11.2 KiB) Viewed 18302 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: RGB to CMYK inverts?

Post by fmw42 »

does gif support cmyk? try jpg
shmk
Posts: 3
Joined: 2011-01-28T08:47:01-07:00
Authentication code: 8675308

Re: RGB to CMYK inverts?

Post by shmk »

fmw42 wrote:does gif support cmyk? try jpg
I've tried with a jpg

Code: Select all

$i = new Imagick('mosaique.jpg');
$i->setImageColorspace(Imagick::COLORSPACE_CMYK);
$i->setImageFormat('jpg');
$i->writeImage('mosaique-cmyk.jpg');
it's converted to CMYK but it is likes in negative :|
Attachments
CMYK
CMYK
mosaique-cmyk.jpg (158.64 KiB) Viewed 18281 times
RGB
RGB
mosaique.jpg (189.7 KiB) Viewed 18281 times
shmk
Posts: 3
Joined: 2011-01-28T08:47:01-07:00
Authentication code: 8675308

Re: RGB to CMYK inverts?

Post by shmk »

An important update!

I've tried to run my script making a .pdf on another server and it works fine.

Are there any known bug in IMagick?
Are there some options to set in the php5 library?

The version that returns me the inverted image is newer than the one that works correctly

WRONG RESULT
PHP 5.3.3
IMagick 3.0.0RC1
ImageMagick 6.6.2

CORRECT RESULT
PHP 5.2.10
IMagick 2.1.1
ImageMagick 6.5.1
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: RGB to CMYK inverts?

Post by fmw42 »

best to use profiles for conversion from rgb to cmyk

see profiles at http://www.imagemagick.org/Usage/formats/#profiles
kevin.florida
Posts: 1
Joined: 2013-06-15T08:56:18-07:00
Authentication code: 6789

Re: RGB to CMYK inverts?

Post by kevin.florida »

I HAVE A SOLUTION!
After 2 days of hacking and intensive research I have solved the problem. It was a very frustrating issue, so I wanted to make sure I share the solution.

Note: my Imagick object is $jpeg and you have to download the adobe AdobeRGB1998.icc profile from their website (do a google search).

Only on 5.3.x you have to negate the converted image, see my solution:

Code: Select all

			$range = $jpeg->getQuantumRange();
			$php_vs_arr = preg_split("/\./", phpversion());
			$php_vs = $php_vs_arr[0] . '.' . $php_vs_arr[1];
			if ($jpeg->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
					
				//make sure cmyk color-space is set correctly
				$jpeg->setImageColorspace(12);
					
				// then we add an RGB profile
				$icc_rgb = file_get_contents(FRAMEWORK_PATH . DS . 'color' . DS . 'AdobeRGB1998.icc');
				$jpeg->profileImage('icc', $icc_rgb);
				unset($icc_rgb);
					
				//set color space to rgb
				$jpeg->setImageColorspace(13);
			
				//fix gamma, hue, saturation, brightness
				if($php_vs < 5.3) {
					//ADJUST GAMMA BY 2.0 for 5.2.x
					$jpeg->levelImage(0, 2.0, $range['quantumRangeString']);
				} else {
					//php 5.3 hack FOR INVERTED COLORS
					$jpeg->negateImage(false, Imagick::CHANNEL_ALL);
				}
					
			}
			$jpeg->stripImage();
			//end convert to RGB=========================|
Post Reply