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

S

ssecorp

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 = Image.open('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
 
T

Tom Anderson

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

public void getPixels(String filename) throws IOException {
BufferedImage img = ImageIO.read(new 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.
<http://java.sun.com/javase/6/docs/api/java/awt/image/PixelGrabber.html> 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.

tom
 

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

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top