首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Imagick将图像从RGB转换为CMYK

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

Stack Overflow用户
提问于 2013-03-23 22:09:31
回答 3查看 10.6K关注 0票数 4

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

代码语言:javascript
复制
<?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颜色。

有人知道怎么做吗?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-23 22:19:18

看一看here

代码语言:javascript
复制
<?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) 
?>
票数 3
EN

Stack Overflow用户

发布于 2013-06-15 23:43:53

好的,这是一个棘手的问题。我也有同样的问题,我花了几天的时间才解决了这个问题。你需要negateImage(),请看我的例子,并确保你只对php 5.3.x这样做,因为这个问题是php独有的。

代码语言:javascript
复制
$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上搜索即可。

我希望这会有帮助,请把它标记为正确的答案,因为很多人都有这个问题。

票数 1
EN

Stack Overflow用户

发布于 2016-09-28 13:46:52

可能的原因是RGB是加法配色方案,CMYK是减法配色方案。也就是说,我们可以使用以下公式将RGB转换为CMYK:

代码语言:javascript
复制
C = 255 - R;
M = 255 - G;
Y = 255 - B;

它看起来像是“如何反转颜色”。所以在这种情况下,我们也应该计算出K值。

为了避免这种情况,我们可以使用ICC-profiles

一种可能的解决方案是:

代码语言:javascript
复制
$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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15587718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档