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

M

maestro

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

Stefan Ram

maestro said:
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(); }}}
 
S

Stefan Ram

Supersedes: <[email protected]>

maestro said:
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(); }}}
 
T

Tom Anderson

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
 
K

Knute Johnson

maestro said:
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);
}
});
}
}
 
M

Mark Space

Tom said:
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()) ;
}
}
 
T

Tom Anderson

Tom said:
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
 
C

conrad

John B. Matthews said:

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

John B. Matthews

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: said:
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!
 
A

Andrew Thompson

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

Lew

Cathy B. Matthews said:

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

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top