How can I easily open and check pixels of an image?



I want to open a bunch of pictures and get their pixels.
i want also to be able to display them

this is easy enoguh in python but seems very complicated in Java.

what library do you recommend?

from PIL import Image
import os

print os.path.exists('C:/Users/saftarn/Desktop/images/blob.jpg')

im2 ='C:/Users/saftarn/Desktop/images/blob.jpg')
for x in range(1,200):
for y in range(1,200):
color = im2.getpixel((x,y))
if color != (255,255,255):
print x,y,color

Tom Anderson

import java.awt.image.BufferedImage ;
import javax.imageio.ImageIO ;
import ;
import ;

public void getPixels(String filename) throws IOException {
BufferedImage img = File(filename)) ;
for (int y = 0 ; y < 200 ; ++y) {
for (int x = 0 ; x < 200 ; ++x) {
int p = img.getRGB(x, y) ;
if (p != 0xffffff) System.err.println("" + x + ", " + y + ": " + Integer.toHexString(p)) ;

Doesn't seem complicated to me. You can do img.getRaster() and use that if
you want more nuanced access to the pixel data.

Displaying an image also isn't hard - it's not quite as easy as PIL's
one-line version, but:

import java.awt.Canvas ;
import java.awt.Image ;
import java.awt.Graphics ;

public class ImageCanvas extends Canvas {
private Image img ;
private Dimension preferredSize ;

public ImageCanvas(Image img) {
this.img = img ;
preferredSize = new Dimension(img.getWidth(), img.getHeight()) ;
public void update(Graphics g) {
g.drawImage(img, 0, 0, null) ;
public Dimension getPreferredSize() {
return preferredSize ;

Then just make one and display it in a Frame.
<> does
pixel grabbing.

As for display, it's built in various places along the AWT and swing
frameworks. Check the Java tutorials for more information.

That's a weird little class. I think it's for extracting pixels from
Images which are not necessarily BufferedImages, such as ones you get from
the AWT toolkit. If you're using javax.imageio, you don't need to worry
about this.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads