php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #59606 CMYK inverts colors
Submitted: 2011-02-03 12:06 UTC Modified: 2015-01-05 02:50 UTC
Votes:31
Avg. Score:4.6 ± 0.7
Reproduced:26 of 27 (96.3%)
Same Version:18 (69.2%)
Same OS:11 (42.3%)
From: info at riccardobianconi dot it Assigned: mkoppanen (profile)
Status: Not a bug Package: imagick (PECL)
PHP Version: 5.3.3 OS: Linux Ubuntu 10.10
Private report: No CVE-ID: None
 [2011-02-03 12:06 UTC] info at riccardobianconi dot it
Description:
------------
I'm trying to create a CMYK .pdf containing an image.

I've tried my script on different servers with different imagick versions and it worked fine, on mine it inverts the color.

Ubuntu 10.10, PHP 5.3.3, Imagick 3.0.1 installed via PECL (on Ubuntu repository the latest version available is the 3.0.0RC1)

If I use the RGB colorspace the colors are correct.

Reproduce code:
---------------
$im = new Imagick();
$im->setResolution(300, 300);
$im->newImage(1000, 1000, new ImagickPixel('rgba(0,0,0,0)'), 'pdf');
	
$draw = new ImagickDraw();
$draw->setFillColor('#FF0000');
$draw->rectangle(100, 100, 200, 200);
$im->drawImage($draw);

$im->setImageProfile('CoatedFOGRA39', 'CoatedFOGRA39.icc');	
$im->setImageColorspace(Imagick::COLORSPACE_CMYK);
$im->writeImage('image.pdf');

Expected result:
----------------
A .pdf in CMYK colorspace.

Actual result:
--------------
A .pdf in CMYK colorspace but with colors inverted.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-07 12:07 UTC] paul dot ago at gmail dot com
Same happens to me when trying to convert a EPS CKYK image to 
JPG RGB, while converting to PNG or JPG CMYK are both fine
 [2011-07-04 10:54 UTC] bernie dot spam at gmx dot at
Also to me: 

$imagick = new Imagick();
$imagick->readImage("../myimg.jpg"); // is a CMYK image
$imagick->setimagecolorspace(Imagick::COLORSPACE_RGB);
$imagick->writeImage("rgb.jpg");

PHP 5.3.5
imagick 3.1.0b1
ImageMagick 6.6.0-4
 [2011-08-10 11:19 UTC] jeffmlevy at Gmail dot com
It would appear as if this issue goes back to 3.0.0~rc1-
1build1, which I have running on Ubuntu 11.04, natty.

Same issues applies: When trying to set colorspace to RGB, 
resulting output image colorspace flag is set, however, 
colors remain CMYK.
 [2012-03-06 04:27 UTC] nidharshi dot warusavith at bizexpress dot com dot au
I have the same Issue. Has this been solved?
I have the following versions:
Imagick 6.5.7-8 2010-12-02 Q16 
PHP 5
Image colors are sort of black. How do I fix this?
 [2012-03-15 17:02 UTC] spam at ethaniel dot com
When converting from CMYK to RGB using this function, the image can become 
inverted. To fix this, use a workaround (don't forget to download the .icc files 
online):

// don't use this (it inverts the image)
//    $img->setImageColorspace (imagick::COLORSPACE_RGB);

if ($img->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
    $profiles = $img->getImageProfiles('*', false);
    // we're only interested if ICC profile(s) exist
    $has_icc_profile = (array_search('icc', $profiles) !== false);
    // if it doesnt have a CMYK ICC profile, we add one
    if ($has_icc_profile === false) {
        $icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc');
        $img->profileImage('icc', $icc_cmyk);
        unset($icc_cmyk);
    }
    // then we add an RGB profile
    $icc_rgb = 
file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
    $img->profileImage('icc', $icc_rgb);
    unset($icc_rgb);
}

$img->stripImage (); // this will drop down the size of the image dramatically 
(removes all profiles)
 [2013-02-28 21:07 UTC] marcelojfnts at gmail dot com
Same Issue...
Software versions:
Imagick 3.1.0RC2 
ImageMagick-6.8.3-6 
PHP 5.4.11
Centos 6.3
 [2013-03-14 22:30 UTC] spam at dranes dot com
The solution of ethaniel dot com works like a charm it is the best solution i've 
found so far and the icc files can be downloaded from here 
https://github.com/vivid-
planet/library/blob/master/icc/sRGB_v4_ICC_preference.icc
 [2013-06-15 15:50 UTC] kevin dot a dot florida at rrd dot com
I SOLVED THIS PROBLEM.

My solution is at stackoverflow.com :

http://stackoverflow.com/questions/15587718/convert-image-from-rgb-to-cmyk-with-imagick/15587798#15587798

You need to negate the image while on php 5.3.3
 [2015-01-02 19:16 UTC] Danack at basereality dot com
@bernie dot spam at gmx

I believe you may have meant to use transformImageColorspace http://www.phpimagick.com/Imagick/transformImageColorspace

I believe setImageColorSpace is meant for new images not for existing ones.

@everyone else

I can't see an issue with the output of the images. CMYK is a subtractive color model so it is to be expected that drawing a 'red' square on an image produces a result that looks 'cyan' colored.
 
This is the behaviour that is seen when running ImageMagick directly on the command line:

convert -size 400x400 xc:white -set colorspace CMYK -fill red -stroke blue -draw "rectangle 50,50 200,200" cmykConvert.png

This produces an image that is similarly inverted.

If you think there is still an issue, please can you test against just ImageMagick using the command line convert program to see what image it creates. If Imagick produces the same result as ImageMagick on the command line, there is either a bug in the underlying ImageMagick library, or you've misunderstood how colorspaces work.
 [2015-01-05 02:50 UTC] danack@php.net
-Status: Assigned +Status: Not a bug
 [2015-01-05 02:50 UTC] danack@php.net
Not a bug, CMYK is subtractive color model, RGB is an additive color model.
 [2016-09-28 12:59 UTC] mpoiriert at gmail dot com
Using 

convert source.jpg -profile ./color-profiles/CMYK/CoatedFOGRA39.icc target.jpg

Does generate a CMYK jpeg that display proper color (the same way when my graphic designer give me a image it's not inverted).

The inverted image generated by the php library if open with the ImageMagick viewer will look ok, probably printing will be fine but obviously the "convert" end result is better since it does display properly everywhere.
 [2016-09-28 13:14 UTC] mpoiriert at gmail dot com
Sorry I missed the -colorspace parameter in the "convert" command

 convert couv_preview_title.jpg -colorspace CMYK  -profile ./color-profiles/CMYK/CoatedFOGRA39.icc converted.jpg

End result seems the same so it's probably good.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC