converting rgb image to cmyk using Imagick - php

I have image that i want to convert from rgb to cmyk using color profile.
Here is the image that i am using
And the peace of code that i am using to convert into cmyk.
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage($image);
$IMagick->transformImageColorspace(12);
$icc_cmyk = file_get_contents("USWebUncoated.icc");
$IMagick->profileImage('icc', $icc_cmyk);
$IMagick->writeImage ($image2);
And here is the output image that i got.
I then checked color space and yes it is cmyk, but you can mark that green color is change though. Am i doing anything wrong here?

It is known that converting RGB to CMYK can change colors. Also browsers hae challenges rendering CMYK images properly. There is nothing you are doing wrong.

Related

I create a cmyk image by php imagemagick, but cmyk color is different on photoshop?

NEED HELP!
I create a cmyk image by php imagemagick, but cmyk color is different on photoshop! e.g.: set ImagickPixel color cmyk(0,0,0,100)(black), but found cmyk(61,61,61,0) on photoshop.
why? and how to set the correct cmyk color?
You need to set the colorspace to CMYK, other wise your pixels will be converted to RGB.
$img->setImageColorspace(Imagick::COLORSPACE_CMYK);
http://php.net/manual/en/imagick.setimagecolorspace.php
Also all make sure that you are using file type that supports CMYK. ( eg. .jpg, .tif )
Edit
It seems Imagick has a bug.
Until it is fixed you can try and use this work around using transformImageColorspace.
$draw = new \ImagickDraw();
$fillColor = new \ImagickPixel();
$fillColor->setColor('cmyk(0%,0%,0%,100%');
$draw->setFillColor($fillColor);
$draw->rectangle(100, 100, 400, 400);
$img = new \Imagick();
$img->newImage(500, 500, 'white');
$img->drawImage($draw);
$img->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$img->setImageFormat("jpg");
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

PHP Imagick (ImageMagick) RGB > CMYK with Flat Black

I'm using PHP Imagick to convert PNG images generated in PhantomJS to TIF CMYK,
for print purposes I need a flat Black (cmyk - 0,0,0,100) - the conversion generates blacks like (cmyk - 58,49,44,89).
I'm converting the images using color profile (section of my code below) -> the code is based on Convert image from RGB to CMYK with Imagick
is it possible to force a flat black with Imagick ? do you know any other tools that might help ?
thanks,
if ($has_icc_profile === false) {
$icc_rgb = file_get_contents( '/srgb_profiles' . '/sRGB.icc');
$image->profileImage('icc', $icc_rgb);
unset($icc_rgb);
}
// then we add an CMYK profile
$icc_cmyk = file_get_contents( '/cmyk_profiles'.'/JapanColor2002Newspaper.icc');
$image->profileImage('icc', $icc_cmyk);
UPDATE :
after checking online I think I'm looking for a UCR en.wikipedia.org/wiki/Under_color_removal method for ImageMagick - I found that convert old versions supported under color removal
-undercolor <undercolor factor>x<black-generation factor>
control undercolor removal and black generation on CMYK images.
This option enables you to perform undercolor removal and black generation on CMYK images-- images to be printed on a four-color printing system. You can con- trol how much cyan, magenta, and yellow to remove from your image and how much black to add to it. The standard undercolor removal is 1.0x1.0. You'll frequently get better results, though, if the percentage of black you add to your image is slightly higher than the percentage of C, M, and Y you remove from it. For example you might try 0.5x0.7. (http://www.chemie.fu-berlin.de/chemnet/use/suppl/imagemagick/www/convert.html) -
apparently the option is not supported anymore, I'm interested if anyone knows if UCR is the solution I'm looking for and if anyone knows if it's supported or if I'm supposed to use a different method to get the same result.
If you use ImageMagick's convert at the command line like this to generate a grayscale ramp, 1 pixel wide and 256 pixels tall, going from white to black and convert it to CMYK colorspace and then show it as text, you get what you want:
convert -size 1x256 'gradient:rgb(255,255,255)-rgb(0,0,0)' -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,256,65535,cmyk
0,0: (0%,0%,0%,0%) #0000000000000000 cmyk(0,0,0,0)
0,1: (0%,0%,0%,0.392157%) #0000000000000101 cmyk(0,0,0,1)
0,2: (0%,0%,0%,0.784314%) #0000000000000202 cmyk(0,0,0,2)
0,3: (0%,0%,0%,1.17647%) #0000000000000303 cmyk(0,0,0,3)
0,4: (0%,0%,0%,1.56863%) #0000000000000404 cmyk(0,0,0,4)
0,5: (0%,0%,0%,1.96078%) #0000000000000505 cmyk(0,0,0,5)
0,6: (0%,0%,0%,2.35294%) #0000000000000606 cmyk(0,0,0,6)
0,7: (0%,0%,0%,2.7451%) #0000000000000707 cmyk(0,0,0,7)
0,8: (0%,0%,0%,3.13725%) #0000000000000808 cmyk(0,0,0,8)
0,9: (0%,0%,0%,3.52941%) #0000000000000909 cmyk(0,0,0,9)
0,10: (0%,0%,0%,3.92157%) #0000000000000A0A cmyk(0,0,0,10)
...
...
0,249: (0%,0%,0%,97.6471%) #000000000000F9F9 cmyk(0,0,0,249)
0,250: (0%,0%,0%,98.0392%) #000000000000FAFA cmyk(0,0,0,250)
0,251: (0%,0%,0%,98.4314%) #000000000000FBFB cmyk(0,0,0,251)
0,252: (0%,0%,0%,98.8235%) #000000000000FCFC cmyk(0,0,0,252)
0,253: (0%,0%,0%,99.2157%) #000000000000FDFD cmyk(0,0,0,253)
0,254: (0%,0%,0%,99.6078%) #000000000000FEFE cmyk(0,0,0,254)
0,255: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
You must be doing something different - maybe this will help you work it out. I am guessing it is your ICC profiles but you can experiment with the above command.
If you just want to experiment with spot values, you can just have IM translate a single pixel like this:
convert -size 1x1 xc:#000000 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,65535,cmyk
0,0: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
or maybe more simply like this:
convert -size 1x1 xc:#000000 -depth 8 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,255,cmyk
0,0: (0,0,0,255) #000000FF cmyk(0,0,0,255)
Note the following though:
You must put profiles between input image and output image names on the command line.
If your image has no embedded profile, the first profile you give is applied to the input image and the second to the output image. If your input image does have a profile, the first profile you give is applied to the output image.

Converting an SVG to PNG and keep transparency does not work

I am trying to convert an SVG to PNG and keep the transparency, but that does not work. Instead, I get a black background.
This is my code:
$png = new Imagick();
$png->setBackgroundColor(new ImagickPixel("transparent"));
$png->readImageBlob($svg);
$png->setImageFormat("png32");
Thanks in advance!

Imagick convert SVG to PNG - colors replaced by black and white

I'am trying to convert an SVG Image which is created by the SVGGraph library ( http://www.goat1000.com/svggraph.php).
The SVG is colored (red, green, yellow, gray, ...) in the browser and everything is fine. But when I convert it, its just black and white.
With this code I convert it:
//new instance of imagick
$im = new Imagick();
//read the svg file/data (its not saved on the filesystem)
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy();
I've tried it with jpeg and png as output format but the result is the same, all colors will be replaced with black
does anyone have an idea how to fix that ?
Try this way:
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy()

Imagick results in negative image while using imagick::profileImage

I m using PHP's Imagick extension to manipulate images. Following are my code
try{
$sourceimg=dirname(__FILE__)."\\source.jpg";
$destinationimg=dirname(__FILE__)."\\source_cmyk.tiff";
$im=new Imagick();
$im->setResolution(300,300);
$im->readImage($sourceimg);
$im->setImageColorSpace(imagick::COLORSPACE_CMYK);
$im->stripImage();
$cmykprofile=#file_get_contents("C:\\USWebUncoated.icc");
$im->profileImage("icm",$cmykprofile);
$im->setImageDepth(8);
$im->setImageUnits(1); //0=undefined, 1=pixelsperInch, 2=PixelsPerCentimeter
$im->setResolution(300,300); //set output resolution to 300 dpi
$im->setImageCompressionQuality(100);
$im->writeImage($destinationimg);
}catch(ImagickException $e){
echo $e->getMessage();
}
The problem i m having is the code negates the original Image. This issue occurs only if i use $im->profileImage. What will be the cause for this? And how to solve this issue? The profile i m using is cmyk color profile that i downloaded from http://www.adobe.com/support/downloads/detail.jsp?ftpID=3680
Any help will be highly appreciated.
Thanks
If you're using images with specialized color profiles or non-RGB images (e.g. CMYK JPEG), you should convert them to sRGB first.
With striping profile you strip ICC color profile as well. Most browsers display your stripped image with gamma 2.2 (default), which differs from CMYK.
Solution of your problem is in user contributed note here: http://www.php.net/manual/en/imagick.setimagecolorspace.php

Categories