ImageMagick CMYK to RGB

WOOOHOOOO! I finally got it sorted, so I thought I should share it with the hordes of people having issues converting CMYK postscript files(e.g. ai eps etc) to an RGB bitmap.

The problem is not with ImageMagick, it is to do with GhostScript using its ‘default’ crappy algorithm to convert CMYK to RGB.

Make sure you are using the ‘patched’ version of GhostScript. Read here for more. The only problem with this is that when you call GhostScript you need to set a command line option of ‘-dUseCIEColor‘. The thing is you are not calling GS, ImageMagick is!

There is a very simple work around for this. You need to open the ‘delegates.xml’ or ‘delegates.mgk’ (dependant on your version of IM) within your ImageMagick installation directory and add the option after every “@PSDelegate@” or “gs”. For example:

<delegate decode="ps:alpha" restrain="True" command='"@PSDelegate@" -q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-g%s" "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"' />

becomes

<delegate decode="ps:alpha" restrain="True" command='"@PSDelegate@" -dUseCIEColor -q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-g%s" "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"' />

Hope this helps… if you have any issues give me a shout.

20 Responses to ImageMagick CMYK to RGB

  1. Nicolas BUI says:

    Hi Jonh,

    Thanx for this cool post… I’ve encounter the same problems .. and thanx to you this succesfully solve the problem !

  2. JK says:

    Thanks man, your post fulfilled its mission!

  3. Thanks a lot man. That really helped me a lot .

  4. You don’t have to mody an existing delegate in the system delegates file. You can create your own delegate.xml file in a “.magick” sub-directory of your home. You will need to name the delegate something new however.

    If you do this you can create say a “pscmyk:” delegate to handle CMYK postscript files while leaving the normal postscript handling of IM alone.

    This also means you don’t need ‘root’ previlages to make the change needed.

    See IM Examples, Delegates
    http://www.cit.gu.edu.au/~anthony/files/#delegates
    (section still under construction) for more details.
    If you do develop a “delegate” please contribute.

  5. Anil Sharma says:

    Thanks John. It was very useful.

  6. Chris says:

    Need a bit more help here… I have already installed ImageMagick on a windows machine. I can find the delegates.xml file but don’t see any block of code similar to above. I have not yet installed GS. Can I just run the GS exe and will modify the IM delegates file to include the GS stuff? I don’t want to mess anything up in the IM setup at this point.

    Thanks.

  7. Tyler Thorsted says:

    This is exactly the information I need to get the color under control during conversion. The only question I have is if the patch is still needed with the latest version. If it is how do I go about getting GS patched? I get terrible color conversion from CMYK PDF to RGB image. Even with profiles it has no effect.

    Thanks!

  8. kanwar says:

    Hi guys,
    i have a problem very related to the one discussed above:–
    when i convert eps file to jpg from ghostscript using
    “gswin32.exe -sDEVICE=jpeg -dJPEGQ=100 -dNOPAUSE -dBATCH -dSAFER -r300 -sOutputFile=myfile.jpg myfile.eps”
    the output file “myfile.jpg” is in a proper jpg format, but when i convert it using Jmagick (ImageMagick 6) , the file created is not readable by java , whenever i try to read it using this code

    MagickImage image = new MagickImage(new ImageInfo(“C:\\idp_IMAGES\\52DGB 010.ai”));
    //MagickImage image = new MagickImage(new ImageInfo(“C:\\logo.jpg”));
    MagickImage[] pages = image.breakFrames();
    System.out.println(pages.length);
    System.out.println(image.getImageFormat() + ” – ” +image.getImageType());
    ImageInfo info1 = new ImageInfo(“c:\\idp_IMAGES\\dummy.ai”);

    image = image.scaleImage(1000, 1000);
    image.setXResolution(330);
    image.setYResolution(330);

    image.setFileName(“c:\\idp_IMAGES\\harleen.jpg”);
    image.writeImage(info1);

    try{
    BufferedImage bufImage = ImageIO.read(new File(“c:\\idp_IMAGES\\harleen.jpg”));
    }catch (Exception e) {
    e.printStackTrace();
    }
    }

    i am getting
    javax.imageio.IIOException: Unsupported Image Type
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown So

    But the thing is when i manually convert the file using ghostscript, the same code works like a proche :)

    Please help.

  9. kanwar says:

    Noticed one thing , if i do this to
    image.transformRgbImage(ColorspaceType.CMYKColorspace);

    then ImageIO.read(..) is able to read, but the colors are awefull :(
    i desperately need my original colors and still need ImageIO.read(..) to pick it

  10. Mike Duguid says:

    @ Tyler – it would seem the latest ghostscript file gs_init.ps is patched (around line 1892), the patch additional lines are there. Still haven’t been able to get a sensible conversion though..

  11. Jorge says:

    Hey John,

    I’m converting from CMYK pdf to RGB jpeg, and the colors I’m was getting dull colors, I changed some parameters in the convert tool and now I’m getting darker or brighter colors, but usually not the same as the CMYK. I applied the changes that you suggested here, but I don’t see a difference. I made sure I had the patch in gs.

    Am I missing something? Do I have to restart something for IM to take the new configuration files?

    Your help is highly appreciated, have been dealing with this for about two weeks with no success :( .

    Thanks!

  12. Andy says:

    Hi,

    Hope this helps others:

    We’ve been working at converting a CMYK PDF to RGB also and you need to place ” -colorspace RGB ” between the PDF input file and JPG output file to get correct colour conversion. Colours look perfect once this has been added to command line calls.

    Hope this helps Tyler and Jorge and anyone else who comes across this.

    Andy

  13. Jean says:

    I followed all your advices but under windows it changes nothing at all, the colors still do not match with the ones of the original PDF’s.

  14. zulfequar says:

    hey i am using this script to convert various high res cmyk formats to high res jpg in cmyk colors i am not sure what goes wrong but the jpg created is not exactly like the highres file (pdf,eps,ai) Is there anything i need to do to get the conversion correct?

  15. Henrik says:

    I’ve been working on bad quality CMYK PDF to RGB TIFF/JPEG in imagemagick all evening. This was very helpfull, thank you.

    One major solution for me was also changing “-sDEVICE=pam” to “sDEVICE=ppm”, before that the -profile flag would do nothing significant, afterwards the result was almost perfect side by side with a Photoshop conversion (Im in OS X btw)

  16. Ruud says:

    Thanks!! :)

  17. James says:

    Thanks for that – I had this problem some time ago, and solved it. After updating ImageMagick I ran in to the same problem (obviously because delegates.xml had been replaced during the update). After scouring through my notes from when I originally wrote the script, I turned to Google and found your post – solved the problem right away.

  18. Steve says:

    Thanks very much for posting this information… this problem had been annoying me for ages!

  19. James says:

    you champion. 2 weeks of research, one 5 second change…

  20. John Baboo says:

    I want to create from a CMYK PDF file a jpg preview. I get weird colors, i wanna convert the jpg(cmyk) to a jpg(rgb) file, i won’t work.

    won’t work;
    convert file.pdf -colorspace RGB output.jpg

    works;
    convert -colorspace RGB file.pdf output.jpg

    Just that easy!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>