Generating thumbnails of PDFs with imagemagick sometimes renders incorrect colors. We're using an old version of imagemagick (6.5.7-8, that's the version installed on the heroku servers). Here is the command we're currently using:

convert \
  -size "725x1200>" \
  -colorspace RGB \
  -flatten \
  -density 300 \
  -quality 100 \
   input.pdf \

I've tried using different colorspaces like sRGB,YIQ,.. but none of them are rendering the color correctly.

Using imagemagick-6.7.7-6 locally works so I've tried to bundle the 'convert' command within my application /bin directory, the command works but the result is still wrong, so it seems that the problem comes either from another imagemagick command used by 'convert' or from another library.

Here's an example of the outputs:

  • Correct output: on the left
  • Wrong output: on the right
    Correct output Incorrect output

Strangely, with some pages of the same pdf the output is always correct. Any idea which library or command could be the issue, or if there is a proper set of options to pass to imagemagick to always get it right?

  • Looks almost like it's not so much that the color is wrong... as it looks like it is converting a 16- or 32 bit image palette to a 256 color palette... at least from that example you gave. Not only was the teal changed to a bright green, but the blurry text was sharpened quite a bit (not legible, but as if there were fewer shades of grey). The times it appears to work could be due to the specific colors themselves, or the lack of distinct colors? – Bon Gart Jul 5 '12 at 15:48
  • Thanks for the pointer Bon, I actually tried to set -colors 32 but without success. From the assumption that imagemagick is using ghostscript I then went on trying to see the output directly from it gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=out.jpg in.pdf to identify the colorspace. The output was already wrong! Passing -dUseCIEColor fixes it. But how can I tell imagemagick to pass this option along to gs? – Bastien Jul 5 '12 at 16:56
  • @kurt-pfeifle thanks for adding the pictures in there, I didn't have enough reputation points to do it myself – Bastien Jul 6 '12 at 22:33

After some more investigation, it appears that the problem comes from ghostscript which is missing the -dUseCIEColor option. And from what I've gathered around the web it is not possible to pass parameters to ghostscript through imagemagick. What is possible though is to call the 2 commands together with a pipe: https://stackoverflow.com/questions/653380/converting-a-pdf-to-png

My specific use case

This is a bit more specific to my use case, but I thought someone might be interested. As we're generating thumbnails via Paperclip in our rails application, I created a new paperclip processor to take care of the pdf to jpg conversion and then imagemagick takes over to do the usual resizing. My code with an example here: https://gist.github.com/3059321

| improve this answer | |
  • Why do you need to post-process the JPEGs with imagemagick at all? Can't you add all the params to Ghostscript so you get the sizes you want ? – Kurt Pfeifle Jul 6 '12 at 18:26
  • '...possible to call the 2 commands together with a pipe...' -- But you didn't upvote this answer, eh? :-) – Kurt Pfeifle Jul 6 '12 at 18:28
  • Good point, upvoted now ;) About only using ghostscript, I suppose I could, as I said, in my specific case Paperclip is automatically doing it for me, but there might be a way to only run the ghostscript processor. – Bastien Jul 6 '12 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.