使用 ColorConverterOp Java 将 RGB JPEG 转换为 CMYK JPEG
我正在尝试将 RGB 格式的 jpeg 图像转换为 CMYK 色彩空间。唯一的问题是我的最终输出始终是黑色图像。但有趣的是,MAC 中的预览应用程序可以正确显示图像。到目前为止,在我看过的任何地方似乎都没有成功的 rgb 到 cmyk 转换的例子。下面是我用来尝试转换的代码。如果我使用 RGB ICC 配置文件执行到 RGB 的转换,则此代码工作正常。非常感谢任何指导。
import javax.imageio.ImageIO;
public class TestClass {
public static void main(String[] args) throws Exception {
BufferedImage cmykImage = ImageIO.read(new File(
"CMYK_Sample.jpg"));
BufferedImage rgbImage = null;
ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(TestClass.class.getClassLoader().getResourceAsStream("icc/USWebCoatedSWOP.icc")));
ColorConvertOp op = new ColorConvertOp(cpace, null);
rgbImage = op.filter(cmykImage, null);
ImageIO.write(rgbImage, "JPEG", new File("CMYK_Sample_RGB_OUTPUT2.jpg"));
}
}
I am attempting to convert a jpeg image in rgb to CMYK colorspace. The only problem is my final output is always a black image. But interesting enough the preview application in MAC shows the image correctly. There does not seem to be an example of a successful rgb to cmyk conversion anywhere I've looked so far. Below is the code i'm using to attempt the conversion. This code works fine If i'm performing the conversion to rgb using RGB ICC Profile. Any guidance is greatly appreciated.
import javax.imageio.ImageIO;
public class TestClass {
public static void main(String[] args) throws Exception {
BufferedImage cmykImage = ImageIO.read(new File(
"CMYK_Sample.jpg"));
BufferedImage rgbImage = null;
ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(TestClass.class.getClassLoader().getResourceAsStream("icc/USWebCoatedSWOP.icc")));
ColorConvertOp op = new ColorConvertOp(cpace, null);
rgbImage = op.filter(cmykImage, null);
ImageIO.write(rgbImage, "JPEG", new File("CMYK_Sample_RGB_OUTPUT2.jpg"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CMYK用于印刷。因此,除了 pdf 和 postscript 文件之外,几乎没有可能显示它。 JPEG 几乎只能显示 RGB。因此,在最后一行 ImageIO.write 中,您尝试将 cmyk 读取为 RGB。问题就在这里。
JPEG 中的 CMYK:“Adobe Photoshop 和其他一些面向印前的应用程序将生成
当要求从 CMYK 图像模式保存 JPEG 时,生成四通道 CMYK JPEG 文件。
几乎任何不精通印前技术的东西都无法处理 CMYK JPEG(或任何
其他 CMYK 格式)。制作 JPEG 供 Web 使用时,请确保
从 RGB 或灰度模式保存。” (http://www.faqs.org/faqs/jpeg-faq/part1/)
至于在 java 中显示 CMYK 文件,请使用 java-2d (http://download.oracle. com/javase/1.3/docs/guide/2d/spec/j2d-color.fm2.html)
CMYK is for printing. So, there are few possibilities to show it, except of pdf and postscript files. JPEG can show almost only RGB. So, in your last line ImageIO.write you are trying to read cmyk as RGB. Here is the problem.
CMYK in JPEG:"Adobe Photoshop and some other prepress-oriented applications will produce
four-channel CMYK JPEG files when asked to save a JPEG from CMYK image mode.
Hardly anything that's not prepress-savvy will cope with CMYK JPEGs (or any
other CMYK format for that matter). When making JPEGs for Web use, be sure
to save from RGB or grayscale mode." (http://www.faqs.org/faqs/jpeg-faq/part1/)
As for showing CMYK files in java, use java-2d (http://download.oracle.com/javase/1.3/docs/guide/2d/spec/j2d-color.fm2.html)