views:

103

answers:

2

Hi there, my name is Daniel and I'm from Spain.

I am developing a small program which cuts images by the color.

That's will be easiest to explain using an example image. I have got this image:

http://dl.dropbox.com/u/944667/Ejemplo1.png

And I want to create a new image just with the purple form, without the black frame.

Does anyone have any idea? I am using Java 2D so I think that I need to create an Object "Shape" with the purple area of the first image.

Thank you!!!

Best regards, Daniel.

+1  A: 

You need to use some flood-fill algorithm that finds the boundries of the purple area:

Wikipedia has a page on it with excellent pseudo code and animations.

http://en.wikipedia.org/wiki/Flood_fill

aioobe
Thanks for your answer!! Neil Coffey gave me the best solution, but thanks anyway!
dafero
+2  A: 

If the image is literally like the one you show, you could:

  • load the image into a BufferedImage (with ImageIO.read())
  • create a new BufferedImage of the same size, ensuring it has an alpha layer (e.g. set its type to BufferedImage.TYPE_4BYTE_ABGR)
  • "manually" go through each pixel in turn in the loaded BufferedImage, getting the pixel colour with getRGB() and checking if it's black
  • if the colour is black, set the corresponding pixel to transparent in the new image, else to the original colour from the first image (see setRGB() method)
  • save the new image (with ImageIO.write())

There are fancier ways, but this simple method is nice and understandable and will work fine for images of the type you showed.

Neil Coffey
Great answer! At the end I found the solution! And all thanks to you!Thanks again!
dafero