1

Is it possible to feed an array of image names into code which converts images to greyscale?

I am able to convert an image into greyscale by using this code:

public static void makeGrey() { 
try{
//Read in original image. 
BufferedImage image = ImageIO.read(new File("images\\012.jpg"));

//Obtain width and height of image.
double image_width = image.getWidth();
double image_height = image.getHeight();

BufferedImage bimg = null;
BufferedImage img = image;

//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 greyscale (output) image.
String temp = "_inverted";
File fi = new File("images\\" + temp + ".jpg");
ImageIO.write(bimg, "jpg", fi);
}
catch (Exception e){
              System.out.println(e);
}
}

However, this code only works on a single file at a time and I would like to know how to go about getting it to work through all files located in the images directory?

I have created an array which goes through the images directory and stores the names of all of the files and I would like to know how to pass these filenames into my makeGrey() method?

    static File dir = new File("images");
    static File imgList[] = dir.listFiles();

    public static void listFiles(String imageName) {

        if(dir.isDirectory()){
            for(File img : imgList){
                if(img.isFile()){
                MakeGrey.makeGrey();
            }
        }
    }

Thank you.

Mus
  • 7,290
  • 24
  • 86
  • 130

1 Answers1

2

Your makeGray() method should look like this:

public static void makeGrey(File image) { 
try{
    //Read in original image. 
    BufferedImage inputImage = ImageIO.read(image);
    ...
    ...
    //Save new greyscale (output) image. (Or you'll rewrite same image all the time...)
    File fi = new File("images\\inverted_" + image.getName() 
    ...
    ...

and other part of the code should call it like this:

static File dir = new File("images");
static File imgList[] = dir.listFiles();

public static void listFiles(String imageName) {

    if(dir.isDirectory()){
        for(File img : imgList){
            if(img.isFile()){
            MakeGrey.makeGrey(img);
        }
    }
}
PrimosK
  • 13,848
  • 10
  • 60
  • 78
  • Thank you - I have made these changes, but am alerted to the fact that the `image` variable is a duplicate; I then changed `BufferedImage image = ImageIO.read(image);` to `BufferedImage inputImage = ImageIO.read(image);` and now, when I compile and run the code, nothing happens...any suggestions? – Mus Feb 02 '12 at 11:29
  • Thank you, but I figured it out - the problem was with the method declaration: `public static void listFiles(String imageName)` does not require an input, so it should read `public static void listFiles()`. Thank you once again. – Mus Feb 02 '12 at 11:49
  • 1
    You can also list only image files by extension using a DirectoryScanner: http://stackoverflow.com/questions/794381/how-to-find-files-that-match-a-wildcard-string-in-java – Kris Feb 02 '12 at 12:03