I am experiencing problems when running some code that I am developing.
It should work as such:
For all images within directory (x)
Read image
Convert to greyscale
Save to new directory (y)
For all images within new directory (y)
Read image
Convert to binary
Save to new directory (z)
End for
End for
I have 300 images at present and so far all images are successfully converted to greyscale and saved to a new directory. However, the binary conversion is where the problems occur as it appears to not detect any images in the new directory and only appears to work if [image] files already exist within the directory before the code is executed.
Therefore, the following is what actually happens:
All files in directory (x) are read
All files in directory (x) are converted to greyscale and saved to new directory (y)
All files in directory (y) are read
It appears that directory (y) is empty (but, in fact, contains 300 greyscale images)
Program ends
However, when I run the program a second time, either with the 300 greyscale images or even a single images, the images in directory (y) are successfully converted into binary; it appears to only work if there are pre-existing images in the directory and not whilst the newly-converted-to-greyscale images are being created on-the-fly.
The method is called as follows:
public static void processFiles(){
processGreyscale();
System.out.println("Greyscale image conversion complete.\n");
processBinary();
System.out.println("Binary image conversion complete.\n");
}
I have even tried adding a time-delay between method calls to allow for the system to update itself in order to detect the newly-created [greyscale] images (in directory (y)), but this does not make any difference and the images are only recognised and converted to binary when two conditions are satisfied:
- There are images present in
directory (y) - The code is run for a second time or if there are any [image] files within the directory before the code is first executed.
Is there a way to do this so that the newly-generated greyscale images are detectable as soon as they have been created and then converted to binary?
Many thanks.
UPDATE: My code for converting to greyscale is as follows:
try{
//Read in original image.
BufferedImage inputImg = ImageIO.read(image);
//Obtain width and height of image.
double image_width = inputImg.getWidth();
double image_height = inputImg.getHeight();
//New images to draw to.
BufferedImage bimg = null;
BufferedImage img = inputImg;
//Draw the new image.
bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D gg = bimg.createGraphics();
gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
//Save new binary (output) image.
String fileName = "greyscale_" + image.getName();
File file = new File("test_images\\Greyscale\\" + fileName);
ImageIO.write(bimg, "jpg", file);
}
catch (Exception e){
System.out.println(e);
}
How would I modify this to add the flush() and/or close() functions?
UPDATE: I have also created a line which prints after each successful conversion and the only feedback I have from the binary method is: java.lang.NullPointerException (BINARY) test_images\Greyscale\desktop.ini: processed successfully. Binary image conversion complete. whereas it should say: (BINARY) images\298.jpg: processed successfully..
Is there any reason for this? I don’t understand why the desktop.ini file is read/processed?
I have discovered what the problem was and have now resolved the issue.
Thanks to those of you who provided useful suggestions.