SimpleITK icon indicating copy to clipboard operation
SimpleITK copied to clipboard

simpleitk can't load chinese path file,but itk can do it

Open junqiangchen opened this issue 6 years ago • 13 comments

hi, i try to load chinese path file with simpleitk in python,but it not working,i debug the code,find the path string is garbled. but i also try to load chinese path file with itk in c++,it is working,without any errors. so i hope simpleitk can fix the bug. thank you.

junqiangchen avatar Aug 01 '19 03:08 junqiangchen

Hello, thank you for reporting your issue. Please see "Report an Issue", for a list of important information that should be included when reporting a bug.

blowekamp avatar Aug 01 '19 13:08 blowekamp

Hello, thank you for reporting your issue. Please see "Report an Issue", for a list of important information that should be included when reporting a bug.

1、load chinese path file with simpleitk ReadImage function,the error is sitk::ERROR the file path does not exist. 2、Operating system win10 professional. 3、python 3.5.5 4、SimpleITK version is 3.0.12 5、pip install simpleitk 6、seg=sitk.ReadImage("肺结节Mask.nii.gz") 7、anyway .nii.gz file is ok,just change the name to 肺结节Mask string.

junqiangchen avatar Aug 05 '19 03:08 junqiangchen

Hello @junqiangchen, The SimpleITK version is strange (we are at 1.2.0), please let us know what you get when running import SimpleITK as sitk; print(sitk.Version())?

zivy avatar Aug 05 '19 12:08 zivy

I was able to come up will the following little self contained test:

import SimpleITK as sitk
img = sitk.Image([10,11,12], sitk.sitkFloat32)
print(sitk.Hash(img)) 
fname =   "肺结节Mask.nii.gz"
sitk.WriteImage(img, fname)
print(sitk.Hash(sitk.ReadImage(fname)))    

This worked on my MacOSX system with Acaconda Python 3.7. Does this script work for you?

blowekamp avatar Aug 05 '19 19:08 blowekamp

Hello @zivy, i try your method,my SimpleITK version is same with you,that is 1.2.0.

junqiangchen avatar Aug 13 '19 06:08 junqiangchen

Hello @blowekamp i run the on win10 with Acaconda Python 3.5,Python 3.6,Python 3.7,it work for me,but the output image name is garbled,like this 鑲虹粨鑺侻ask.nii.gz ,and i modify the file name to 肺结节Mask.nii.gz by hand,and using sitk.ReadImage(fname) to read the file,but the error of can't load chinese file happens.the error message is: RuntimeError: Exception thrown in SimpleITK ReadImage: C:\d\VS14-Win64-pkg\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx:99:sitk::ERROR: The file "肺结节Mask.nii.gz" does not exist.

junqiangchen avatar Aug 13 '19 07:08 junqiangchen

I was finally able to reproduce the issue on windows 10 Python 3.7.

First I had to change settings in windows so that it displayed Chinese characters correctly .

I then ran the following code:

import SimpleITK as sitk

file_name = '肺结节Mask.nii.gz'
sitk.WriteImage(sitk.Image([256,256], sitk.sitkUInt8), file_name)

with open(file_name, 'w') as fp:
    fp.write('my text')

The result are two files, one with the expected name which is a text file, the other is the image with the garbled file name. Not sure why the string is processed correctly by the open function and not by the WriteImage function.

@blowekamp, any idea?

zivy avatar Aug 15 '19 15:08 zivy

With Python 3.0 string default to unicode. ITK uses just byte ASCII encoding.

blowekamp avatar Aug 15 '19 15:08 blowekamp

Shouldn't the behavior be uniform across platforms? It works correctly on OSX and fails on Win-10.

zivy avatar Aug 15 '19 16:08 zivy

This bug is also a problem with filenames like μ.nii or ä.nii.

>>> import SimpleITK as sitk
>>> import pathlib
>>> pathlib.Path("C:\μ.nii").exists()
True
>>> sitk.ReadImage("C:\μ.nii")
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python37\lib\site-packages\SimpleITK\extra.py", line 346, in ReadImage
    return reader.Execute()
  File "C:\Python37\lib\site-packages\SimpleITK\SimpleITK.py", line 8015, in Execute
    return _SimpleITK.ImageFileReader_Execute(self)
RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: D:\a\1\sitk\Code\IO\src\sitkImageReaderBase.cxx:102:
sitk::ERROR: Unable to open "C:\μ.nii" for reading.

This itksys::SystemTools::FileExists check seems to wrongly claim the file does not exist.

Assuming this is the code, it has special Windows implementation, so maybe the bug is there? Or because Python detects some OS legacy encoding on Windows as "preferred" for file system operations it is converted wrong from Python to C++ std::string?

petsuter avatar Nov 17 '21 11:11 petsuter

你好, 我尝试在 python 中使用 simpleitk 加载中文路径文件,但它不起作用,我调试代码,发现路径字符串是乱码。 但我也尝试在 C++ 中用 itk 加载中文路径文件,它工作正常,没有任何错误。 所以我希望 simpleitk 可以修复这个错误。 谢谢你。

guanjj1 avatar Dec 29 '22 05:12 guanjj1

What status about this issue?

xiaofeixeniro avatar Dec 12 '23 08:12 xiaofeixeniro

你好, 我尝试在 python 中使用 simpleitk 加载中文路径文件,但它不起作用,我调试代码,发现路径字符串是乱码。 但我也尝试在 C++ 中用 itk 加载中文路径文件,它工作正常,没有任何错误。 所以我希望 simpleitk 可以修复这个错误。 谢谢你。

为什么我的itk无法打开中文路径,用的msvc编译器编译的吗,什么版本的itk,什么版本的msvc编译器,能否帮忙透露下

xixi911 avatar Mar 13 '24 10:03 xixi911