0

I have an assignment that requires me to write a program that reads in an image given at the command line (image is saved in the project folder prior to running the program), then tile and change the colours around on the image and save this new file with a -tiled suffix.

The only thing I could find on Stack Overflow was to use the renameTo method but it is undefined for the type BufferedImage which the variable 'inputfile' is, therefore this current code will not compile.

File outputfile = inputfile.renameTo(inputfile + "-tiled");
try {
    ImageIO.write(inputfile1, "png", outputfile);
} catch (IOException e) {
    e.printStackTrace();
}

What is a better way to do this?

sksamuel
  • 16,154
  • 8
  • 60
  • 108
user3405010
  • 21
  • 1
  • 4

1 Answers1

0

Don't rename the file; you need to create a new File as in using the File constructor with the filename as String.

// --- READ ---
File inputFile = new File(args[0]);
BufferedImage image = ImageIO.read(inputFile);

// --- PERFORM IMAGE MANUPULATION --- 
...

// --- DERIVE NAME --- 
String inputPath = inputFile.getPath();
// do string manipulation to turn into outputPath (using "-tiled")
File outputFile = new File(outputPath);

// --- WRITE ---
ImageIO.write(image, "png", outputFile);

From the JavaDocs of the write method:

Writes an image using an arbitrary ImageWriter that supports the given format to a File. If there is already a File present, its contents are discarded.

So it will automatically create a new file. Note that File instances do not represent file handles directly. They are just virtual representations of a file; the real file is only created when it is written to.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • It now says that the getPath method is also undefined for type BufferedImage. What's happening? – user3405010 Apr 13 '14 at 10:28
  • Your `inputfile` is a `BufferedImage` instead of a file... Make sure that `inputfile` is a `File` instead and make sure you name your variables well. – Maarten Bodewes Apr 13 '14 at 10:30
  • But inputfile needs to be a BufferedImage for the rest of the program in order to modify size and colours and all that using the BufferedImage class – user3405010 Apr 13 '14 at 10:40
  • Call it `inputImage` instead and keep the `inputfile` (or the path string) around for storing the data later on. You can use two variables instead of one. *Never ever* create type confusion in your identifiers. An image in memory *is not a file*. – Maarten Bodewes Apr 13 '14 at 10:56
  • thanks for all the help. Still confused as to what I initialise the new inputImage variable as? if it's null it does nothing.... – user3405010 Apr 13 '14 at 11:05
  • The `inputImage` variable should hold the object instance that is currently in `inputfile`. `inputfile` on the other hand should keep the `File` that was created when you read the command line. If you used a `String` directly to read the file, you need to use `new File(inputPath)`. – Maarten Bodewes Apr 13 '14 at 11:10
  • Sorry but I'm a massive noob and still very lost. Now what I have is: – user3405010 Apr 13 '14 at 11:19
  • `File inputImage = null; String inputPath = inputImage.getPath(); String outputPath = inputPath + "-tiled"; File outputFile = new File(outputPath); try { ImageIO.write(inputFile1, "png", outputFile); } catch (IOException e) { e.printStackTrace(); }` – user3405010 Apr 13 '14 at 11:20
  • Never mind that. Take another look at my answer and see if you understand the pseudo code in there... You must have retrieved the input image from *somewhere*? `BufferedImage` in itself is not linked to any file. – Maarten Bodewes Apr 13 '14 at 11:22
  • I still dont quite understand. I declared `inputFile` as a `BufferedImage` and initialised it as `null` then used `inputFile = ImageIO.read(new File(args[0]));` within the try-catch statement to read in the file from command line. So how do I use the second variable `inputImage`??? – user3405010 Apr 13 '14 at 11:39
  • The return value of ImageIO.read is an image, not a file. So you split your statement in two: `inputFile = new File(args[0]);` and `image = ImageIO.read(inputFile);` then do the image manipulation on `image` and use the `inputFile` variable as input to create the `outputFile`. – Maarten Bodewes Apr 13 '14 at 12:00
  • ok sweet that's all working fine but now it creates a new file of type "png-tiled" eg original.png-tiled how do i resolve this? At the moment I'm trying to add the "-tiled" part in the line `String outputPath = inputPath + "-tiled";` – user3405010 Apr 13 '14 at 12:17
  • Create a new `String` using [`lastIndexOf`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(java.lang.String)) and `substring()` and concatenation (`+`). Have fun figuring it out :) – Maarten Bodewes Apr 13 '14 at 12:30
  • If this helped you, don't forget to accept the answer. Did you solve the filename puzzle? – Maarten Bodewes Apr 15 '14 at 19:51