Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I easily open and check pixels of an image?

Reply
Thread Tools

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

 
 
ssecorp
Guest
Posts: n/a
 
      07-07-2008
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
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      07-07-2008
ssecorp wrote:
> 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?


<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.


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      07-07-2008
On Mon, 7 Jul 2008, Joshua Cranmer wrote:

> ssecorp wrote:
>
>> 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.


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.

>> what library do you recommend?

>
> <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

--
There are lousy reviews, and then there's empirical shitness. -- pikelet
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why don't Sony and Pentax have this problem? Dead pixels, defective pixels RichA Digital Photography 9 04-12-2011 08:54 AM
Can hot pixels become dead pixels? kl_tom Digital Photography 4 10-05-2006 06:52 PM
Pse explain "3.1m effective pixels and 6.0m redorded pixels" notreallyme Digital Photography 14 12-28-2003 03:41 PM
What's the difference between effective pixels and recorded pixels? Mark Grady Digital Photography 10 09-28-2003 11:11 PM



Advertisments