I'm assembling a bunch of .tif using Imagick. Everything is going good, except that the color depth of the output image is always reduced (16 on the input, 1 on the output). I'm using setImageDepth() during the process, but it looks like it has no effect at all. Here's a snippet:
$imagick = new Imagick();
foreach ($_pile_of_tiles as $_index_to_append) {
$_tmp_buffer = new Imagick();
$_tmp_buffer->readImage($_index_to_append . ".tif");
$_imagick->addImage($_tmp_buffer);
}
$_imagick->resetIterator();
$_appended_images = $_imagick->appendImages(true);
$_appended_images->setImageFormat("tiff");
$_appended_images->setImageDepth(16);
file_put_contents("output.tif", $_appended_images);
That gives me a 1-bit image. I tried doing this using the command-line, and it works fine (-depth 16).
Does someone encountered a similar issue?
After looking at the code for the underlying ImageMagick library, apparently this works:
$imagick = new Imagick();
$imagick->newPseudoImage(200, 200, 'xc:white');
$imagick->setType(\Imagick::IMGTYPE_TRUECOLOR);
$imagick->setImageFormat('tiff');
$imagick->setImageDepth(16);
$imagick->writeImage("./output.tif");
system("identify output.tif");
Output is:
output.tif TIFF 200x200 200x200+0+0 16-bit sRGB 241KB 0.000u 0:00.000
No. That's not documented anywhere in the ImageMagick docs. Woo.
Related
I'm trying to convert the first page of PDF documents to a JPEG using Imagick and PHP. As long as the colorspace of the PDF is SRGB, conversion succeeds and the resulting images have correct colors. However, if the PDF has a CMYK colorspace, after conversion, the image colors are off (much brighter or darker).
I'm currently using the following software:
PHP 7.4.3
ImageMagick 6.9.10-23 Q16 x86_64 20190101 (deb package)
Ghostscript 9.50 (2019-10-15)
I'm working on WSL2 on Windows 10.
My test PDF can be found here.
Because I was not happy with the resulting conversions, I firstly tried to see if it is possible to do a successful conversion using Imagick cli. After lots of trial and error, I found that the following command yielded the best result:
convert -density 300 -colorspace srgb input.pdf[0] -layers flatten -strip output.jpg
Result:
I then rewrote the command to PHP:
$input = 'input.pdf';
$output = 'output.pdf';
$image = new Imagick();
$image->setResolution(300, 300);
$image->readImage("{$input}[0]");
$image->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$image = $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageFormat('jpeg');
$image->stripImage();
$image->writeImage($output);
$image->destroy();
Result:
The result of the PHP code is not the same as the result of the CLI version and the original PDF. The result is the same as if I would run te following CLI command:
convert -density 300 input.pdf[0] -colorspace srgb -layers flatten -strip output.jpg
The command looks almost the same, however the transforming of the color space happens later.
My question is: what step do I miss in my PHP code to achieve the same result as the command
convert -density 300 -colorspace srgb input.pdf[0] -layers flatten -strip output.jpg
Additional information:
I also tried using color profiles to do the colorspace transformations. Instead of
$image->transformImageColorspace(Imagick::COLORSPACE_SRGB);
I used
$cmyk = file_get_contents('USWebCoatedSWOP.icc');
$rgb = file_get_contents('sRGB_v4_ICC_preference.icc');
$image->profileImage('icc', $cmyk);
$image->profileImage('icc', $rgb);
Besides these two profiles, I also tried combinations of other CMYK (CoatedFOGRA39, JapanColor2001Coated...) and SRGB (AdobeRGB1998, AppleRGB, sRGB_v4_ICC_preference_displayclass...) profiles.
However, I couldn't find a profile combination that came close to the result of the CLI output and the original PDF file.
Thanks to #fmw42, I was able to fix my issue. To fix it, set the color space using setColorSpace() before reading in the pdf.
$input = 'input.pdf';
$output = 'output.pdf';
$image = new Imagick();
$image->setResolution(300, 300);
$image->setColorSpace(Imagick::COLORSPACE_SRGB); // Add this line
$image->readImage("{$input}[0]");
// $image->transformImageColorspace(Imagick::COLORSPACE_SRGB); // You don't need this line
$image = $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageFormat('jpeg');
$image->stripImage();
$image->writeImage($output);
$image->destroy();
I'm trying to convert and resize eps files into jpg. I use php imagick for this.
After converting the quality is very bad.
my eps you can download here:
https://www.file-upload.net/download-14285439/icon.eps.html
my jpg-img
i use this code:
if ( extension_loaded('imagick') ) {
$imagePath = 'icon.eps';
$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->setColorspace(Imagick::COLORSPACE_SRGB);
$imagick->readImage($imagePath);
$imagick->resizeImage(0, 1000, Imagick::FILTER_LANCZOS, 1);
$imagick->setImageResolution(72, 72);
$imagick->setImageCompressionQuality(70);
$imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
$imagick->setCompressionQuality(70);
$imagick->setImageFormat('jpeg');
$imagick->writeImage('test.jpg');
} else {
echo 'not found';
}
same result with this settings without resize/only convert, but the quality is still bad:
if ( extension_loaded('imagick') ) {
$imagePath = 'icon.eps';
$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->setColorspace(Imagick::COLORSPACE_SRGB);
$imagick->readImage($imagePath);
$imagick->setImageFormat('jpeg');
$imagick->writeImage('test.jpg');
} else {
echo 'not found';
}
i use this version with php 7.2.33:
phpinfo
What is wrong?
As you obviously know ImageMagick uses Ghostscript to render EPS files to JPEG. I would suggest that, rather than use ImageMagick you use Ghostscript directly. This will give you more control over the process than using ImageMagick and will mean that you can post the Ghostscript command line instead of an IM one.
I'm afraid that I have no idea what ImageMagick sends to Ghostscript which makes it rather difficult to offer any suggestions.
In addition you really need to be much more explicit about your problem. What do you actually mean by 'the quality is very bad'. Is this purely subjective or is there some objective criteria you are using ?
The image you've posted doesn't look much like what I see, but since I don't know what command is being used to drive Ghostscript, it may simply be that I am not reproducing your setup exactly.
First note; the nature of your EPS is not really suitable for JPEG compression. JPEG performs best when applied to smoothly varying images, like photographs (JPEG = Joint Photographic Expert Group), it does not work well with large areas of flat colour with sharp edges (which is exactly what you have here), the high frequency component of the sharp edges gives rise to 'ringing' or 'fringing' effects.
When using Ghostscript directly it is possible to alter the JPEG quality. Setting -dJPEGQ=100 will produce the highest quality, trading off the compression (ie the output file will be larger).
In addition your EPS gives its BoundingBox as 20x20 points. So that's 20/72 inch in each dimension. Even at 300 dpi that's going to result in an image which is 84x84 pixels. Pretty small. At 72 dpi you'll get an image which is 20x20 pixels,
It looks to me like you have rendered the EPS at 72 dpi with the default JPEGQ value, the 'poor quality' appears to be nothing more than the well known artefacts produced by JPEG compression. Using that setup with Ghostscript produces something not entirely unlike your posted image (though without the sharp edged corner artefacts). Setting JPEGQ to 100 produces something more sensible. The file produced by the default settings is 3,564 bytes, while the higher quality file is 4,485 bytes.
If it were me I would render to a TIFF file at a decent resolution, say 1200 dpi to give an image 333x333 pixels. Then load that into ImageMagick and resize it to your desired dimensions. Finally export as a JPEG if you require it that way for some reason.
here's a comparison of the output from Ghostscript. On the left is a JPEG produced at 1200 dpi, in the middle is the default quality rendering at 72 dpi and on the right the 72 dpi rendering with JPEGQ set to 100.
I am getting a low quality image while trying to convert from sRGB to CMYK in PHP using Imagick.
Original Image
Converted Image
And here is my PHP code:
$i = new \Imagick();
$i->clear();
$i->readimage("original_image.jpg");
$icc_cmyk = file_get_contents('/tmp/USWebCoatedSWOP.icc');
$i->profileimage('icc', $icc_cmyk);
unset($icc_cmyk);
$i->transformImageColorspace(12);
$i->writeImage('converted_image.php');
I couldn't figure out why this is happen.
Any ideas?
There were several bugs related to CMYK in the older versions of ImageMagick.
According to the changelog there was something fixed for 6.8.2-4 which might be related to what you are seeing.
Upgrading to the latest version of ImageMagick, when you see bad an unexpected output from image conversion is usually worth trying to see if that eliminates the problem.
I am trying to convert a vector image formats .emf,.wmf to a high resolution sharp and crisp raster image .gif,jpg. (Usually this could be easily done in Illustrator). But i am unable to do this in PHP. I am trying the following code but the results are either blurry or distorted or even totally black.
<?php
$image = new Imagick("1.emf");
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('gif');
$image->setresolution(900, 900);
$image->writeImage("2.gif");
?>
We just needed to set the resolution before loading the image.
$image = new Imagick();
$image->setresolution(300, 300);
$image->readimage($filename);
$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);
$image->setImageFormat('jpg');
$image->writeImage("1.jpg");
This code will convert a vector to a sharp and crisp raster image. It works for all vector formats (svg, ai, emf, wmf, etc). If the jpg result is unexpectedly a black image, you need to change the image transparency to white (check this link). Another way to get around with transparency problem is by getting your PHP updated to 5.5 and by installing Imagick for that version. By doing so, it will not cause any problems with transparent images and the above code will just work fine.
For testing purposes you could change jpg to png because it supports transparency.
I'm using the following script to generate an scaled up PNG version of an SVG.
<img src="barrington.svg" width="418" height="188"/> //comparison
<?php
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$svg = file_get_contents("barrington.svg");
$im->setresolution(144,144);
$im->readImageBlob($svg);
$im->setImageFormat("png32");
echo '<img src="data:image/png32;base64,' . base64_encode($im) . '" />'
?>
When displayed/compared to the SVG version at the same size, the PNG has jagged edges around some of the image.
(see image here: http://i.stack.imgur.com/WGKIH.png)
I'm using ImageMagick (if it wasn't already obvious) and would like to fix this problem.
EDIT:
To clarify, for my purposes I need a PNG and not an SVG. This isn't an issue of fixing compatibility in browsers or something.
Generally there is confusion on setImageResolution and setResolution. So you may want to try setImageResolution along with setResolution and see if your edge issue is corrected. From my understanding setResolution is correct for density, but you want to still set your image size. Plus 144 may not be enough high enough density.
If not, then resizing an image is always a possibility. Make the image 2x larger then it needs to be and then resize it.