如何在Java中将RGB图像转换为CMYK,反之亦然?

时间:2023-02-04 15:47:34

our web app let users download dynamically generated images in different formats (bmp, png and jpeg). Some of our users download the images for printing, thus we would like to allow them to choose between RGB or CMYK. Is there a way to specify the color model when creating a RenderedImage/BufferedImage? If not, what is the default color model and how can I change it to another? Code snippets are welcome :)

我们的网络应用程序允许用户以不同的格式(bmp,png和jpeg)下载动态生成的图像。我们的一些用户下载图像进行打印,因此我们希望允许他们在RGB或CMYK之间进行选择。有没有办法在创建RenderedImage / BufferedImage时指定颜色模型?如果没有,默认颜色模型是什么?如何将其更改为另一个?欢迎使用代码片段:)

Thanks,

谢谢,

Olivier.

奥利维尔。

4 个解决方案

#1


0  

It appears that it's not simple and you'll have to either load up a color profile to do it or extend the colorspace to support CYMK.

它似乎并不简单,您必须加载颜色配置文件才能执行此操作或扩展颜色空间以支持CYMK。

#2


0  

Some image formats doesn't allow CMYK color spaces (PNG, JPEG/JFIF, GIF...) and for normal users printing in RGB is desirable.

某些图像格式不允许CMYK颜色空间(PNG,JPEG / JFIF,GIF ...),并且对于普通用户来说,打印RGB是理想的。

What are the reasons you need to provide CMYK images to your customers?

您需要向客户提供CMYK图像的原因是什么?

#3


-1  

To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image).

要通过Java将RGB图像转换为CMYK图像,最简单的方法之一是使用JAI(Java高级图像)。

Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/

下载JAI:http://download.java.net/media/jai/builds/release/1_1_3/

DownLoad JAI ImageIO: http://download.java.net/media/jai-imageio/builds/release/1.1/

下载JAI ImageIO:http://download.java.net/media/jai-imageio/builds/release/1.1/

Here is the code:

这是代码:

public static void rgbToCmyk() throws IOException{

    BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
    BufferedImage cmykImage = null;
    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
    ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
    cmykImage = op.filter(rgbImage, null);

    JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
}

NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.

注意:“ISOcoated.icc”是我的ICC配置文件。您可以从打印机或其他地方获取。

#4


-2  

Suggest using fromRGB() - see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

建议使用fromRGB() - 参见http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

Sample code:

示例代码:

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

public abstract float[] fromRGB(float[] rgbvalue)

public abstract float [] fromRGB(float [] rgbvalue)

Transforms a color value assumed to be in the default CS_sRGB color space into this ColorSpace.

将假定为默认CS_sRGB颜色空间的颜色值转换为此ColorSpace。

This method transforms color values using algorithms designed to produce the best perceptual match between input and output colors. In order to do colorimetric conversion of color values, you should use the toCIEXYZ method of the CS_sRGB color space to first convert from the input color space to the CS_CIEXYZ color space, and then use the fromCIEXYZ method of this color space to convert from CS_CIEXYZ to the output color space. See toCIEXYZ and fromCIEXYZ for further information.

此方法使用设计用于在输入和输出颜色之间产生最佳感知匹配的算法来转换颜色值。为了进行颜色值的比色转换,您应该使用CS_sRGB颜色空间的toCIEXYZ方法首先从输入颜色空间转换为CS_CIEXYZ颜色空间,然后使用此颜色空间的fromCIEXYZ方法从CS_CIEXYZ转换为输出颜色空间。有关详细信息,请参阅CIEXYZ和fromCIEXYZ。

#1


0  

It appears that it's not simple and you'll have to either load up a color profile to do it or extend the colorspace to support CYMK.

它似乎并不简单,您必须加载颜色配置文件才能执行此操作或扩展颜色空间以支持CYMK。

#2


0  

Some image formats doesn't allow CMYK color spaces (PNG, JPEG/JFIF, GIF...) and for normal users printing in RGB is desirable.

某些图像格式不允许CMYK颜色空间(PNG,JPEG / JFIF,GIF ...),并且对于普通用户来说,打印RGB是理想的。

What are the reasons you need to provide CMYK images to your customers?

您需要向客户提供CMYK图像的原因是什么?

#3


-1  

To convert RGB image to CMYK image by Java, one of the easiest way is to use JAI (Java Advanced Image).

要通过Java将RGB图像转换为CMYK图像,最简单的方法之一是使用JAI(Java高级图像)。

Download JAI: http://download.java.net/media/jai/builds/release/1_1_3/

下载JAI:http://download.java.net/media/jai/builds/release/1_1_3/

DownLoad JAI ImageIO: http://download.java.net/media/jai-imageio/builds/release/1.1/

下载JAI ImageIO:http://download.java.net/media/jai-imageio/builds/release/1.1/

Here is the code:

这是代码:

public static void rgbToCmyk() throws IOException{

    BufferedImage rgbImage = ImageIO.read(new File("C://Users//Public//Pictures//Sample Pictures//RGB_IMAGE.jpg"));
    BufferedImage cmykImage = null;
    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(RbgToCmyk.class.getClassLoader().getResourceAsStream("ISOcoated.icc")));
    ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);       
    cmykImage = op.filter(rgbImage, null);

    JAI.create("filestore", cmykImage, "c:/tmp/CMYK_IMAGE.TIF", "TIFF");
}

NOTE: "ISOcoated.icc" is my ICC profile. You can get it from your printer or somewhere else.

注意:“ISOcoated.icc”是我的ICC配置文件。您可以从打印机或其他地方获取。

#4


-2  

Suggest using fromRGB() - see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

建议使用fromRGB() - 参见http://download.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html

Sample code:

示例代码:

java.awt.color.ColorSpace

ColorSpace cmyk = new ColorSpace(ColorSpace.TYPE_CMYK, 4);
float[] values = cmyk.fromRGB(rgbFloatArray);

public abstract float[] fromRGB(float[] rgbvalue)

public abstract float [] fromRGB(float [] rgbvalue)

Transforms a color value assumed to be in the default CS_sRGB color space into this ColorSpace.

将假定为默认CS_sRGB颜色空间的颜色值转换为此ColorSpace。

This method transforms color values using algorithms designed to produce the best perceptual match between input and output colors. In order to do colorimetric conversion of color values, you should use the toCIEXYZ method of the CS_sRGB color space to first convert from the input color space to the CS_CIEXYZ color space, and then use the fromCIEXYZ method of this color space to convert from CS_CIEXYZ to the output color space. See toCIEXYZ and fromCIEXYZ for further information.

此方法使用设计用于在输入和输出颜色之间产生最佳感知匹配的算法来转换颜色值。为了进行颜色值的比色转换,您应该使用CS_sRGB颜色空间的toCIEXYZ方法首先从输入颜色空间转换为CS_CIEXYZ颜色空间,然后使用此颜色空间的fromCIEXYZ方法从CS_CIEXYZ转换为输出颜色空间。有关详细信息,请参阅CIEXYZ和fromCIEXYZ。