Commit f17a7a4f8ac2db3ad8a80d0c98443d198adc488d
1 parent
d6611774
Exists in
master
Fix some problems with encoding on dicom patient name and dicom description
Showing
1 changed file
with
19 additions
and
17 deletions
Show diff stats
invesalius/reader/dicom.py
... | ... | @@ -1282,17 +1282,12 @@ class Parser(): |
1282 | 1282 | except(KeyError): |
1283 | 1283 | return "" |
1284 | 1284 | |
1285 | - if (data): | |
1286 | - name = data.strip() | |
1287 | - encoding = self.GetEncoding() | |
1288 | - | |
1289 | - try: | |
1290 | - # Returns a unicode decoded in the own dicom encoding | |
1291 | - return utils.decode(name, encoding, 'replace') | |
1292 | - except(UnicodeEncodeError): | |
1293 | - return name | |
1294 | - | |
1295 | - return "" | |
1285 | + encoding = self.GetEncoding() | |
1286 | + try: | |
1287 | + data = data.encode(encoding, errors="surrogateescape").decode(encoding) | |
1288 | + except Exception as err: | |
1289 | + print(err) | |
1290 | + return data | |
1296 | 1291 | |
1297 | 1292 | def GetPatientID(self): |
1298 | 1293 | """ |
... | ... | @@ -1559,15 +1554,22 @@ class Parser(): |
1559 | 1554 | """ |
1560 | 1555 | try: |
1561 | 1556 | data = self.data_image[str(0x0008)][str(0x103E)] |
1562 | - if data == "None": | |
1563 | - return _("unnamed") | |
1564 | - if (data): | |
1565 | - return data | |
1566 | - else: | |
1567 | - return _("unnamed") | |
1568 | 1557 | except(KeyError): |
1569 | 1558 | return _("unnamed") |
1570 | 1559 | |
1560 | + encoding = self.GetEncoding() | |
1561 | + try: | |
1562 | + data = data.encode(encoding, errors="surrogateescape").decode(encoding) | |
1563 | + except Exception as err: | |
1564 | + print(err) | |
1565 | + | |
1566 | + if data == "None": | |
1567 | + return _("unnamed") | |
1568 | + if data: | |
1569 | + return data | |
1570 | + else: | |
1571 | + return _("unnamed") | |
1572 | + | |
1571 | 1573 | def GetImageTime(self): |
1572 | 1574 | """ |
1573 | 1575 | Return the image time. | ... | ... |