I would like to ask for some idea on how to create an image from the array of pixels that I have. The image should be in black and white (since the input image is black and white as well).

Thank you.

Something like this?

BufferedImage image = null;

           for(int i=0; i<height; i++){
               for(int j=0; j<width; i++){
                   image.setRGB(j, i, pixels[ctr] & 0x00ffffff);

where pixels[] values ranges from 0 to 255. The image does not appear and when I tried to print the values, it does not work either. I have initialized the width and height. :/

If the variable image is null, how does your code execute?
I would expect you to get an NullPointerException.

Nope, I did not receive any NullPointerException. If image should not be null, what will I put then? I tried "new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)" but it does not work either. Or maybe, I'll use createGraphics()? :/

The first code fragment you posted will definitely throw an NPE if it executes - do you have a try with an empty catch somewhere? Your inner loop has i++ where it should have j++ so it would loop infinitely if you ever managed to execute it. The fact that it didn't implies that height and/or width == 0, despite your belief that hey are not.

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
is the best place to start. In what way does that "not work either"?

Oops, my bad. I did not notice it. But it did not throw an exception though my testing statements stopped in an abrupt manner.

As for the other one, it does not produce any image. I try to change "BufferedImage.TYPE_INT_ARGB" into "BufferedImage.TYPE_BYTE_GRAY". Though it produces an image, it becomes darker. I wonder if it is because of my code or do I need to put some adjustments in the pixel values because when I get the pixels in the first part of my code, I include "& 0x000000ff".

You haven't said what format your original pixel data is in. You said black and white, but your later posts seem to imply greyscale. How many bits per pixel does your source data have? And if the pixels[] array isn't an array of bytes, how are the pixel bits placed in the array's ints?

It's actually grayscale though I wonder if it is the correct statement to use so I describe it as "black and white".

In the code:

public static int[] getAllPixels(BufferedImage bi){
		width = bi.getWidth(null);
		height = bi.getHeight(null);
		rgbs = new int[width*height];
		rgbs = bi.getRGB(0, 0, width, height, rgbs, 0, width);
		return rgbs;

One of rgbs' values appears "-12632257" (and if I apply & 0x000000ff, it will appears like 70-something)

My pixels[] is declared as int[]. The contents of it is something like:
[...154, 0, 0, 0, 155, 255, 10...] (values of pixels[] range from 0-255)

OK, your source data is 8 bit greyscale, so TYPE_BYTE_GRAY is correct. What do you mean by "it becomes darker"? Do you mean the byte greyscale values are reduced in magnitude?

I suggest you create a really small file to simplify testing, maybe 256 x 1 pixels containing a linear black to white gradient so you will expect values 0-255.
When you create the BufferedImage with TYPE_BYTE_GRAY and populate its data, what are the pixel values in it, and how do they compare to the pixel values in your source data?

When I try to run the program and inserted some test statements, the pixel value from a certain part of the source data is 63 (-12632257) and when I applied my codes in relation to histogram equalization, its new value is supposedly 65. But when the array of the new pixels were set in the new image via setRGB method, its pixel value suddenly becomes 22 (-15329770).

Hold on a minute! "my codes in relation to histogram equalization"!
All along we have been trying to fix the problem that your image is not being created correctly. Where does "my codes in relation to histogram equalization" come in?
Have you been executing some code (which we never knew about) that modifies the pixels?

Yep, you were asking the relationship of the pixels in source data and the one I create right? But that "histogram equalization" part is already OK now so my only problem is creating an image from the new pixels that I have.
It's like, if I get all the pixels in the new image, that part of the image should be 65 (from its previous value 63), not 22.

Edited 4 Years Ago by chixm8_49: n/a

I suggest you comment out the eq. calls for now and just confirm the new image creation. If the source value for a pixel is 63, without any eq. processing commented out, you should be creating a BufferedImage with a pixel value of 63. I would test that first.
The secret of debugging is to debug one thing at at time.

It still changed.
Original pixel is 63
But after I create the image using the same pixels[], it became 66.

public static int[] getAllPixels(BufferedImage bi){
                int[] rgbs;
		width = bi.getWidth(null);
		height = bi.getHeight(null);
		rgbs = new int[width*height];
		rgbs = bi.getRGB(0, 0, width, height, rgbs, 0, width);
                //just for checking purposes only
		for(int i=18800; i<18815; i++){
				int value = rgbs[i] & 0x000000ff;
                                //int value = rgbs[i];
				System.out.println("i="+i+" ->pixel "+ value);
		return rgbs;
public static BufferedImage creatingImage(int[] pixels){
            BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            int ctr=0;
            for(int i=0; i<height; i++){
               for(int j=0; j<width; j++){               
                   newImage.setRGB(i, j, pixels[ctr]);
                   //I tried to use newImage.setRGB(i, j, pixels[ctr]&0xFFFFFF00);
                   //The new pixel's value changed into 64 instead of 66.
           //just to check for the Pixels
           int[] dummy = ImageFcn.getAllPixels(newImage);                     
           return newImage;
for(int i=0; i<height; i++){
  for(int j=0; j<width; j++){
    newImage.setRGB(i, j, pixels[ctr]);

Is this deliberate - i would normally be the width (x coord) and J the height (y coord) but your loops have the boundary conditions reversed

The new image does not appear/appears wrong to the window after I set the new pixels.
I tried JamesCherrill's suggestion to comment out the functions that make the new pixels and try to make the original image appear. After then, I'll try to make the new image appears.

This is what usually happens. If I use:
TYPE_INT_ARGB - the original appears but the image with the new pixels do not.
TYPE_BYTE/USHORT_GRAY - the original do not but the image appears, in a very dark tone (the pixel number suddenly changes from supposedly 65 to 13)
I forgot the other one but the original did not appear and the new image will have blue and black colors.

How are you testing?
Do you have a small image, say 5X5, with known color(s) for all the pixels.
Then you can print out the values of all the pixels before and after the changes.

Edited 4 Years Ago by NormR1: n/a

It's a 200x200 (can't do a smaller image) then I'll print a certain part of the image for its corresponding pixel, before and after its change.

For testing you should use a small image that you can see exactly what is happening to every pixel.

I used a small image now but nothing appears, either the original version or the image with new pixels.

now but nothing appears either the original version or the image with new pixels

How can the original image not appear? If you do not change the original then it will always be the same????

Did you print out all of the pixels for the image for before and after the changes you make to them to verify that they were changed as you want them to be?

This article has been dead for over six months. Start a new discussion instead.