Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Could not deal with non-English paths #126

Open
hope-data-science opened this issue Mar 26, 2020 · 8 comments
Open

Could not deal with non-English paths #126

hope-data-science opened this issue Mar 26, 2020 · 8 comments

Comments

@hope-data-science
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

Set LC_CTYPE, not LC_TIME.

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

@hope-data-science
Copy link
Author

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
Copy link
Contributor

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

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

@hope-data-science
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants