2

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)

Community
  • 1
  • 1
Johann
  • 447
  • 2
  • 8
  • 23
  • Whichever way you end up doing this, be very mindful of the effect and need for colour profiles in this process. – Orbling Sep 21 '11 at 13:50
  • When you write the image, why is the set ColorSpace lost ? imageCMYK.writeImage(info); – Johann Sep 21 '11 at 14:03

2 Answers2

1

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) {
    ...
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • Thanks for your answer ! But there is still a problem because once you have written your image : imageCMYK.writeImage(info); System.out.println("ColorSpace AFTER => " + imageCMYK.getColorspace()); RGBColorSpace comes back.... WHY ? – Johann Sep 21 '11 at 13:55
  • 2
    Oh, I see your your working with `".gif"` files. I don't think they support CMYK. Updated answer! – dacwe Sep 21 '11 at 15:01
0

This still won't work for other image formats such as PNG.

trigoman
  • 3,585
  • 2
  • 21
  • 20