0

I have a program that takes an image and creates a gray scale version of that image. I am doing this for a class, and I need to be able to run the program like - > java Grey lena_color.gif However, the only way it works is if I copy the entire file path which for me looks something like java Grey C:Users\David\...\lena_color.gif with a bunch of other folders in between. How can I make my code work with the input only being the name of the file? Also, where should I make the destination of the new file for anyone (my professor) who might run my program, since currently it is specific to my computer? I am not sure how I'd even go about setting a common file destination. Attached below is my source code

import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Grey {

    public static void main(String[] args)  {
        File ogImg = new File(args[0]);
        String fileName = ogImg.getName();
        String newName = stripExt(fileName);
        BufferedImage img = null;
        try {
            img = ImageIO.read(ogImg);
            BufferedImage greyscale = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
            
            for (int i = 0; i < img.getWidth(); i++)    {
                for (int j = 0; j < img.getHeight(); j++)   {
                    Color c = new Color(img.getRGB(i, j));
                    int r = c.getRed();
                    int g = c.getGreen();
                    int b = c.getBlue();
                    
                    int grey = (int)((0.3 * r) + (0.59 * g) +(0.11 * b)); 
                    
                    Color GREY = new Color(grey, grey, grey);
                    greyscale.setRGB(i, j, GREY.getRGB());
                }
            }
            
            ImageIO.write(greyscale, "png", new File("C:\\Users\\David\\Downloads\\" + newName + "_grey.png"));
        } catch (IOException e) {
        }
    }
    
    public static String stripExt(String s) {
        int dot = s.lastIndexOf('.');
        return s.substring(0,dot);
    }
}
  • 1
    Does this help? https://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-does-it-mean – ykaganovich Sep 16 '20 at 22:33
  • Well, if you just provide the file name your application would need to know where that file is. You'd have a few options: a) by default the file name would be relative to the current working directory so run the application from the folder the image is located in, b) search a few standard folders like ["home"](https://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java), "temp" etc. (look up how to get that from within Java), c) search the entire filesystem and hope you get the correct file (there might be many with the same name) – Thomas Sep 16 '20 at 22:35
  • You have to put your files in your project directory maybe in a folder named gif. Then you can simply give the path ".. /gif/yourfile" similar to this. – bilginyuksel Sep 16 '20 at 22:36

1 Answers1

0

When command-line utilities (in any language) take filename arguments, it's pretty conventional that they expect to get a full pathname, not a short name. There are exceptions, of course -- but in those cases there are usually well-established, conventional places to look for files (a user home directory, for example).

You could certainly arrange for your program to either (a) read and write its files from some well-defined location, or (b) search the filesystem for the input file. If it searches the filesystem, it could perhaps write the output file in the same place as it found the input file.

But not of this is really necessary -- I'm pretty sure that anybody who runs a command-line utility that operates on filenames will be expecting to provide full pathnames.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15