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);
}
}