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.
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)
You can easily do it with pip.
pip install SimpleITK
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.
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. .. ..
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