Skip to content

Some special characters in metadata are truncated #2082

@nicolascedilnik

Description

@nicolascedilnik

Describe the bug
Metadata is incorrectly read from the MHA file: some characters are removed.

To Reproduce
Steps to reproduce the behavior:

  1. Operating system, version, and architecture
  • OS: Fedora
  • Architecture: x86
  1. Programming language: Python 3.12
  2. Version of SimpleITK: 2.3.1
  3. How was SimpleITK installed? pip
  4. A minimal working example which causes the error.
rom pathlib import Path

import SimpleITK as sitk
import numpy as np

path = Path("/tmp/test.mha")

img = sitk.GetImageFromArray(np.zeros((2,2,2)))
img.SetMetaData("test1", "des caractères spéciaux")
img.SetMetaData("test2", "des caractères spéciö")
sitk.WriteImage(img, str(path))

img2 = sitk.ReadImage(str(path))
print(img2.GetMetaData("test1"))  # output: "des caractères spéciaux"
print(img2.GetMetaData("test2"))  # output: "des caractères spéci"

for line in path.open("r"):
    if line.startswith("test"):
        print(line)  

# output:
# test1 = des caractères spéciaux
# test2 = des caractères spéciö
# which shows that the content of the MHA is correctly written, but not correctly read

Expected behavior
img2.GetMetaData("test2") should return "des caractères spéciö" instead of "des caractères spéci"

I suspect some unicode black magic is at work here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions