Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Non-verbose, simple way of getting pixels of an image?

Reply
Thread Tools

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

 
 
maestro
Guest
Posts: n/a
 
      07-14-2008
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))

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-14-2008
maestro <(E-Mail Removed)> 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(); }}}

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-14-2008
Supersedes: <(E-Mail Removed)-berlin.de>

maestro <(E-Mail Removed)> 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(); }}}

 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      07-14-2008
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/com...8d147b82961e51

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
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      07-14-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))
>


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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-14-2008
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()) ;
}
}
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      07-14-2008
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!
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-14-2008
On Jul 15, 8:43*am, Tom Anderson <(E-Mail Removed)> 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/
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-15-2008
Andrew Thompson wrote:
> On Jul 15, 8:43 am, Tom Anderson <(E-Mail Removed)> 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/>
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      07-15-2008
In article <487c049d$0$13802$(E-Mail Removed)>,
Daniel Pitts <(E-Mail Removed)> wrote:

> Andrew Thompson wrote:
> > On Jul 15, 8:43 am, Tom Anderson <(E-Mail Removed)> 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/t...kages.html#7.5.
5>

And this may redefine short!

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

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
 
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
Why don't Sony and Pentax have this problem? Dead pixels, defective pixels RichA Digital Photography 9 04-12-2011 08:54 AM
getting colors from pixels BlueJ774 C++ 3 02-26-2009 08:35 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