PDF image colorspace [RESOLVED]

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
larowlan

PDF image colorspace [RESOLVED]

Post by larowlan »

Hi
I'm using imagick to generate pdf's from a series of elements/tiles. Everything works fine except for the colors.
If I set the image format to png and then use the convert cli the colors are correct (ie intermediary of png).
If I go straight to pdf (ie set image format as pdf) the colors are washed out. I've tried several different values of setImageColorSpace as follows (RGB, sRGB, CMYK) but it is not working.
Here is a sample of the color if I use png as intermediate format (and the color seen in html).
Image
Here is a sample of the straight to pdf color
Image
Any clues?
Last edited by larowlan on 2010-03-17T21:08:23-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

did you see viewtopic.php?f=18&t=15708 just two topics down the list? apparently it is not yet implemented in Imagick, but they are working on it.
larowlan

Re: PDF image colorspace

Post by larowlan »

Cheers, no I didn't read this through to the end as it was more to do with jpeg and reading pdfs rather than creating pdfs. If the function is not implemented, why doesn't it give an error?
Why does making a png work but making a pdf not work - is this because image magick uses a different colorspace for pdf by default?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

I cannot answer that. Perhaps I misunderstand your issue vs viewtopic.php?f=18&t=15708.

But perhaps it has to do with your ghostscript? I am not sure whether ghostscript is needed to convert from or to pdf.

What version of IM are you using and what version of ghostscript.

Have you tried just using command line?

When I do:

convert -size 100x100 xc:blue blue.png
convert -size 100x100 xc:blue blue.pdf

I get exactly the same color when I look at the results.
larowlan

Re: PDF image colorspace

Post by larowlan »

Yeah, I too get the same colour with the cli, but unfortunately this is part of a larger php/web app which needs to generate the pdf on the fly. If I can't get it to work, I'll need to look at caching the png as a file and using convert via the php exec or equivalent function - not that I'm keen on it!.
My versions are:
ghostscript-8.63-1.fc8
ImageMagick-6.3.5.10-1.fc8
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

larowlan wrote:Yeah, I too get the same colour with the cli, but unfortunately this is part of a larger php/web app which needs to generate the pdf on the fly. If I can't get it to work, I'll need to look at caching the png as a file and using convert via the php exec or equivalent function - not that I'm keen on it!.
My versions are:
ghostscript-8.63-1.fc8
ImageMagick-6.3.5.10-1.fc8

That is a rather old version of IM. I know that ISPs are reluctant to upgrade, but if you have the option then perhaps you should.

What API are you using for your PHP? Imagick? Perhaps there is an issue there. What are your commands to generate the PDF?
larowlan

Re: PDF image colorspace

Post by larowlan »

If I save the pdf generated then convert it to a png using convert, the colours are correct again!
I think it is some sort of CMYK to RGB type issue between pdf format and other image formats.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

Perhaps I am missing something, but the swatch you posted as a pdf is really jpg? and so it the png?

Again, can you post your commands as I am unsure what you are doing in the way of creating the swatches, especially the pdf and if you then need to convert to other image types.

If converting a cmyk pdf to some other format, one needs to set the colorspace before reading the image. This was the issue that I thought was not coded in Imagick in the other post I mentioned.

However, if you are creating a pdf and not doing anything to it, then it should work as from the command line, unless there is some issue either with your IM version or the version of IMagick.
larowlan

Re: PDF image colorspace

Post by larowlan »

I can upgrade (no isp) but I'd need to upgrade the whole OS to the newest Fedora and I don't have the time for that unfortunately, maybe later in the month.
I am using imagick, the commands are quite involved as it is taking an array of tiles/images and converting them (the whole thing is inside a Drupal module as well).
Here are the relevant bits

Code: Select all

  $image = new Imagick();
  $image->setResolution(300, 300);
  $image->newImage(1181, 1181,
    new ImagickPixel('none')); //new image w/ transparency 
  
  //make it a pdf 
  $image->setImageFormat('pdf');
  $image->setImageColorspace(imagick::COLORSPACE_RGB);
  $image->setCompression(imagick::COMPRESSION_ZIP);
  $image->setCompressionQuality(80);

//adding the rectangle
$background = new ImagickPixel('#00ff00'); //green for example
$tile_img = new Imagick();
$tile_img->newImage(500,
                          500,
                          $background);
//combine the tile into the image (this is done lots of times)
$image->compositeImage($tile_img,
                           Imagick::COMPOSITE_DEFAULT,
                           10,
                           10);

//output the image
header("Content-type: application/pdf");
echo $image->getImageBlob();

If I change the two refs to pdf to png (eg the header and the setImageFormat) I get the correct green, with pdf, the colour is washed. -
here is the png version

Code: Select all

  $image = new Imagick();
  $image->setResolution(300, 300);
  $image->newImage(1181, 1181,
    new ImagickPixel('none')); //new image w/ transparency 
  
  //make it a pdf 
  $image->setImageFormat('png');
  $image->setImageColorspace(imagick::COLORSPACE_RGB);
  $image->setCompression(imagick::COMPRESSION_ZIP);
  $image->setCompressionQuality(80);

//adding the rectangle
$background = new ImagickPixel('#00ff00'); //green for example
$tile_img = new Imagick();
$tile_img->newImage(500,
                          500,
                          $background);
//combine the tile into the image (this is done lots of times)
$image->compositeImage($tile_img,
                           Imagick::COMPOSITE_DEFAULT,
                           10,
                           10);

//output the image
header("Content-type: image/png");
echo $image->getImageBlob();

This is an edited bit of my code but demonstrates the basics for this issue and should run on your machine (can't see any dependencies on my code!)
larowlan

Re: PDF image colorspace

Post by larowlan »

here are the output files
Attachments
31.zip
the pdf (had to make it a zip)
(900 Bytes) Downloaded 1078 times
the png
the png
31.png (194 Bytes) Viewed 29714 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

this looks on the surface like the problem mentioned in the other topic where it was suggested to move the setting of the colorspace right after the creation of the image

$image = new Imagick();
$image->setImageColorspace(imagick::COLORSPACE_RGB);
...

Don't know if that will help as Imagick may not be able to do that yet as I read that other topic. But that is what may be needed in command line, if I understand your problem.
larowlan

Re: PDF image colorspace

Post by larowlan »

Also, for what it is worth - the following lines don't make an impact (I've taken them out/added them in)

Code: Select all

 $image->setImageColorspace(imagick::COLORSPACE_RGB);
  $image->setCompression(imagick::COMPRESSION_ZIP);
  $image->setCompressionQuality(80);
But changing the first line to

Code: Select all

 $image->setImageColorspace(imagick::COLORSPACE_CMYK);
Makes a huge difference - the colours become black/brown etc (so I think that setImageColorSpace might be implemented!)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF image colorspace

Post by fmw42 »

your pdf and png look to me to have the same green color when opened in Mac Preview.
larowlan

Re: PDF image colorspace

Post by larowlan »

Adding the setImageColorSpace straight after the new $imagick throws an error
'Can not process empty Imagick object'

If I move it down, 1 line at a time, as soon as I pass this line

Code: Select all

  $image->newImage(50, 50,
    new ImagickPixel('none')); //new image w/ transparency 
The error is no longer thrown but the poor colour remains!
Is it to do with my background maybe?
larowlan

Re: PDF image colorspace

Post by larowlan »

Hmm
Let me try a different monitor
Post Reply