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

Discussion in 'Java' started by ssecorp, Jul 7, 2008.

  1. ssecorp

    ssecorp Guest

    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
     
    ssecorp, Jul 7, 2008
    #1
    1. Advertising

  2. 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
     
    Joshua Cranmer, Jul 7, 2008
    #2
    1. Advertising

  3. ssecorp

    Tom Anderson Guest

    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
     
    Tom Anderson, Jul 7, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trint Smith
    Replies:
    2
    Views:
    368
    Brad Williams
    Feb 17, 2004
  2. Peter the Swede

    Pixels colorvalue in an image.

    Peter the Swede, Nov 11, 2003, in forum: Java
    Replies:
    0
    Views:
    323
    Peter the Swede
    Nov 11, 2003
  3. Read pixels from a PGM/PPM Image

    , May 23, 2005, in forum: C Programming
    Replies:
    2
    Views:
    1,679
    Malcolm
    May 24, 2005
  4. jimgardener
    Replies:
    0
    Views:
    302
    jimgardener
    Feb 17, 2008
  5. maestro
    Replies:
    13
    Views:
    632
Loading...

Share This Page