Modify image and save to file

T

Tim

I want to read a .png image from a file, replace a color with a new color,
and write it back to a .png file.

I've managed to read the file into a BufferedImage. I've got a PixelGrabber
to look at the pixels, identify those of the old color, and replace them
with the new color, leaving me with an int[] of the pixels I want.

How do I modify write the modified pixels out to a .png file? I don't care
if I overwrite the original image or write to a new one.

No matter how I try to do it I end up with a new file that is identical to
the original, even though I can tell my code is correctly identifying the
pixels I want to change.

Please help,
Tim
 
J

jan V

Tim said:
I want to read a .png image from a file, replace a color with a new color,
and write it back to a .png file.

Have you had a look at the javax.imageio.* package? Once you've got your
int[] containing the modified image, the image I/O framework should be your
image's way out of the JVM :)
 
T

Tim

Here's a some sample code that I'm trying to use. I need some specific info
about how to get from an int[] of pixels to an image file on the disk. I
think I need to either construct a completely new image from the new pixels,
or update the original image with the new pixel values. I'm hoping for
details on how to do one of those.

Thanks,
Tim
----------------------
File imgFile = new File("myImage.png");

// get BufferedImage
BufferedImage image = ImageIO.read(imgFile);

int x = image.getMinX();
int y = image.getMinY();
int w = image.getWidth();
int h = image.getHeight();

// get image pixels
int[] oldPixels = new int[w * h];
PixelGrabber grabber = new PixelGrabber(image, x, y, w, h, oldPixels, 0,
w);

// modify pixels
int[] newPixels = replaceColor(oldPixels);

// NOW WHAT? How to write to a file?
 
P

Paul Bilnoski

Tim said:
Here's a some sample code that I'm trying to use. I need some specific info
about how to get from an int[] of pixels to an image file on the disk. I
think I need to either construct a completely new image from the new pixels,
or update the original image with the new pixel values. I'm hoping for
details on how to do one of those.

Thanks,
Tim
----------------------
File imgFile = new File("myImage.png");

// get BufferedImage
BufferedImage image = ImageIO.read(imgFile);

int x = image.getMinX();
int y = image.getMinY();
int w = image.getWidth();
int h = image.getHeight();

// get image pixels
int[] oldPixels = new int[w * h];
PixelGrabber grabber = new PixelGrabber(image, x, y, w, h, oldPixels, 0,
w);

// modify pixels
int[] newPixels = replaceColor(oldPixels);

// NOW WHAT? How to write to a file?

1) I am not sure, but ImageIO.read() may be asynchronous. You'll have to
make sure the whole image has been read before continuing.

One of the 2D tutorials has this code:
http://java.sun.com/docs/books/tutorial/2d/problems/
"To create a BufferedImage from a gif or jpeg, you load your gif or jpeg
into an Image object and then draw the Image to the BufferedImage object"

Image img = Toolkit.getDefaultToolkit().getImage("picture.gif");
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {}
int width = img.getWidth(this);
int height = img.getHeight(this);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(img, 0, 0, null);

2) If you already have a BufferedImage and you're replacing a few
pixels, why not use the getRGB/setRGB methods there? Those should change
the pixels in-place and you can write out the same BufferedImage.

If each of those int values is an RGB, you should be able to use the
setRGB overload in BufferedImage that takes an array as an argument.

--Paul
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top