[PYTHON] When reading an image with SimpleITK, there is a problem if there is Japanese in the path

Introduction

I think that openCV and PIL are famous as python image processing libraries, but I started using SimpleITK because I want to load dicom images directly. First of all, I wrote a simple code to read and display an image using SimpleITK, but suddenly I stumbled upon this. .. .. From the conclusion, if Japanese (probably not English?) Is included in the path of the input image, it seems to be useless due to the problem of the character code. SimpleITK itself is a minor library, so few people may use it, but I will share it for the time being.

PC environment

About Simple ITK

SimpleITK (http://www.simpleitk.org/) seems to be a C ++-based image processing library called ITK (https://itk.org/itkindex.html) that can be used in python and Java. It is rarely used to process medical images such as CT and MRI. Medical images are often stored in a format called dicom, so SimpleITK can read dicom images directly. The documentation (https://simpleitk.readthedocs.io/en/master/index.html) is fairly solid, so it's easy for beginners to get started? (Well, I suddenly stumbled)

Install SimpleITK

You can easily do it with pip.

pip install SimpleITK

Loading images

Load the images exactly as described in the official documentation "Reading and Writing for Images and Transforms" (https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/IO.html). However, make sure that the path of the input image includes Japanese. This time, it is the code to read Lena (png format) instead of the dicom image.

read_image.py


import SimpleITK as sitk

file_name="C:/Japanese/Lenna_(test_image).png "#It doesn't work with this
#file_name="C:/Lenna_(test_image).png "#Works normally

reader = sitk.ImageFileReader()
reader.SetImageIO("PNGImageIO")
reader.SetFileName(file_name)
image = reader.Execute();

if ( not "SITK_NOSHOW" in os.environ ):
    sitk.Show( image, "image show" )#Display an image with ImageJ.

result

I get an error message saying that the file cannot be found, as shown below.

RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: D:\a\1\sitk-build\ITK\Modules\IO\PNG\src\itkPNGImageIO.cxx:149:
itk::ERROR: PNGImageIO(000001A1C5886F00): PNGImageIO could not open file: C:/Japanese/Lenna_(test_image).png for reading.
Reason: Illegal byte sequence

** Countermeasure **

If you change the path of the image so that it does not include Japanese, it works normally.

Looking at the issues on Github ...

Don't include Chinese in your path! There seems to be some people who are worried. (https://github.com/SimpleITK/SimpleITK/issues/795 ) The character code of python string is unicode, but the ITK side is arguing that the character code is ASCII. ASCII does not support Japanese, so when it comes to the fact that the ITK side can only read ASCII, it may be necessary to make all paths alphabetical. .. ..

Summary

When using SimpleITK, do not include Japanese in the path of the image. I will update it when I find the underlying solution.

Recommended Posts

When reading an image with SimpleITK, there is a problem if there is Japanese in the path
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Determining if there are birds in the image
Create an image with characters in python (Japanese)
If branch depending on whether there is a specific element in the list
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Check if the string is a number in python
When writing an if statement with a regular expression
Determine if an attribute is defined in the object
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
When a local variable with the same name as a global variable is defined in the function
Addresses an issue where an error does not occur even if the path does not pass when trying to read an image with cv2.imread ()
Is there a bias in the numbers that appear in the Fibonacci numbers?
Solve the Japanese problem when using the CSV module in Python.
The image is a slug
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
What to do if an error message "There was a problem displaying this web page" is displayed in Chrome's Web Driver (Selenium)
Let's run Fusion 360 with Python Part 11 Since there is no point along the path in the API, I thought of an alternative
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
Python will fail if there is a space after the backslash
Check if the configuration file is read in an easy-to-understand manner
Delete a particular character in Python if it is the last
Read a file in Python with a relative path from the program
Error with pip: There was a problem confirming the ssl certificate
The solution when an empty object is returned in go's json.Marshal
Solve the subset sum problem with a full search in Python
[Golang] Check if a specific character string is included in the character string
A story when a directory is buggy with the django-admin start project command in a virtual environment using Pipenv
Check if there is a specific symbol in the executable file and its dependent libraries (simplified version)
Be careful of the type when making an image mask with Numpy
Behavior when returning in the with block
Cut out A4 print in the image
When the target is Ubuntu 16.04 in Ansible
Is there NaN in the pandas DataFrame?
In the Chainer tutorial, I get an error when importing a package. (mock)
If you guys in the scope kitchen can do it with a margin ~ ♪
How to display in the entire window when setting the background image with tkinter
If you get a long error when tabbing an interactive shell with Anaconda
Draw a graph with Japanese labels in Jupyter
Speaking Japanese with OpenJtalk (reading a text file)
Speaking Japanese with gTTS (reading a text file)
What is wheezy in the Docker Python image?
Determine the numbers in the image taken with the webcam
Detect folders with the same image in ImageHash
Isn't there a default value in the dictionary?
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
Implement a circular expression binary search in Python. There is a comparison with a simple full search.
A memo when checking whether the specified key exists in the defined dictionary with python
I got stuck when trying to specify a relative path with relative_to () in python
Convert a path string that uses a symbolic link in the middle to an absolute path
When reading a csv file with read_csv of pandas, the first column becomes index
How to check in Python if one of the elements of a list is in another list
There is a pattern that the program did not stop when using Python threading
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
I asked the problem of the tribonacci sequence in C ++ & the number of function calls when writing with the recurrence function (python is also available)
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
When accessing a URL containing Japanese (Japanese URL) with python3, it will be encoded in html without permission and an error will occur, so make a note of the workaround.