【问题标题】:Read in Bufferedimage pixel values then manipulate each one and write to file读取 Bufferedimage 像素值,然后操作每个像素值并写入文件
【发布时间】:2015-06-16 18:32:22
【问题描述】:

我目前正在尝试逐像素读取图像并将每个彩色像素更改为 (100,100,100) 的 rgb 值。无论出于何种原因,当我检查每个像素的值时,图像被保存,它的所有彩色像素都为 (46,46,46)。

这是原图

运行我的程序后,这是它给我的图像

这里是代码

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Cmaps {
    public static void main(String[] args){
        File originalImage = new File("C:\\Users\\Me\\Desktop\\0005.bmp");
        BufferedImage img = null;
        try{
            img = ImageIO.read(originalImage);
            for(int i = 0; i < img.getHeight(); i++){
                for(int j = 0; j < img.getWidth(); j++){
                    //get the rgb color of the image and store it
                    Color c = new Color(img.getRGB(i, j));
                    int r = c.getRed();
                    int g = c.getGreen();
                    int b = c.getBlue();
                    //if the pixel is white then leave it alone
                    if((r == 255) && (g == 255) && (b == 255)){
                        img.setRGB(i, j, c.getRGB());
                        continue;
                    }
                    //set the colored pixel to 100,100,100
                    r = 100;// red component 0...255
                    g = 100;// green component 0...255
                    b = 100;// blue component 0...255
                    int col = (r << 16) | (g << 8) | b;
                    img.setRGB(i, j, col);
                }
            }
            File f = new File("C:\\Users\\Me\\Desktop\\2\\1.png");
            try {
                ImageIO.write(img, "PNG", f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

我不知道为什么它没有将像素设置为预期的 rgb 值。当我在 x 和 y 中向下移动行和列时,我最终希望能够基本上增加 rgb 颜色,所以最终图像的外观是它会在左上角开始变暗,然后有淡出效果当您从那一侧到达右下角时。

【问题讨论】:

  • 因为您的 rgb (int col = (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;) 设置为固定值。使用rg 和/或b 值并尝试例如将它们中的一个(全部或几个)乘以ij,然后你'会看到区别的。无论如何,您总是设置相同的值 ((100 &lt;&lt; 16) | (100 &lt;&lt; 8) | 100)
  • 问题是即使我这样设置它们也不会以 (100,100,100) 的形式出现。相反,它们会以 46、46、46 的形式出现,我不知道它是从哪里得到这个数字的
  • 在调用img.setRGB(i, j, col); 之前,请尝试使用col |= 0xff000000; 设置alpha。另外,请注意 getRGB 和 setRGB 的参数是 x 和 y 坐标,但是您首先传递 y 坐标 (i),然后传递 x 坐标 (j)。对于方形图像,这无关紧要,但对于其他图像,您会得到一个例外。
  • 您的BufferedImage 是什么类型的(img.getType())?如果它是 BMP,它可能有一个 IndexColorModel(调色板)。使用这样的颜色模型,您无法将像素的颜色设置为任意值。如果您尝试,该值将更改为调色板中最匹配的条目的值。
  • Haraldk 你就是那个男人。这是一个 BMP 文件,当我使用 png 文件时,它完全按预期工作。谢谢你一百万次,感谢所有给我反馈的人

标签: java rgb bufferedimage pixels bitmapimage


【解决方案1】:

好的,基于 cmets:

如果BufferedImage 具有IndexColorModel(基于调色板的颜色模型),则使用setRGB 将像素设置为任意RGB 值将不起作用。相反,将查找颜色,并且像素将获得被认为是调色板中最接近匹配的颜色。

使用 ImageIO 读取时,BMP、GIF 和 PNG 等格式都可能使用 IndexColorModel

要将图像转换为“真彩色”(Java 中的 DirectColorModelComponentColorModel 都可以),您可以使用:

BufferedImage img; // your original palette image

int w = img.getWidth();
int h = img.getHeight();
BufferedImage trueColor = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

Graphics2D g = trueColor.createGraphics();
try {
    g.drawImage(img, 0, 0, null);
}
finally {
    g.dispose();
}

img = trueColor;

在此之后,getRGB(x, y) 应该返回您指定的内容,使用 setRGB(x, y, argb)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多