-1

I wanted to read a grayscale image values into a 2d array, change the values of the 2d array and create a new grayscale image from the modified values of 2d array.

Following is the code to read the grayscale image into 2D array(arr[][]).

package dct;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.File;
import javax.imageio.ImageIO;

/**
 *
 * @author jain
 */
public class writeGR {

            public static void main(String[] args)
            {
                File file = new File("lightning.jpg");
                BufferedImage img = null;
                try
                {
                    img = ImageIO.read(file);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

                int width = img.getWidth();
                int height = img.getHeight();
                int[][] arr = new int[width][height];

                Raster raster = img.getData();

                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        arr[i][j] = raster.getSample(i, j, 0);

                    }
                }

                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
                image.setData(raster);
                //
                //BufferedImage image1 = image;
                try
                {
                    File ouptut = new File("grayscale.png");
                    ImageIO.write(image, "png", ouptut);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }    

        }// main

}

Now I want to change the "arr[][]" vlaues and create a new grayscale image out of that. How to do this?

Rohit S
  • 395
  • 4
  • 18
  • If you want to change the values of arr, just loop through them again as you did before and say for instance: `arr[i][j] = arr[i][j]/2;` – Roel Strolenberg Jun 23 '15 at 08:26
  • @Roel - Assume I did "arr[i][j] = arr[i][j]/2;" . Now how to write the image with new "arr[][]" values?? – Rohit S Jun 23 '15 at 09:02
  • http://stackoverflow.com/questions/14416107/int-array-to-bufferedimage explains how to go from int[][] to bufferedImage. Then just save the image =) – Roel Strolenberg Jun 23 '15 at 09:33

1 Answers1

0

The above problem can be solved by the following code:

                for(int i=0;i<width;i++)
                {
                    for(int j=0;j<height;j++)
                    {
                        tempArr[0] = 100;
                        raster1.setPixel(i, j, tempArr);
                    }
                }

                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
                image.setData(raster1);

                try
                {
                    File ouptut = new File("change.png");
                    ImageIO.write(image, "png", ouptut);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }    
Rohit S
  • 395
  • 4
  • 18