如何在 ColdFusion (Java) 中在 CMYK 和 RGB 之间转换图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert images between CMYK and RGB in ColdFusion (Java)?
提问by Adam Tuttle
I have a need to convert images from CMYK to RGB - not necessarily back again, but hey, if it can be done...
我需要将图像从 CMYK 转换为 RGB - 不一定再次返回,但是嘿,如果可以的话......
With the release of ColdFusion 8, we got the CFImagetag, but it doesn't support this conversion; and nor does Image.cfc, or Alagad's Image Component.
随着 ColdFusion 8 的发布,我们获得了CFImage标签,但它不支持这种转换;Image.cfc或Alagad 的 Image Component也没有。
However, it should be possible in Java; which we can leverage through CF. For example, here's how you might create a Java thread to sleep a process:
但是,在 Java 中应该是可能的;我们可以通过 CF 来利用它。例如,以下是创建 Java 线程以休眠进程的方法:
<cfset jthread = createObject("java", "java.lang.Thread")/>
<cfset jthread.sleep(5000)/>
I would guess a similar method could be used to leverage java to do this image conversion, but not being a Java developer, I don't have a clue where to start. Can anyone lend a hand here?
我想可以使用类似的方法来利用 java 来进行此图像转换,但我不是 Java 开发人员,我不知道从哪里开始。有谁能帮帮忙吗?
采纳答案by Randy Stegbauer
I use the Java ImageIO libraries (https://jai-imageio.dev.java.net). They aren't perfect, but can be simple and get the job done. As far as converting from CMYK to RGB, here is the best I have been able to come up with.
我使用 Java ImageIO 库 ( https://jai-imageio.dev.java.net)。它们并不完美,但可以很简单并完成工作。至于从 CMYK 转换为 RGB,这是我能想到的最好的方法。
Download and install the ImageIO JARs and native libraries for your platform. The native libraries are essential. Without them the ImageIO JAR files will not be able to detect the CMYK images. Originally, I was under the impression that the native libraries would improve performance but was not required for any functionality. I was wrong.
为您的平台下载并安装 ImageIO JAR 和本机库。本地库是必不可少的。没有它们,ImageIO JAR 文件将无法检测 CMYK 图像。最初,我的印象是本机库会提高性能,但不是任何功能所必需的。我错了。
The only other thing that I noticed is that the converted RGB images are sometimes much lighter than the CMYK images. If anyone knows how to solve that problem, I would be appreciative.
我注意到的另一件事是转换后的 RGB 图像有时比 CMYK 图像亮得多。如果有人知道如何解决这个问题,我将不胜感激。
Below is some code to convert a CMYK image into an RGB image of any supported format.
下面是一些将 CMYK 图像转换为任何支持格式的 RGB 图像的代码。
Thank you,
Randy Stegbauer
谢谢你,
兰迪·斯特格鲍尔
package cmyk;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.lang.StringUtils;
public class Main
{
/**
* Creates new RGB images from all the CMYK images passed
* in on the command line.
* The new filename generated is, for example "GIF_original_filename.gif".
*
*/
public static void main(String[] args)
{
for (int ii = 0; ii < args.length; ii++)
{
String filename = args[ii];
boolean cmyk = isCMYK(filename);
System.out.println(cmyk + ": " + filename);
if (cmyk)
{
try
{
String rgbFile = cmyk2rgb(filename);
System.out.println(isCMYK(rgbFile) + ": " + rgbFile);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
/**
* If 'filename' is a CMYK file, then convert the image into RGB,
* store it into a JPEG file, and return the new filename.
*
* @param filename
*/
private static String cmyk2rgb(String filename) throws IOException
{
// Change this format into any ImageIO supported format.
String format = "gif";
File imageFile = new File(filename);
String rgbFilename = filename;
BufferedImage image = ImageIO.read(imageFile);
if (image != null)
{
int colorSpaceType = image.getColorModel().getColorSpace().getType();
if (colorSpaceType == ColorSpace.TYPE_CMYK)
{
BufferedImage rgbImage =
new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(image, rgbImage);
rgbFilename = changeExtension(imageFile.getName(), format);
rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();
ImageIO.write(rgbImage, format, new File(rgbFilename));
}
}
return rgbFilename;
}
/**
* Change the extension of 'filename' to 'newExtension'.
*
* @param filename
* @param newExtension
* @return filename with new extension
*/
private static String changeExtension(String filename, String newExtension)
{
String result = filename;
if (filename != null && newExtension != null && newExtension.length() != 0);
{
int dot = filename.lastIndexOf('.');
if (dot != -1)
{
result = filename.substring(0, dot) + '.' + newExtension;
}
}
return result;
}
private static boolean isCMYK(String filename)
{
boolean result = false;
BufferedImage img = null;
try
{
img = ImageIO.read(new File(filename));
}
catch (IOException e)
{
System.out.println(e.getMessage() + ": " + filename);
}
if (img != null)
{
int colorSpaceType = img.getColorModel().getColorSpace().getType();
result = colorSpaceType == ColorSpace.TYPE_CMYK;
}
return result;
}
}
回答by Micha? Piaskowski
A very simple formula for converting from CMYK to RGB ignoring all color profiles is:
从 CMYK 转换为 RGB 忽略所有颜色配置文件的一个非常简单的公式是:
R = ( (255-C)*(255-K) ) / 255;
G = ( (255-M)*(255-K) ) / 255;
B = ( (255-Y)*(255-K) ) / 255;
This code requires CMYK values to be in rage of 0-255. If you have 0 to 100 or 0.0 to 1.0 you'll have to convert the values.
此代码要求 CMYK 值介于 0-255 之间。如果您有 0 到 100 或 0.0 到 1.0,则必须转换这些值。
Hope this will get you started.
希望这会让你开始。
As for the java and ColdFusion interfacing, I'm sorry, but I have no idea how to do that.
至于 java 和 ColdFusion 接口,我很抱歉,但我不知道该怎么做。
回答by Jas Panesar
The tag cfx_image may be of use to you. I haven't used it in a while but I remember it had a ton of features.
标签 cfx_image 可能对您有用。我有一段时间没有使用它,但我记得它有很多功能。
Alternatively, you might be able to script a windows app such as Irfanview (via commandline using cfexecute) to process images.
或者,您也可以编写 Irfanview 等 Windows 应用程序的脚本(通过命令行使用 cfexecute)来处理图像。
Hope that helps
希望有帮助
回答by James Moberg
I know that this question is old, but I still encounter problems with CMYK images & ColdFusion. However, I just read a CMYK JPEG image using ColdFusion 10 and resaved it. The saved image was able to to be read using ColdFusion 9 (which is only capable of reading RGB JPEGs.) I'm not sure if this conversion is intentional or not and I don't currently have any way of identifying whether the source image's color profile is CMYK or not as the saved color profile still appears to be the same.
我知道这个问题很老,但我仍然遇到 CMYK 图像和 ColdFusion 的问题。但是,我只是使用 ColdFusion 10 读取了 CMYK JPEG 图像并重新保存了它。可以使用 ColdFusion 9 读取保存的图像(它只能读取 RGB JPEG。)我不确定这种转换是否是有意的,我目前没有任何方法来识别源图像的颜色配置文件是否为 CMYK,因为保存的颜色配置文件看起来仍然相同。
<cfset imgData = ImageRead(expandPath("./CMYK_image.jpg"))>
<cfset ImageWrite(imgData, expandPath("./Saved_image.jpg"))>

