Get Pixels from Image using Java

Here I am going to show you how you can extract pixels from an image using Java. So I am going to read the image to extract pixels from the image. There are several kinds of image formats such as JPEG, GIF, PNG, BMP, TIFF etc.

Java has built-in Image I/O API in javax.imageio package and it can be used to load external image formats into its BufferedImage formats. By default, Image I/O supports image formats such as JPEG, PNG, GIF, BMP, WBMP. It can also be extensible to support additional image formats for TIFF and JPEG 2000.

Image I/O recognizes the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.

Image can be load or read using following code snippets, where imagePath is the full path with image name from where the image can be read.

try {
  BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
} catch (Exception e) {
  e.printStackTrace();
}

You can save the below sample image for reading and extracting pixels using Java.

get pixels from image

The whole source code is given below for getting pixels from an image.

package com.roytuts.java.pixels.from.image;

import java.awt.image.BufferedImage;
import java.awt.image.ImagingOpException;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageToPixels {

	private static final String IMAGE_EXT_JPG = "jpg";
	private static final String IMAGE_EXT_JPEG = "jpeg";
	private static final String IMAGE_EXT_PNG = "png";
	private static final String IMAGE_EXT_GIF = "gif";
	/**
	 * Exception message to be thrown when allowed image types are not read
	 */
	public static final String IMAGE_ALLOW_TYPES = "Image types allowed - " + IMAGE_EXT_JPG + IMAGE_EXT_JPEG
			+ IMAGE_EXT_PNG + IMAGE_EXT_GIF;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * Read the image, Desert.jpg, from Desktop/Pictures
		 */
		BufferedImage bufferedImage = getImage("Chrysanthemum.jpg");
		
		/**
		 * Get pixels from the above image and display in the console
		 */
		int[][] pixels = getImageToPixels(bufferedImage);
		for (int i = 0; i < pixels.length; i++) {
			for (int j = 0; j < pixels[0].length; j++) {
				System.out.print(pixels[i][j] + " ");
			}
			System.out.println();
		}
	}

	/**
	 * Read image and return BufferedImage imageFullPath - full path of the image
	 * with image file name
	 */
	public static BufferedImage getImage(String imageFullPath) {
		BufferedImage bufferedImage = null;
		try {
			if (imageFullPath == null) {
				throw new NullPointerException("Image full path cannot be null or empty");
			}
			
			/**
			 * Check if the selected file is an image
			 */
			boolean isImage = isFileAnImage(imageFullPath);
			
			if (!isImage) {
				throw new ImagingOpException(IMAGE_ALLOW_TYPES);
			}
			
			String imagePath = imageFullPath;
			
			/**
			 * get BufferedImage and return it
			 */
			bufferedImage = ImageIO.read(new File(imagePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bufferedImage;
	}

	/**
	 * Get pixels in two dimensional array bufferedImage - get the BufferedImage
	 * instance from the image
	 */
	public static int[][] getImageToPixels(BufferedImage bufferedImage) {
		if (bufferedImage == null) {
			throw new IllegalArgumentException();
		}
		int h = bufferedImage.getHeight();
		int w = bufferedImage.getWidth();
		int[][] pixels = new int[h][w];
		for (int i = 0; i < h; i++) {
			/**
			 * get pixels from image
			 */
			bufferedImage.getRGB(0, i, w, 1, pixels[i], 0, w);
		}
		return pixels;
	}

	/**
	 * Check a file is an image imageName - image file name with extension
	 */
	private static boolean isFileAnImage(String imageName) {
		if (imageName == null) {
			throw new NullPointerException("Image full path cannot be null or empty");
		}
		File imageFile = new File(imageName);
		String ext = getFileExtension(imageFile);
		if (IMAGE_EXT_GIF.equalsIgnoreCase(ext) || IMAGE_EXT_JPEG.equalsIgnoreCase(ext)
				|| IMAGE_EXT_JPG.equalsIgnoreCase(ext) || IMAGE_EXT_PNG.equalsIgnoreCase(ext)) {
			return true;
		}
		return false;
	}

	/**
	 * Get file extension from the file
	 *
	 * @param file - file
	 */
	public static String getFileExtension(File file) {
		if (file == null) {
			throw new NullPointerException("Image file cannot be null");
		}
		String name = file.getName();
		int lastDotIndex = name.lastIndexOf(".");
		if (lastDotIndex > 0 && lastDotIndex < (name.length() - 1)) {
			return name.substring(lastDotIndex + 1).toLowerCase();
		}
		return "";
	}

}

Running the above code you will get the pixels are displayed in the console.

Source Code

Download

That’s all. Thanks for reading.

1 thought on “Get Pixels from Image using Java

  1. i tried your code because I need the pixel values of an image. I was successful in running your code but the output values are mostly large negative numbers (ex. -7747839) and not between 0 and 255 as I expected for a pixel value.

    (I am novice to programming but have accumulated decent experience in it. )

    I hope someone can correct or guide me as to where did I go wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *