From f17a7a4f8ac2db3ad8a80d0c98443d198adc488d Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Tue, 22 Jun 2021 15:56:22 -0300 Subject: [PATCH] Fix some problems with encoding on dicom patient name and dicom description --- invesalius/reader/dicom.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/invesalius/reader/dicom.py b/invesalius/reader/dicom.py index f7ed570..52b1f16 100644 --- a/invesalius/reader/dicom.py +++ b/invesalius/reader/dicom.py @@ -1282,17 +1282,12 @@ class Parser(): except(KeyError): return "" - if (data): - name = data.strip() - encoding = self.GetEncoding() - - try: - # Returns a unicode decoded in the own dicom encoding - return utils.decode(name, encoding, 'replace') - except(UnicodeEncodeError): - return name - - return "" + encoding = self.GetEncoding() + try: + data = data.encode(encoding, errors="surrogateescape").decode(encoding) + except Exception as err: + print(err) + return data def GetPatientID(self): """ @@ -1559,15 +1554,22 @@ class Parser(): """ try: data = self.data_image[str(0x0008)][str(0x103E)] - if data == "None": - return _("unnamed") - if (data): - return data - else: - return _("unnamed") except(KeyError): return _("unnamed") + encoding = self.GetEncoding() + try: + data = data.encode(encoding, errors="surrogateescape").decode(encoding) + except Exception as err: + print(err) + + if data == "None": + return _("unnamed") + if data: + return data + else: + return _("unnamed") + def GetImageTime(self): """ Return the image time. -- libgit2 0.21.2