Question

I've made the following example, I can't figure out why it isn't working.

  • None of my checks are returning false,
  • I have read and write permissions,
  • the imageIO responds that it has in fact overwritten the image,
  • no exceptions are thrown...

but the image still hasn't changed on disk.

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class example
{
    public static void main(String[] args)
        {
            try
                {
                    // load a jpg file
                    File imageFile = getFile();

                    // Make a Buffered Image from it
                    BufferedImage img = ImageIO.read(imageFile);

                    // for every pixel in the image
                    for (int x = 0; x < img.getWidth(); x++)
                        for (int y = 0; y < img.getHeight(); y++)
                            // check if the RGB integer is an odd number
                            if (img.getRGB(x, y) % 2 != 0)
                                // make it an even number if it is odd (the OCD god demands it!)
                                img.setRGB(x, y, img.getRGB(x, y) - 1);

                    // Write the OCD friendly version to the file
                    System.out.println("Was overwritten: " + ImageIO.write(img, "jpg", imageFile));
                    System.out.println();

                    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                    // Select the same file again
                    imageFile = getFile();

                    // Make the bufferedImage from it
                    img = ImageIO.read(imageFile);

                    // for every pixel in the image
                    for (int x = 0; x < img.getWidth(); x++)
                        for (int y = 0; y < img.getHeight(); y++)
                            // check if the RGB integer is an odd number
                            if (img.getRGB(x, y) % 2 != 0)
                                {
                                    // Report the failing
                                    System.out.println("It didn't work :(");
                                    // Stop the loop
                                    x = img.getWidth();
                                    y = img.getHeight();
                                }
                }
            catch (Exception e)
                {
                    e.printStackTrace();
                }
        }

    public static File getFile()
        {
            // set up a file chooser that only accepts jpgs
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("JPG Images only", "jpg"));

            // This will represent our loaded file
            File imageFile = null;

            // Select a file
            chooser.showOpenDialog(null);
            imageFile = chooser.getSelectedFile();

            // Check we can do stuff to this file
            System.out.println("Exists: " + imageFile.exists());
            System.out.println("Can Read: " + imageFile.canRead());
            System.out.println("Can Write: " + imageFile.canWrite());
            System.out.println();

            return imageFile;
        }
}
Was it helpful?

Solution

It is overwritten, you just don't detect that visually with your
eyes. That is because you change the RGB values by just 1.

for (int x = 0; x < img.getWidth(); x++)
    for (int y = 0; y < img.getHeight(); y++)
        // check if the RGB integer is an odd number
        if (img.getRGB(x, y) % 2 != 0)
            // make it an even number if it is odd (the OCD god demands it!)
            img.setRGB(x, y, 0); /// A ///

Change this line /// A /// as I did and you'll see it is overwritten.

Of course it also depends on the picture you're testing this on.
If img.getRGB(x, y) is never odd, the img.setRGB will never be executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top