dahtah / imager

R package for image processing
GNU Lesser General Public License v3.0
187 stars 43 forks source link

Can't save to file when file/pathname contains dots #38

Closed hongooi73 closed 7 years ago

hongooi73 commented 7 years ago

I just ran into this today. If I pass a file or pathname that contains a dot to save.image, and ImageMagick isn't available, the save fails:

> save.image(boats, file="my.file.jpg")
Error in save.image(boats, file = "my.file.jpg") : 
  Unsupported output file format. Use jpg/png or install ImageMagick
> save.image(boats, file="temp.folder/myfile.jpg")
Error in save.image(boats, file = "temp.folder/myfile.jpg") : 
  Unsupported output file format. Use jpg/png or install ImageMagick

The underlying cause is the regex that save.image uses to detect the file type:

    ftype <- stringr::str_match(file, "\\.(.+)$")[1, 2]
    if (ftype == "png") ...

If file contains more than one dot, it will match everything past the first dot:

> stringr::str_match("my.file.jpg", "\\.(.+)$")
     [,1]        [,2]      
[1,] ".file.jpg" "file.jpg"

Replacing the regex with \\.([^.]+)$ should fix this:

> stringr::str_match("my.file.jpg", "\\.([^.]+)$")
     [,1]   [,2] 
[1,] ".jpg" "jpg"
dahtah commented 7 years ago

Thanks a lot for the bug report! Fixed in current branch ("pixset").