1

I would like to convert an array of pixels to images so that I can read each pixel of the image, I've converted the image to pixels and now what I'm trying to do is that I have a picture of an RGB circle , After converting the image to pixels I want to draw images again but Not the entire picture again , I want to draw the red circle alone , the green circle alone and the blue circle alone and store the images in a file , how can that be done ?

Here's the code that I used to convert the image to array pixels :

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

public class BMPtoArray {
public static int[] convertToPixels(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixel = new int[width * height];

    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        throw new IllegalStateException("Error: Image Fetch Aborted");
    }
    return pixel;
}
public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("C:\\Users\\Mhamd\\Desktop\\3rd year 
first semester\\Analysis and Coding\\labs\\2.2Java\\src\\circleRGB.bmp"));
    //System.out.println(convertToPixels(image));
}
}

Here's the image :

RGBCirle

so this is basically what I'm trying to achieve

[![This is what I want to Achieve][2]][2]

  • You might want to look at cropping the image, rather than manipulating pixels. https://stackoverflow.com/questions/2386064/how-do-i-crop-an-image-in-java – computercarguy Nov 24 '21 at 17:18
  • Here's a couple more links, some including libraries that might help: https://stackoverflow.com/questions/7894275/cropping-an-image-in-java and https://stackoverflow.com/questions/23610007/crop-image-based-on-x-y-coordinates-in-java and https://stackoverflow.com/questions/5186597/how-crop-some-region-of-image-in-java – computercarguy Nov 24 '21 at 17:26
  • @computercarguy thanks but I don't want to crop the image , I want to keep the dimensions of the old image but rather than drawing all the circles , I want to draw each color on its own as in the picture above – Mohammed Hamdoon Nov 24 '21 at 17:28
  • I was thinking you wanted to keep the color interaction of the overlap, but if that's not the case, then why not simply draw the circles in their own `BufferedImage` and save them separately? Even if the image is generated dynamically, you should be able to keep track of the individual changes, then recreate them in each of their own image. Or keep an array of `BufferedImage`s for each element that you never display. That'll take up lots of RAM, but it'd be similar to Layers in Photoshop and Gimp. – computercarguy Nov 24 '21 at 17:35
  • So you want to subtract certain colors? For example, subtracting red will leave just the blue and green circles? – 001 Nov 24 '21 at 17:36
  • @JohnnyMopp exactly and save that image in a file , just as in the picture above , you see different pictures are generated , each one has different colors , one has only red without the blue and green the other blue without red and green and like this – Mohammed Hamdoon Nov 24 '21 at 17:38
  • 1
    When you’re working with a `BufferedImage`, you don’t need such complicated approaches, see [`getRGB`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/image/BufferedImage.html#getRGB(int,int,int,int,int%5B%5D,int,int)) and [`setRGB`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/image/BufferedImage.html#setRGB(int,int,int,int,int%5B%5D,int,int)) – Holger Nov 24 '21 at 17:52

1 Answers1

1

Here's a simple example of how to "turn off" the red channel:

final BufferedImage image = ImageIO.read(new File("test.bmp"));
int red = 0x00FF0000;
for (int row = 0; row < image.getHeight(); row++) {
    for (int col = 0; col < image.getWidth(); col++) {
        int color = image.getRGB(col, row);
        color &= ~red;
        image.setRGB(col, row, color);
    }
}

This sets the red in each RGB pixel to 0. You can repeat for other colors and combinations.

This produces this from your source image:

Picture of just the blue and green balls

001
  • 13,291
  • 5
  • 35
  • 66