Commit 5ffbac4d291fe3af12105902cbffac47c5bcd894
1 parent
5f5a5d7d
Exists in
beta4
and in
1 other branch
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,7 +1262,7 @@ class Parser(): | ||
1262 | 1262 | ||
1263 | try: | 1263 | try: |
1264 | # Returns a unicode decoded in the own dicom encoding | 1264 | # Returns a unicode decoded in the own dicom encoding |
1265 | - return name.decode(encoding) | 1265 | + return name.decode(encoding, 'replace') |
1266 | except(UnicodeEncodeError): | 1266 | except(UnicodeEncodeError): |
1267 | return name | 1267 | return name |
1268 | 1268 | ||
@@ -1284,7 +1284,7 @@ class Parser(): | @@ -1284,7 +1284,7 @@ class Parser(): | ||
1284 | if (data): | 1284 | if (data): |
1285 | encoding = self.GetEncoding() | 1285 | encoding = self.GetEncoding() |
1286 | # Returns a unicode decoded in the own dicom encoding | 1286 | # Returns a unicode decoded in the own dicom encoding |
1287 | - return data.decode(encoding) | 1287 | + return data.decode(encoding, 'replace') |
1288 | return "" | 1288 | return "" |
1289 | 1289 | ||
1290 | 1290 | ||
@@ -1489,7 +1489,7 @@ class Parser(): | @@ -1489,7 +1489,7 @@ class Parser(): | ||
1489 | if isinstance(data, unicode): | 1489 | if isinstance(data, unicode): |
1490 | return data | 1490 | return data |
1491 | encoding = self.GetEncoding() | 1491 | encoding = self.GetEncoding() |
1492 | - return data.decode(encoding) | 1492 | + return data.decode(encoding, 'replace') |
1493 | except(KeyError): | 1493 | except(KeyError): |
1494 | return "" | 1494 | return "" |
1495 | 1495 |
invesalius/reader/dicom_reader.py
@@ -174,7 +174,7 @@ class LoadDicom: | @@ -174,7 +174,7 @@ class LoadDicom: | ||
174 | data_dict[group] = {} | 174 | data_dict[group] = {} |
175 | 175 | ||
176 | if not(utils.VerifyInvalidPListCharacter(data[1])): | 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 | else: | 178 | else: |
179 | data_dict[group][field] = "Invalid Character" | 179 | data_dict[group][field] = "Invalid Character" |
180 | 180 |