2

I am using the DevIL library to read and write images. The problems is that I want to overwrite the file if it already exists.

Here is my code:

(RGB v) <- runIL $ readImage "/foo/foo.png"
let rotated = (computeS $ batman v) :: Array F DIM3 Word8
runIL $ writeImage ("/foo/foo.png") (RGB rotated)

How can I achieve that? Can I do this or do I have to think of another way? It is something of the OS?

I am using that file as a temporary image until the user decides to save it (after some changes, like rotating it, expanding it, etc).

SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25
Hamburguesa66
  • 87
  • 1
  • 7
  • 1
    Have you tried saving an image to a file that already exists? What happened in that case? – SwiftsNamesake Jul 31 '17 at 22:06
  • If that doesn't work, you can check whether the file exists with [`doesFileExist`](http://hackage.haskell.org/package/directory-1.3.1.1/docs/System-Directory.html#v:doesFileExist) and delete it before you save the new file. – SwiftsNamesake Jul 31 '17 at 22:09
  • When i save an image to a file that already exist, the program exits with a message ("Unable to save file"). I check the DevIL source code and i didnt get any answer. I will try what you said. Thanks! – Hamburguesa66 Jul 31 '17 at 23:17
  • I'll write up an answer then. – SwiftsNamesake Jul 31 '17 at 23:32

1 Answers1

0

If the library won't let you replace an existing file directly, you can check whether the file exists with doesFileExist and delete it before you save the new file.

replace = do    
  let fn = "path/to/image/file/..."
  exists <- doesFileExist fn
  when exists $ removeFile fn
  runIL $ writeImage fn (RGB rotated)
SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25