0

I created new BufferedImage gimage (gray image), and one Image named Gimage. There I used method GrayFilter, then I have drawn Image on my BufferedImage, but I just got empty output image. What's wrong with the code below?

BufferedImage gimage = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
        ImageFilter filter = new GrayFilter(true,50);
        ImageProducer producer = new FilteredImageSource(img.getSource(),filter);
        Image Gimage = Toolkit.getDefaultToolkit().createImage(producer);
        gimage.getGraphics().drawImage(Gimage, 0, 0 , null);
        ImageIO.write(gimage, "png", new File("out.png"));
Kyoko
  • 1
  • 1
  • I've tried your code and don't seem to have any issues. You might want to consider using a `MediaTracker` to ensure that the image is fully realised before painting it and disposing of the `Graphics` context before you save it – MadProgrammer Apr 12 '18 at 23:59
  • 1
    I have to say, personally, I steer clear of `Toolkit`s imaging API, it can be a pain to work with some times. Instead, I'd consider using `ColorConvertOp`, [for example](https://stackoverflow.com/questions/21899824/java-convert-a-greyscale-and-sepia-version-of-an-image-with-bufferedimage/21900125#21900125) and [example](https://stackoverflow.com/questions/21176754/how-can-i-convert-an-image-to-grayscale-without-losing-transparency/21176863#21176863) – MadProgrammer Apr 13 '18 at 00:03
  • 1
    or [this for example](https://stackoverflow.com/questions/18710560/how-to-convert-colors-to-grayscale-in-java-with-just-the-java-io-library/18710815#18710815) – MadProgrammer Apr 13 '18 at 00:03
  • Thanks, `ColorConvertOp` worked well and it looks a lot easier, maybe I have had compiler issues with my code.. – Kyoko Apr 13 '18 at 05:56

0 Answers0