javaimagemagickrgbcmykjmagick

JMagick - How to convert a picture from CMYK to RGB?


I know there exists another post dealing with that problem How to convert colorspace using JMagick? but but there is something I do not understand:

    String baseName = "Pictures/";
    String fileName = "dragon.gif";
     MagickImage imageCMYK;
     try {
     ImageInfo info = new ImageInfo( baseName + fileName);
     info.setColorspace(ColorspaceType.CMYKColorspace);

     System.out.println("ColorSpace BEFORE => " + info.getColorspace());

     imageCMYK = new MagickImage( info );


     System.out.println("ColorSpace AFTER => " +
             imageCMYK.getColorspace());

When I create the new MagickImage, the CMYKColorSpace is not kept as I obtain :

ColorSpace BEFORE => 12 (CMYK)

How to correctly convert a picture from CMYK to RGB ?

Thanks.

ColorSpace AFTER => 1 (RGB)


Solution

  • Update: You are using GIF images. They don't support "CMYK" so the transform won't work for you (see this forum post at imagemagick's web site)!


    Use MagicImage.rgbTransformImage(ColorspaceType.CMYKColorspace). From the API:

    public boolean rgbTransformImage(int colorspace) throws MagickException

    Converts the reference image from RGB to an alternate colorspace. The transformation matrices are not the standard ones: the weights are rescaled to normalized the range of the transformed values to be [0..MaxRGB].


    Example:

    try {
        MagickImage image = new MagickImage(new ImageInfo(baseName + fileName));
    
        if (!image.rgbTransformImage(ColorspaceType.CMYKColorspace))
             throw new Exception("Couldn't convert image color space");
    
        ...
    } catch (MagickException e) {
        ...
    }