使用Imagick将图像从RGB转换为CMYK

4

我正在尝试将RGB图像转换为CMYK,因为它们需要打印出来。 我正在使用以下代码:

<?php
$filePath = 'rgb.jpg';

// First save image as png
$image = new Imagick($filePath);
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
$image->setImageCompressionQuality(0); 
$image->setImageFormat("png");
$filePath = 'rgb.png';
$image->writeImage($filePath);
$image->clear();
$image->destroy();
$image = null;

// Convert colors
$image = new Imagick($filePath);
$image->stripImage();
$image->setImageColorspace(Imagick::COLORSPACE_CMYK);
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
$image->setImageCompressionQuality(0); 
$image->setImageFormat("png");
$filePath = 'cmyk.png';
$image->writeImage($filePath);

$image->clear();
$image->destroy();
$image = null;


$fileUrl = 'http://www.product-designer.nl/rgb2cmyk/cmyk.png';
?>
CMYK Image:<br/>
<img src="<?php echo $fileUrl; ?>" width="400" /><br /><br />
<?php
$fileUrl = 'http://www.product-designer.nl/rgb2cmyk/rgb.png';
?>
RGB Image:<br/>
<img src="<?php echo $fileUrl ?>" width="400" />

你可以在http://product-designer.nl/rgb2cmyk上查看结果。但是,不知道为什么图像的颜色变成了反色。我需要转换图像,但颜色需要尽可能接近 RGB 颜色。有人知道如何做到这一点吗?谢谢。

我曾经遇到过同样的问题。经过大量的研究和尝试,我解决了这个问题,请查看我的答案。 - Kevin Florida
3个回答

3

点击这里查看相关内容:

<?php 
// 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) 
?>

谢谢你的回答。我实现了你提供的代码,但是得到了以下结果:product-designer.nl/rgb2cmyk 我完全看不出任何区别...我已经将.icc文件添加到正确的目录中。 - Laurens Schuitemaker
对我来说可以工作,但颜色似乎有点苍白。也许是由于使用的配置文件? - Arne Cordes
1
问题是 - "使用Imagick将图像从RGB转换为CMYK"。但是您的解决方案是针对“使用此函数从CMYK转换为RGB时,图像可能会反转。要解决此问题,请使用解决方法”。如果($img->getImageColorspace()== Imagick :: COLORSPACE_CMYK){ - 如果图像已经是CMYK,则为真,但我们需要将RGB转换为CMYK - Yaroslav
这个解决方案(以及来自Kevin Florida的解决方案)对我无效。尝试了各种方法都没有成功。但是这个答案对我有用:http://stackoverflow.com/a/17933051/2404541 - TheStoryCoder
1
-1:抱歉,但您需要使用 transformimagecolorspace 处理现有图像:http://php.net/manual/en/imagick.transformimagecolorspace.php。 - WonderLand
显示剩余3条评论

1

好的,这是一个棘手的问题。我也遇到了同样的问题,花了几天时间才解决。您需要使用negateImage()函数,请参考我的示例,并确保仅在php 5.3.x中执行此操作,因为此问题仅适用于该版本的php。

$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();

注意:我的Imagick对象显然是$jpeg
另外,你需要从Adobe网站下载AdobeRGB1998.icc配置文件,只需在Google上搜索即可。
希望这可以帮到你,请将其标记为正确答案,因为很多人都遇到了这个问题。

0
可能的原因是RGB是加色模式,而CMYK是减色模式。也就是说,我们可以使用以下公式将RGB转换为CMYK:
C = 255 - R;
M = 255 - G;
Y = 255 - B;

看起来像是“如何反转颜色”的问题。 因此,在这种情况下,我们还应该找出K值。

为了避免这种情况,我们可以使用ICC配置文件

可能的解决方案之一:

$image = new Imagick('img/test.jpg');
$profiles = $image->getImageProfiles('*', false);
if (!array_search('icc', $profiles)) {
    // Without this code Photoshop cannot open image with original icc-profile
    $icc_rgb = file_get_contents('profiles/AppleRGB.icc');
    if ($image->profileImage('icc', $icc_rgb))
        echo '<br>Changed!';
    unset($icc_rgb);
}
// don't use this code, it leads to inverted image
//$image->setImageColorspace(Imagick::COLORSPACE_CMYK);
// tiff format supports CMYK colorscheme
$image->setFormat('tiff');
$image->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
$image->setImageCompressionQuality(0);
// We can download profiles here http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=4075&fileID=3790
$icc_cmyk_profile_path='profiles/USWebCoatedSWOP.icc';

$icc_cmyk = file_get_contents($icc_cmyk_profile_path);
if ($image->profileImage('icc', $icc_cmyk))
    echo '<br>Changed!';
unset($icc_cmyk);
// Drops all profiles, so we comment it
//$image->stripImage();
if ($image->writeImage('img/test_cmyk.tiff'))
    echo '<br>Save!';
$image->clear();
$image->destroy();

文件大小已更改,例如从72Kb变为1.4Mb。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接