imager icon indicating copy to clipboard operation
imager copied to clipboard

Could not deal with non-English paths

Open hope-data-science opened this issue 5 years ago • 8 comments

When I use load.image, if the directory has non-English symbols, it return an error. Is there a way to make it right?

hope-data-science avatar Mar 26 '20 00:03 hope-data-science

You have to set locale properly if the error is similar with

Error in readfun(f, ...) : unable to open とり.jpg

You can set locale with Sys.setlocale function. For example, run the following code if you want to read a image whose filename has Japanese symbols. Sys.setlocale(category = "LC_ALL", locale="Japanese")

Note that you may not be able to load image that has non-English symbols if you are on Windows. imager uses read.bitmap function of readbitmap package to load image. However, read.bitmap function does nothing on my Windows machine if filename of image has non-English symbols.

ShotaOchi avatar Mar 26 '20 03:03 ShotaOchi

Thank you for the response, here's what I get:

> Sys.setlocale("LC_TIME", "Chinese")
[1] "Chinese (Simplified)_China.936"
> imager::load.image(pic_dir[1])
Error in readfun(f, ...) : unable to open for_selection/1.10濡傜殝寰愯憶灞?156080920854794240_A.JPG

Still not working...

Thanks

hope-data-science avatar Mar 26 '20 04:03 hope-data-science

Set LC_CTYPE, not LC_TIME.

Sys.setlocale("LC_CTYPE", "Chinese")

ShotaOchi avatar Mar 26 '20 05:03 ShotaOchi

Thank you. Still, this might not be the case:

> Sys.setlocale("LC_CTYPE", "Chinese")
[1] "Chinese (Simplified)_China.936"
> imager::load.image(pic_dir[1])
Error in readfun(f, ...) : unable to open for_selection/1.10濡傜殝寰愯憶灞?156080920854794240_A.JPG

hope-data-science avatar Mar 26 '20 05:03 hope-data-science

If the filename has traditional Chinese symbols, use "cht" instead of "Chinese".

Sys.setlocale("LC_CTYPE", "cht")

ShotaOchi avatar Mar 26 '20 05:03 ShotaOchi

Thanks, still:

> Sys.setlocale("LC_CTYPE", "cht")
[1] "Chinese (Traditional)_Taiwan.950"
> imager::load.image(pic_dir[1])
Error in readfun(f, ...) : unable to open for_selection/1.10憒?敺?撅?156080920854794240_A.JPG

The file name do not have Chinese, but the path has. I've tried several packages, only magick could read it correctly. However, imager has some functionalities that no alternative compares, I might consider how to rename...

hope-data-science avatar Mar 26 '20 05:03 hope-data-science

You don't have to rename the path if magick package can read it correctly because magick2cimg function of imager package converts magick-image object into cimg object. You can load the image with magick package and convert it into cimg object.

# load image with magick package
  a <- magick::image_read("some_image.jpg")
# convert magick-image object into cimg object  
  b <- imager::magick2cimg(a) 

Moreover, you can convert cimg object into magick-image object with cimg2magick function of imager package or image_read function of magick package.

# convert cimg object into magick-image object
  a2 <- imager::cimg2magick(b) 
  a3 <- magick::image_read(b)

ShotaOchi avatar Mar 26 '20 06:03 ShotaOchi

This works well! I did not know that the two package could be linked through this way.

> p_load(imager,magick)
> image_read(pic_dir[1]) %>% R()
Error in `[.magick-image`(im, , , , ind, drop = FALSE) : 
  unused arguments (alist(, , ind, drop = FALSE))
> image_read(pic_dir[1]) %>% 
+   magick2cimg() %>% R()
Image. Width: 800 pix Height: 800 pix Depth: 1 Colour channels: 1 

Thank you so much.

hope-data-science avatar Mar 26 '20 07:03 hope-data-science