dahtah / imager

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

Could not deal with non-English paths #126

Open hope-data-science opened 4 years ago

hope-data-science commented 4 years ago

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

ShotaOchi commented 4 years ago

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.

hope-data-science commented 4 years ago

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

ShotaOchi commented 4 years ago

Set LC_CTYPE, not LC_TIME.

Sys.setlocale("LC_CTYPE", "Chinese")
hope-data-science commented 4 years ago

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
ShotaOchi commented 4 years ago

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

Sys.setlocale("LC_CTYPE", "cht")
hope-data-science commented 4 years ago

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...

ShotaOchi commented 4 years ago

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)
hope-data-science commented 4 years ago

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.