PHP Imagick (ImageMagick) RGB > CMYK with Flat Black - php

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.

Related

PNG processing WITHOUT Image Magick and imagefrompng()

I need to change each hue of yellow to blue, and each hue of dark gray to light gray in PNG images with transparency.
The problem is:
I can't use Photoshop, because I have 100 images, and I need to change hues many time.
I can't use Image Magick, because I need more sophisticated calculations, than '-fx' can do.
I can't use PHP imagefrompng(), because this nasty crap not works with a lot of my images,
even with all suggested fixes like:
$background = imagecolorallocate($png, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($png, $background);
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($png, true);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($png, true);
and so on. It works with some images, but not with others.
All I need is a PNG library (maybe not in PHP), that can give me red, green, blue and alpha component of a pixel at coordinates x, y, and then set this pixel after my calculations, eg:
$rgba = getrgba($image, $x, $y);
$rgba = my_function($rgba);
setrgba($image, $x, $y, $rgba);
Maybe you can suggest libraries in other languages, not only PHP?
If you don't mind using Python check out Pillow, specifically its PixelAccess class. These threads (1, 2) should be helpful and have some code examples.
Method 1
If you just want to get at the raw pixel values of R,G,B and Alpha without worrying about compression and encoding, use ImageMagick to convert your image to plain, uncompressed, unencoded binary and read it and process it to your heart's content.
So, if we make a 1x1 pixel PNG file with RGBA(0,64,255,0.5) to test with:
convert -size 1x1 xc:"rgba(0,64,255,0.5)" a.png
Now we can get ImageMagick to make a raw, RGBA file that you can read and process as you wish with whatever language you wish at whatever level of complexity that you wish:
convert a.png rgba:data.bin
and now we can look in that file:
xxd data.bin
Result
0000000: 0040 ff80 .#..
There you can see and read all the RGBA pixels. When you are finished, just do the opposite to get back a PNG - note that you must tell ImageMagick the size first since it cannot know this:
convert -size 1x1 rgba:data.bin new.png
Note that ImageMagick is quite a large package to install, and you can achieve much the same as the above with the much lighter-weight vips package:
vips VipsForeignSaveRaw a.png data.rgb
Method 2
Alternatively, if you want your data as uncompressed, human-readable ASCII, use the venerable NetPBM formats of PPM (Portable Pixmap) and PGM (Portable Greymap) - see NetPBM on Wikipedia.
Make a 4x1 image and write as PPM:
convert -size 4x1 xc:"rgba(0,64,255,0.1)" -compress none ppm:-
P3
4 1
65535
0 16448 65535 0 16448 65535 0 16448 65535 0 16448 65535
You can see the 4 repeated RGB values there hopefully. If you want it as a file, just change the ppm:- at the end with someFile.ppm.
Make same image again and extract Alpha channel to separate file:
convert -size 4x1 xc:"rgba(0,64,255,0.1)" -alpha extract -compress none pgm:-
P2
4 1
65535
6554 6554 6554 6554
Hopefully you can see that 6554 is 0.1 on a scale of 0-65535.
If you just want 8-bit data on any of the above, add in -depth 8.
Method 3
As Glenn suggests in the comments, another option is to omit the -compress none on Option 2 which will give you a very similar file format except the pixel data will be in binary, after the header which remains in ASCII. This is generally faster and smaller.

ImageMagick caption in PHP not filling size vertical dimension

I have cobbled together the following code, which very nearly works:
<?php
$img = new Imagick("quote_blank.jpg");
$txt = new Imagick();
$txt->setBackgroundColor("transparent");
$txt->newPseudoImage(380,250, "Caption:".htmlspecialchars($_GET['quote']) );
$txt->colorizeImage('#468847',1);
$img->compositeImage($txt, imagick::COMPOSITE_OVER, 10, 80);
$draw = new ImagickDraw();
$draw->setFillColor('#468847');
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
$draw->setFontSize(25);
$draw->setFontStyle(3);
$img->annotateImage($draw, 5,5,0, htmlspecialchars($_GET['attrib']) );
$img->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $img;
?>
(please note that quote_blank.jpg is a 400x400 image background over which the text is rendered and resides in the same directory as the php file).
The issue is that the caption only fills the 380x250 PseudoImage with a very small number of short words. Anything of any length results in just the top half (or less) of the box having any text in it (aside from the attribution annotation).
It seems like the PseudoImage is working correctly but that ImageMagick's algorithm for calculating the font size is only designed to fill the width, not the height. I have no idea how it decides what line length to go for (which would presumably in turn dictate the font size and therefore number of lines and vertical coverage of the caption box).
So I guess my question is this: Is there any way of changing how it does it's calculations in order to fill as much of the caption box as possible, horizontal AND vertical?
Sample of just a few words, showing the caption can go full-height:
Sample of a more typical length of text, showing it doesn't fill the box vertically
I tested your code with ImageMagick 6.8.9-8 and got the following output, which is better than what you're getting. If you're using an older version, try updating ImageMagick.
Vinicius Pinto had the right answer right off the bat. But updating wasn't so easy on a shared server- I have not figured out how to get Imagick to use the updated version. So I had to rewrite my code to access ImageMagick via the commandline, which I wanted to share. Code doesn't show up well as far as I can tell on a comment, so sorry for cheating the answer feature a little.
$location='/home/user/local/bin/convert';
$command='convert -background none -size 380x250 -fill "#468847" caption:"'.htmlspecialchars($_GET['quote']).'" quote_blank.jpg +swap -gravity southeast -geometry +10+80 -composite convert -fill "#468847" -gravity southeast -pointsize 25 -annotate 0x20+5+5 "'.htmlspecialchars($_GET['attrib']).'" anno_label.jpg';
exec ($location . ' ' .$command);
header('Content-Type: image/jpeg');
readfile('anno_label.jpg');
unlink('anno_label.jpg');

Detect if image is grayscale or color using Imagick

I'm attempting to try and assign a value to an image based on its 'saturation level', to see if the image is black and white or color. I'm using Imagick, and have found what seems to be the perfect code for the command line and am trying to replicate it using the PHP library.
I think I understand the concept:
Convert image to HSL.
Extract the 'g' channel (which is the S channel in HSL).
Calculate the mean of this channel.
Command line code
convert '$image_path' -colorspace HSL -channel g -separate +channel -format '%[fx:mean]' info:
My PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
print_r($imagick->getImageChannelMean(imagick::CHANNEL_GREEN));
Output
My PHP code isn't outputting the same sort of values as the command line code, though. For example, a grayscale image gives 0 for the command line code, but the PHP code gives [mean] => 10845.392051182 [standardDeviation] => 7367.5888849872.
Similarly, another grayscale image gives 0 vs. [mean] => 31380.528443457 [standardDeviation] => 19703.501101904.
A colorful image gives 0.565309 vs. [mean] => 33991.552881892 [standardDeviation] => 16254.018540044.
There just doesn't seem to be any kind of pattern between the different values. Am I doing something obviously wrong?
Thanks.
Just to add, I've also tried this PHP code
$imagick = new Imagick($image_path);
$imagick->setColorspace(imagick::COLORSPACE_HSL);
$imagick->separateImageChannel(imagick::CHANNEL_GREEN);
$imagick->setFormat('%[fx:mean]');
But I get an Unable to set format error when I try and set the format. I've also tried setFormat('%[fx:mean] info:'), setFormat('%[mean]'), setFormat('%mean'), etc.
Update — FIXED!
Thanks to #danack for figuring out I needed to use transformImageColorspace() and not setColorspace(). The working code is below.
$imagick = new Imagick($image_path);
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
$saturation_channel = $imagick->getImageChannelMean(imagick::CHANNEL_GREEN);
$saturation_level = $saturation_channel['mean']/65535;
setFormat doesn't replicate the command line option -format - the one in Imagick tries to set the image format, which should be png, jpg etc. The one in the command line is setting the format for info - the closest match for which in Imagick is calling $imagick->identifyImage(true) and parsing the results of that.
Also you're just calling the wrong function - it should be transformImageColorspace not setColorSpace. If you use that you can use the statistics from getImageChannelMean.
There are other ways to test for greyness which may be more appropriate under certain circumstances. The first is to convert the a clone of the image to grayscale, and then compare it to the original image:
$imagick = new Imagick($image_path);
$imagickGrey = clone $imagick;
$imagickGrey->setimagetype(\Imagick::IMGTYPE_GRAYSCALE);
$differenceInfo = $imagick->compareimages($imagickGrey, \Imagick::METRIC_MEANABSOLUTEERROR);
if ($differenceInfo['mean'] <= 0.0000001) {
echo "Grey enough";
}
That would probably be appropriate if you image had areas of color that were also transparent - so they theoretically have color, but not visibly.
Or if you only care about whether the image is of a grayscale format:
$imageType = $imagick->getImageType();
if ($imageType === \Imagick::IMGTYPE_GRAYSCALE ||
$imageType === Imagick::IMGTYPE_GRAYSCALEMATTE) {
//This is grayscale
}
I found a command line here:
convert image.png -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:
It prints a number between 0 and 1, where zero means grayscale.
If your images have tint. (from scanner as example) you should do auto color for them before detecting gray scale.
You should normalizeImage(imagick::CHANNEL_ALL) for all separate channels of image. separateImageChannel()
But

How to convert CMYK/RGB TIFF to RGB JPEG using PHP IMagick

I have a PHP application which needs to deal with incoming TIFF files. I have neither control nor knowledge over the colorspaces of this TIFFs and the application should store all incoming images as RGB JPEGs.
Problem is, incoming TIFF files are anything: CMYK, RGB, some sort of YCbCr wrapped in sRGB, and so on, and I need to convert them somehow to RGB JPEGs before saving.
I need some sort of a conversion function in PHP which uses IMagick extension which can get any binary TIFF data and convert it to proper RGB JPEG binary data. It needs to handle different colorspaces inside TIFF images correctly. Output format (RGB JPEG) stays the same for any input file.
The following obvious solution converts some CMYK TIFFs correctly, some CMYK TIFFs get inverted colors and YCbCr RGB TIFFs get totally corrupted by red overlay:
$converter = new IMagick();
$converter->setResourceLimit(6, 1);
$converter->readImageBlob($data);
if ($converter->getImageColorspace() != IMagick::COLORSPACE_RGB
&& $converter->getImageColorspace() != IMagick::COLORSPACE_GRAY
) {
$icc_rgb = file_get_contents('sRGB_v4_ICC_preference.icc');
$converter->profileImage('icc', $icc_rgb);
$converter->setImageColorspace(IMagick::COLORSPACE_RGB);
}
$converter->setImageFormat('jpeg');
$converter->setImageCompression(Imagick::COMPRESSION_JPEG);
$converter->setImageCompressionQuality(60);
$converter->resizeImage(1000, 1000, IMagick::FILTER_LANCZOS, 1, true);
$converter->stripImage();
$result = $converter->getImagesBlob();
This solution is taken from there: http://blog.rodneyrehm.de/archives/4-CMYK-Images-And-Browsers-And-ImageMagick.html Obviously, it doesn't work for all colorspaces, because it doesn't detect them reliably. As you can see, it even uses the sRGB_v4 ICC color profile downloaded from it's homepage.
Google finds me one particular solution to the red overlay problem (just one of the conversion screw-ups), but it's only for console and when you know beforehand that you deal with YCbCr images:
convert some.tif -set colorspace YCbCr -colorspace RGB some.jpg
I can live with passthru-ing convert and pass to convert all the magical switches needed, but I suppose I need to detect the source image's colorspace beforehand and call a identify | grep before every convert in an otherwise PHP application is an overkill.
I've experienced this same issue.
It also came up in the imagick forums and the correction was pushed into ImageMagick 6.8.0-4 .
So upgrading should solve this issue. I've upgraded to ImageMagick 6.8.1-9 and haven't encountered this since.

PHP Imagick API: How to change the Hue of an Image to known hexadecimal value

I create template websites. If a client choose a blue or green or purple heading, I don't want to have to store all those different color variations of an image. I want to programmatically change the hue. I do not want to 'flood fill' it because that would remove any textures or bevels.
For example page you see I have accomplished exactly what I want.
http://sta.oursitesbetter.com/test/index.php
I have done this using Imagick modulateImage function.
HOWEVER, I am just throwing random 'Hue' values and not RGB values. I want to accomplish this same thing feeding RGB values. I need a function similar to modulateImage however it must take RGB as value and set the image to that hue.
I have studied for the past 5 hours and cannot figure out how to do it. I need help.
Has any of the gurus of StackOverflow got a PHP Imagick solution to this color quandary?
From Wikipedia: Hue #Computing hue from RGB:
$hex=(sqrt(3)*($green-$blue)) /
(2*($red-$green-$blue));//$red, $green and $blue are each a value in the range 0->255
$img->modulateImage(100, 100, intval($hex*100/256));
//probably 256->100 value above will work, if didn't , try with the following insead.
//$img->modulateImage(100, 100, $hex);
Another option might be to use ImageMagick to create a single pixel image in the RGB colour you want and then see what ImageMagick makes that in HSL colorspace. Say your RGB values are 25,126,200, you could do this:
convert -size 1x1! xc:rgb\(25,126,200\) -colorspace HSL txt:-
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (57.0474%,77.7783%,44.1184%) #920AC71C70F1 hsl(57.0474%,77.7783%,44.1184%)
The following equivalent methods are maybe a little more succinct:
convert xc:rgb\(25,126,200\) -format "%[fx:100*p{0,0}.hue]" info:
57.0476
convert xc:rgb\(25,126,200\) -format "%[fx:100*hue]" info:
57.0476
So, IM makes that a Hue of 57.0474%. Of course you don't need to run that and parse the text output, I am only doing that to demonstrate the concept - you can access the pixel directly in PHP.

Categories