Commit 27398d4a686a38d2c3bc6b84922397a07cf45017
1 parent
54759b5d
Exists in
master
and in
63 other branches
Using replace method error to decode dicom fields and avoid crashes.
Some dicom files have the encoding setted wrongly, so when decoding a dicom field from string to unicode some errors may happen. Python string.decode have some methods to handle those cases. We are using the "replace" one, the characters it cannot decode to unicode it replaces with a character showing the character was not decoded.
Showing
2 changed files
with
4 additions
and
4 deletions
Show diff stats
invesalius/reader/dicom.py
... | ... | @@ -1262,7 +1262,7 @@ class Parser(): |
1262 | 1262 | |
1263 | 1263 | try: |
1264 | 1264 | # Returns a unicode decoded in the own dicom encoding |
1265 | - return name.decode(encoding) | |
1265 | + return name.decode(encoding, 'replace') | |
1266 | 1266 | except(UnicodeEncodeError): |
1267 | 1267 | return name |
1268 | 1268 | |
... | ... | @@ -1284,7 +1284,7 @@ class Parser(): |
1284 | 1284 | if (data): |
1285 | 1285 | encoding = self.GetEncoding() |
1286 | 1286 | # Returns a unicode decoded in the own dicom encoding |
1287 | - return data.decode(encoding) | |
1287 | + return data.decode(encoding, 'replace') | |
1288 | 1288 | return "" |
1289 | 1289 | |
1290 | 1290 | |
... | ... | @@ -1489,7 +1489,7 @@ class Parser(): |
1489 | 1489 | if isinstance(data, unicode): |
1490 | 1490 | return data |
1491 | 1491 | encoding = self.GetEncoding() |
1492 | - return data.decode(encoding) | |
1492 | + return data.decode(encoding, 'replace') | |
1493 | 1493 | except(KeyError): |
1494 | 1494 | return "" |
1495 | 1495 | ... | ... |
invesalius/reader/dicom_reader.py
... | ... | @@ -174,7 +174,7 @@ class LoadDicom: |
174 | 174 | data_dict[group] = {} |
175 | 175 | |
176 | 176 | if not(utils.VerifyInvalidPListCharacter(data[1])): |
177 | - data_dict[group][field] = data[1].decode(encoding) | |
177 | + data_dict[group][field] = data[1].decode(encoding, 'replace') | |
178 | 178 | else: |
179 | 179 | data_dict[group][field] = "Invalid Character" |
180 | 180 | ... | ... |