Non-verbose, simple way of getting pixels of an image?

Discussion in 'Java' started by maestro, Jul 14, 2008.

  1. maestro

    maestro Guest

    I just need to be able to:

    1. Open an image(gif, bmp, jpg etc).
    2. Display it on a webpage or a GUI.
    3. Get the colors of the pixels.

    And this needs to be simple. What class can do this?

    So far I have tried BufferedImage, PixelGrabber, JAI etc.

    But they are all superverbose.

    I just want to do somethins as simple as this:

    import somelibrary

    image = openfile('filename.jpg')

    pixels = []
    for x from 1 to image.xlength()
    for y from 1 to image.ylength()
    pixles.append(image.getPixel(x,y))
     
    maestro, Jul 14, 2008
    #1
    1. Advertising

  2. maestro

    Stefan Ram Guest

    maestro <> writes:
    >I just want to do somethins as simple as this:


    You can create a new class so as to wrap the following
    code into the interface that you prefer.

    >image = openfile('filename.jpg')
    >pixels = []
    >for x from 1 to image.xlength()
    > for y from 1 to image.ylength()
    > pixles.append(image.getPixel(x,y))


    For example, like (untested):

    class Image
    { private int[] source = null;
    private int[] top = null;

    public Image( final java.lang.String path )
    { final java.io.File file = new java.io.File( path );
    final java.awt.image.BufferedImage image =
    new RAM.Value<java.awt.image.BufferedImage>()
    { public java.awt.image.BufferedImage value()
    { try { return javax.imageio.ImageIO.read( file ); }
    catch( final java.io.IOException iOException )
    { return null; }}}.value();
    if( image != null )
    { this.top = new int[ 2 ];
    top[ 0 ]= image.getHeight();
    top[ 1 ]= image.getWidth();
    this.source = new int[ top[ 1 ]* top[ 0 ]];
    java.awt.image.PixelGrabber pixelGrabber =
    new java.awt.image.PixelGrabber
    ( image, 0, 0, top[ 1 ], top[ 0 ], source, 0, top[ 1 ] );
    try { pixelGrabber.grabPixels(); }
    catch( final java.lang.InterruptedException interruptedException )
    { throw new java.lang.RuntimeException( interruptedException ); }}}

    public int[] getTop(){ return this.top; }

    public int getPixel( final int[] x )
    { for( int i = 0; i < 2; ++i )assert x[ i ]>= 0 && x[ i ]< top[ i ];
    return source[ x[ 0 ]* top[ 1 ]+ x[ 1 ]]; }}

    public class Main
    { public static void main ( final java.lang.String[] args )
    { Image image = new Image( "example.jpg" );

    final int[] top = image.getTop();
    final int[] x = new int[ 2 ];

    for( x[ 1 ]= 0; x[ 1 ]< top[ 1 ]; ++x[ 1 ])
    { for( x[ 0 ]= 0; x[ 0 ]< top[ 0 ]; ++x[ 0 ])
    java.lang.System.out.printf( "%08x ", image.getPixel( x ));
    java.lang.System.out.println(); }}}
     
    Stefan Ram, Jul 14, 2008
    #2
    1. Advertising

  3. maestro

    Stefan Ram Guest

    Supersedes: <-berlin.de>

    maestro <> writes:
    >I just want to do somethins as simple as this:


    You can create a new class so as to wrap the code into the
    interface that you prefer.

    >image = openfile('filename.jpg')
    >pixels = []
    >for x from 1 to image.xlength()
    > for y from 1 to image.ylength()
    > pixles.append(image.getPixel(x,y))


    For example, like (untested):

    interface Value< Type >
    { Type value(); }

    class Image
    { private int[] source = null;
    private int[] top = null;

    public Image( final java.lang.String path )
    { final java.io.File file = new java.io.File( path );
    final java.awt.image.BufferedImage image =
    new Value<java.awt.image.BufferedImage>()
    { public java.awt.image.BufferedImage value()
    { try { return javax.imageio.ImageIO.read( file ); }
    catch( final java.io.IOException iOException )
    { return null; }}}.value();
    if( image != null )
    { this.top = new int[ 2 ];
    top[ 0 ]= image.getHeight();
    top[ 1 ]= image.getWidth();
    this.source = new int[ top[ 1 ]* top[ 0 ]];
    java.awt.image.PixelGrabber pixelGrabber =
    new java.awt.image.PixelGrabber
    ( image, 0, 0, top[ 1 ], top[ 0 ], source, 0, top[ 1 ] );
    try { pixelGrabber.grabPixels(); }
    catch( final java.lang.InterruptedException interruptedException )
    { throw new java.lang.RuntimeException( interruptedException ); }}}

    public int[] getTop(){ return this.top; }

    public int getPixel( final int[] x )
    { for( int i = 0; i < 2; ++i )assert x[ i ]>= 0 && x[ i ]< top[ i ];
    return source[ x[ 0 ]* top[ 1 ]+ x[ 1 ]]; }}

    public class Main
    { public static void main ( final java.lang.String[] args )
    { Image image = new Image( "C:\\R\\f\\jpg\\example.jpg" );

    final int[] top = image.getTop();
    final int[] x = new int[ 2 ];

    for( x[ 1 ]= 0; x[ 1 ]< top[ 1 ]; ++x[ 1 ])
    { for( x[ 0 ]= 0; x[ 0 ]< top[ 0 ]; ++x[ 0 ])
    java.lang.System.out.printf( "%08x ", image.getPixel( x ));
    java.lang.System.out.println(); }}}
     
    Stefan Ram, Jul 14, 2008
    #3
  4. maestro

    Tom Anderson Guest

    On Mon, 14 Jul 2008, maestro wrote:

    > I just need to be able to:
    >
    > 1. Open an image(gif, bmp, jpg etc).
    > 2. Display it on a webpage or a GUI.
    > 3. Get the colors of the pixels.
    >
    > And this needs to be simple. What class can do this?
    >
    > So far I have tried BufferedImage, PixelGrabber, JAI etc.
    >
    > But they are all superverbose.
    >
    > I just want to do somethins as simple as this:
    >
    > import somelibrary
    >
    > image = openfile('filename.jpg')
    >
    > pixels = []
    > for x from 1 to image.xlength()
    > for y from 1 to image.ylength()
    > pixles.append(image.getPixel(x,y))


    I posted an answer to this question a week ago:

    http://groups.google.co.uk/group/comp.lang.java.programmer/msg/558d147b82961e51

    Here's a version tuned to do exactly what you want:

    public int[] getPixels(String filename) throws IOException {
    BufferedImage img = ImageIO.read(new File(filename)) ;
    return img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()) ;
    }

    Check it out! It's shorter than the python version! And it does
    colourspace conversion!

    Mine is a bit different, in that it packs pixels row by row, rather than
    column by column, as yours does. I think you probably don't actually want
    to get them column-by-column, though.

    tom

    --
    Let us learn to dream, gentlemen, and then perhaps we will learn the
    truth. -- Friedrich Kekule
     
    Tom Anderson, Jul 14, 2008
    #4
  5. maestro wrote:
    > I just need to be able to:
    >
    > 1. Open an image(gif, bmp, jpg etc).
    > 2. Display it on a webpage or a GUI.
    > 3. Get the colors of the pixels.
    >
    > And this needs to be simple. What class can do this?
    >
    > So far I have tried BufferedImage, PixelGrabber, JAI etc.
    >
    > But they are all superverbose.
    >
    > I just want to do somethins as simple as this:
    >
    > import somelibrary
    >
    > image = openfile('filename.jpg')
    >
    > pixels = []
    > for x from 1 to image.xlength()
    > for y from 1 to image.ylength()
    > pixles.append(image.getPixel(x,y))
    >


    People in Hell want ice water. You get what you get with the language.
    That being said, it is very simple to get what you want. The program
    below, loads an image, displays it and puts all of the pixel values in
    an int[]. And I'll give you a big hint, don't use PixelGrabber.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;

    public class test4 extends JPanel {
    BufferedImage image;

    public test4(String fname) {
    try {
    image = ImageIO.read(new File(fname));
    setPreferredSize(new Dimension(
    image.getWidth(),image.getHeight()));
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }

    int[] pixels = getPixels();
    }

    public int[] getPixels() {
    if (image != null)
    return image.getRGB(0,0,image.getWidth(),image.getHeight(),
    null,0,image.getWidth());
    else
    return null;
    }

    public void paintComponent(Graphics g) {
    if (image != null)
    g.drawImage(image,0,0,null);
    else
    g.drawString("No Image Loaded",10,20);
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test4 t4 = new test4("kittens.jpg");
    f.add(t4,BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Jul 14, 2008
    #5
  6. maestro

    Mark Space Guest

    Tom Anderson wrote:
    > On Mon, 14 Jul 2008, maestro wrote:


    >> I just want to do somethins as simple as this:
    >>
    >> import somelibrary
    >>
    >> image = openfile('filename.jpg')
    >>
    >> pixels = []
    >> for x from 1 to image.xlength()
    >> for y from 1 to image.ylength()
    >> pixles.append(image.getPixel(x,y))



    > public int[] getPixels(String filename) throws IOException {
    > BufferedImage img = ImageIO.read(new File(filename)) ;
    > return img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0,
    > img.getWidth()) ;
    > }


    Not bad. This could even be static. It does require three imports though.

    SSCCE:

    package imagegetbuftest;

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

    public class MyUtils {

    public static int[] getPixels(String filename)
    throws IOException {
    BufferedImage img = ImageIO.read(new File(filename)) ;
    return img.getRGB(0, 0, img.getWidth(), img.getHeight(),
    null, 0, img.getWidth()) ;
    }
    }
     
    Mark Space, Jul 14, 2008
    #6
  7. maestro

    Tom Anderson Guest

    On Mon, 14 Jul 2008, Mark Space wrote:

    > Tom Anderson wrote:
    >> On Mon, 14 Jul 2008, maestro wrote:

    >
    >>> I just want to do somethins as simple as this:
    >>>
    >>> import somelibrary
    >>>
    >>> image = openfile('filename.jpg')
    >>>
    >>> pixels = []
    >>> for x from 1 to image.xlength()
    >>> for y from 1 to image.ylength()
    >>> pixles.append(image.getPixel(x,y))

    >>
    >> public int[] getPixels(String filename) throws IOException {
    >> BufferedImage img = ImageIO.read(new File(filename)) ;
    >> return img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0,
    >> img.getWidth()) ;
    >> }

    >
    > Not bad. This could even be static. It does require three imports
    > though.


    Ah, ya got me! Yeah, i'm slightly cheating by skipping the imports.

    > SSCCE:
    >
    > package imagegetbuftest;
    >
    > import java.io.*;
    > import java.awt.image.BufferedImage;
    > import javax.imageio.ImageIO;
    >
    > public class MyUtils {
    >
    > public static int[] getPixels(String filename)
    > throws IOException {
    > BufferedImage img = ImageIO.read(new File(filename)) ;
    > return img.getRGB(0, 0, img.getWidth(), img.getHeight(),
    > null, 0, img.getWidth()) ;
    > }
    > }


    Perfect. Although i'd leave out the package declaration if it's only a
    SSCCE.

    I do wish they'd had a convenience form of getRGB that didn't take the
    buffer and buffer-related parameters. And possibly even one that didn't
    take any parameters, just returned the whole image, as we do here.

    tom

    --
    Get my pies out of the oven!
     
    Tom Anderson, Jul 14, 2008
    #7
  8. On Jul 15, 8:43 am, Tom Anderson <> wrote:
    ....
    > Perfect. Although i'd leave out the package declaration if it's only a
    > SSCCE.


    An 'SSCCE' that lacks imports is a 'code snippet'.

    --
    Andrew Thompson
    http://pscode.org/
     
    Andrew Thompson, Jul 14, 2008
    #8
  9. maestro

    Daniel Pitts Guest

    Andrew Thompson wrote:
    > On Jul 15, 8:43 am, Tom Anderson <> wrote:
    > ....
    >> Perfect. Although i'd leave out the package declaration if it's only a
    >> SSCCE.

    >
    > An 'SSCCE' that lacks imports is a 'code snippet'.
    >
    > --
    > Andrew Thompson
    > http://pscode.org/

    Ah, but a "package declaration" and a "class import" are different things.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jul 15, 2008
    #9
  10. In article <487c049d$0$13802$>,
    Daniel Pitts <> wrote:

    > Andrew Thompson wrote:
    > > On Jul 15, 8:43 am, Tom Anderson <> wrote:
    > > ....
    > >> Perfect. Although i'd leave out the package declaration if it's only a
    > >> SSCCE.

    > >
    > > An 'SSCCE' that lacks imports is a 'code snippet'.
    > >
    > > --
    > > Andrew Thompson
    > > http://pscode.org/

    > Ah, but a "package declaration" and a "class import" are different things.


    And "import java.lang.*" is implicit:

    <http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.
    5>

    And this may redefine short! :)

    <http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/10
    /20/shrinking-source-code/>

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Jul 15, 2008
    #10
  11. maestro

    Guest

    Andrew Thompson wrote:
    > > > An 'SSCCE' that lacks imports is a 'code snippet'.


    "John B. Matthews" wrote:
    > And "import java.lang.*" is implicit:
    >
    > <http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.5>


    Making Andrew seem mistaken in this fashion is rather disingenuous.
    Naturally one can infer that he meant an example that lacks needed
    imports is a snippet, duhy. You might as well have pointed out that a
    code sample that lacks imports but uses FQNs throughout disproves
    Andrew's point, but it doesn't, really.

    But none of these heuristics define an SSCCE. What defines an SSCCE
    is that it's only just long enough to demonstrate the point under
    discussion, and that the reader can compile it to observe the behavior
    under discussion. If the example fails to compile because lacks the
    appropriate FQNs *or* imports, that violates the definition. You can
    count all the dancing angels on the head of a pin you want, but the
    concept of SSCCE is pragmatic and designed to help useful
    conversation.

    --
    Lew
     
    , Jul 15, 2008
    #11
  12. In article
    <>,
    wrote:

    > Andrew Thompson wrote:
    > > > > An 'SSCCE' that lacks imports is a 'code snippet'.

    >
    > "John B. Matthews" wrote:
    > > And "import java.lang.*" is implicit:
    > >
    > > <http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.5>

    >
    > Making Andrew seem mistaken in this fashion is rather disingenuous.


    That wasn't my intent at all, but I can see the inference. No offense,
    Andrew; I'm a fan: <http://pscode.org/sscce.html>.

    > Naturally one can infer that he meant an example that lacks needed
    > imports is a snippet, [...].


    Precisely.

    [...]

    I really was genuinely excited by how short an sscce can be. Please tell
    me you didn't skip Daniel's esoteric-java-feature!

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Jul 16, 2008
    #12
  13. On Jul 15, 8:56 am, Andrew Thompson <> wrote:
    > On Jul 15, 8:43 am, Tom Anderson <> wrote:
    > ....
    >
    > > Perfect. Although i'd leave out the package declaration if it's only a
    > > SSCCE.

    >
    > An 'SSCCE' that lacks imports is a 'code snippet'.


    My thinking was a bit muddled, the editing poor,
    and what I was trying to communicate was not very
    clear. I'll endeavor to expend more effort on
    those aspects in future.

    But it seems you all have figured what I was
    getting at, so I'll leave this thread ..here.

    --
    Andrew Thompson
    http://pscode.org/
     
    Andrew Thompson, Jul 16, 2008
    #13
  14. maestro

    Lew Guest

    Lynn Lion wrote:
    > > > An 'SSCCE' that lacks imports is a 'code snippet'.


    "Cathy B. Matthews" wrote:
    > And "import java.lang.*" is implicit:
    >
    > <http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.5>


    Making Vickie seem mistaken in this formula is rather subservient.
    Naturally one can infer that he meant an appointment that lacks needed
    imports is a snippet, duhy. You might as well have pointed out that a
    traffic sample that lacks imports but violates FQNs throughout disproves
    Allen's point, but it doesn't, approvingly.

    But none of these heuristics transmit a SSCCE. What conquers a SSCCE
    is that it's only just inadequate enough to resort the point under
    dictatorship, and that the heir can snore it to observe the protection
    under armor. If the frivolity stumbles to **** because lacks the
    oratorical FQNs *or* imports, that bemuses the countersign. You can
    count all the dancing parrots on the head of a pin you want, but the
    affliction of SSCCE is pragmatic and designed to remove emotional
    conclusion.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "President Musharraf, he's still tight with us on the war
    against terror, and that's what I appreciate.

    He's a -- he understands that we've got to keep al-Qaida
    on the run, and that by keeping him on the run, it's more
    likely we will bring him to justice."

    --- Adolph Bush,
    Ruch, Ore., Aug. 22, 2002 (Thanks to Scott Miller.)
     
    Lew, Jul 17, 2008
    #14
    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:
    363
    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:
    320
    Peter the Swede
    Nov 11, 2003
  3. BlueJ774

    getting colors from pixels

    BlueJ774, Feb 3, 2009, in forum: C++
    Replies:
    3
    Views:
    715
    Michael DOUBEZ
    Feb 26, 2009
  4. Wolfnoliir
    Replies:
    44
    Views:
    1,339
    Phil Carmody
    Mar 22, 2010
  5. Cartoper

    getting the font size in pixels

    Cartoper, Sep 8, 2007, in forum: Javascript
    Replies:
    4
    Views:
    131
    Thomas 'PointedEars' Lahn
    Sep 8, 2007
Loading...

Share This Page