我需要将图像从 CMYK 转换为 RGB - 不一定再转换回来,但是嘿,如果可以做到的话......

随着 ColdFusion 8 的发布,我们得到了 CF图像 标签,但不支持这种转换;也没有 图像.cfc, , 或者 Alagad 的图像组件.

不过,在Java中应该是可以的;我们可以通过 CF 来利用它。例如,以下是创建 Java 线程来休眠进程的方法:

<cfset jthread = createObject("java", "java.lang.Thread")/>
<cfset jthread.sleep(5000)/>

我猜想可以使用类似的方法来利用 java 来进行图像转换,但由于不是 Java 开发人员,我不知道从哪里开始。有人可以帮忙吗?

有帮助吗?

解决方案

我使用 Java ImageIO 库(https://jai-imageio.dev.java.net)。它们并不完美,但可以很简单并完成工作。至于从 CMYK 转换为 RGB,这是我能想到的最好的方法。

下载并安装适用于您的平台的 ImageIO JAR 和本机库。本机库是必不可少的。如果没有它们,ImageIO JAR 文件将无法检测 CMYK 图像。最初,我的印象是本机库可以提高性能,但不是任何功能所必需的。我错了。

我注意到的另一件事是,转换后的 RGB 图像有时比 CMYK 图像亮得多。如果有人知道如何解决这个问题,我将不胜感激。

下面是一些将 CMYK 图像转换为任何受支持格式的 RGB 图像的代码。

谢谢你,
兰迪·斯泰格鲍尔

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;
    }
}

其他提示

从 CMYK 转换为 RGB 并忽略所有颜色配置文件的一个非常简单的公式是:

    R = ( (255-C)*(255-K) ) / 255;
    G = ( (255-M)*(255-K) ) / 255;
    B = ( (255-Y)*(255-K) ) / 255;

此代码要求 CMYK 值在 0-255 范围内。如果您有 0 到 100 或 0.0 到 1.0,则必须转换这些值。

希望这能让您开始。

至于java和ColdFusion接口,我很抱歉,但我不知道该怎么做。

标签 cfx_image 可能对您有用。我已经有一段时间没有使用它了,但我记得它有很多功能。

或者,您可以编写 Windows 应用程序(例如 Irfanview)脚本(通过使用 cfexecute 的命令行)来处理图像。

希望有帮助

我知道这个问题很老了,但我仍然遇到 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"))>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top