rivu
rivu

Reputation: 2514

ImageIO not printing proper color

I am trying to read a PNG image file from disk, draw some rectangles on it and save the modified image on the disk. Here's the scala code:

//l is a list of Rectangle objects of the form (x1,x2,y1,y2)

val image = ImageIO.read(sourceimage);
val graph=image.createGraphics()
graph.setColor(Color.GREEN)
l.foreach(x=>graph.draw(new java.awt.Rectangle(x.x1,x.y1,x.x2-x.x1,x.y2-x.y1)))
graph.dispose()
ImageIO.write(image,"png",new File(destimage))

The rectangles are drawn but in GREY color instead of GREEN. What am I doing wrong?

Upvotes: 1

Views: 563

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

If the source image is a gray scale image, then it's unlikely that it will be capable of using any color of any sort.

Instead, you need to create a second, colored, BufferedImage and paint the original to it.

 BufferedImage original = ImageIO.read(sourceimage);
 BufferedImage iamge = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
 Graphics2D g2d = image.createGraphics();
 g2d.drawImage(original, 0, 0, null);
 // Continue with what you have

Sorry, I have no experience with PIL, but that's how you'd do it (basically) in pure Java

Upvotes: 3

Related Questions