Commit bc3ba9d1a9e1896628ca1652faa2ddf7c90ccbf1
1 parent
9bc7d016
Exists in
master
and in
6 other branches
FIX: UnicodeEncodeError when importing the case 0809, this case has non-ascii characters
Showing
4 changed files
with
52 additions
and
28 deletions
Show diff stats
invesalius/data/vtk_utils.py
... | ... | @@ -129,8 +129,10 @@ class Text(object): |
129 | 129 | value = str(value) |
130 | 130 | if sys.platform == 'win32': |
131 | 131 | value += "" # Otherwise 0 is not shown under win32 |
132 | - | |
133 | - self.mapper.SetInput(str(value)) | |
132 | + # With some encoding in some dicom fields (like name) raises a | |
133 | + # UnicodeEncodeError because they have non-ascii characters. To avoid | |
134 | + # that we encode in utf-8. | |
135 | + self.mapper.SetInput(value.encode("utf-8")) | |
134 | 136 | |
135 | 137 | def SetPosition(self, position): |
136 | 138 | self.actor.GetPositionCoordinate().SetValue(position[0], | ... | ... |
invesalius/gui/import_panel.py
... | ... | @@ -160,17 +160,17 @@ class TextPanel(wx.Panel): |
160 | 160 | first += 1 |
161 | 161 | |
162 | 162 | tree.SetItemPyData(parent, patient) |
163 | - tree.SetItemText(parent, str(dicom.patient.id), 1) | |
164 | - tree.SetItemText(parent, str(dicom.patient.age), 2) | |
165 | - tree.SetItemText(parent, str(dicom.patient.gender), 3) | |
166 | - tree.SetItemText(parent, str(dicom.acquisition.study_description), 4) | |
167 | - tree.SetItemText(parent, str(dicom.acquisition.modality), 5) | |
168 | - tree.SetItemText(parent, str(date_time), 6) | |
169 | - tree.SetItemText(parent, str(patient.nslices), 7) | |
170 | - tree.SetItemText(parent, str(dicom.acquisition.institution), 8) | |
171 | - tree.SetItemText(parent, str(dicom.patient.birthdate), 9) | |
172 | - tree.SetItemText(parent, str(dicom.acquisition.accession_number), 10) | |
173 | - tree.SetItemText(parent, str(dicom.patient.physician), 11) | |
163 | + tree.SetItemText(parent, "%s" % dicom.patient.id, 1) | |
164 | + tree.SetItemText(parent, "%s" % dicom.patient.age, 2) | |
165 | + tree.SetItemText(parent, "%s" % dicom.patient.gender, 3) | |
166 | + tree.SetItemText(parent, "%s" % dicom.acquisition.study_description, 4) | |
167 | + tree.SetItemText(parent, "%s" % dicom.acquisition.modality, 5) | |
168 | + tree.SetItemText(parent, "%s" % date_time, 6) | |
169 | + tree.SetItemText(parent, "%s" % patient.nslices, 7) | |
170 | + tree.SetItemText(parent, "%s" % dicom.acquisition.institution, 8) | |
171 | + tree.SetItemText(parent, "%s" % dicom.patient.birthdate, 9) | |
172 | + tree.SetItemText(parent, "%s" % dicom.acquisition.accession_number, 10) | |
173 | + tree.SetItemText(parent, "%s" % dicom.patient.physician, 11) | |
174 | 174 | |
175 | 175 | group_list = patient.GetGroups() |
176 | 176 | for group in group_list: |
... | ... | @@ -179,11 +179,11 @@ class TextPanel(wx.Panel): |
179 | 179 | child = tree.AppendItem(parent, group.title) |
180 | 180 | tree.SetItemPyData(child, group) |
181 | 181 | |
182 | - tree.SetItemText(child, str(group.title), 0) | |
183 | - tree.SetItemText(child, str(dicom.acquisition.protocol_name), 4) | |
184 | - tree.SetItemText(child, str(dicom.acquisition.modality), 5) | |
185 | - tree.SetItemText(child, str(date_time), 6) | |
186 | - tree.SetItemText(child, str(group.nslices), 7) | |
182 | + tree.SetItemText(child, "%s" % group.title, 0) | |
183 | + tree.SetItemText(child, "%s" % dicom.acquisition.protocol_name, 4) | |
184 | + tree.SetItemText(child, "%s" % dicom.acquisition.modality, 5) | |
185 | + tree.SetItemText(child, "%s" % date_time, 6) | |
186 | + tree.SetItemText(child, "%s" % group.nslices, 7) | |
187 | 187 | |
188 | 188 | tree.Expand(self.root) |
189 | 189 | ... | ... |
invesalius/project.py
... | ... | @@ -157,22 +157,27 @@ class Project(object): |
157 | 157 | ps.Publisher.sendMessage('Set raycasting preset', preset) |
158 | 158 | |
159 | 159 | def SavePlistProject(self, dir_, filename): |
160 | - | |
160 | + | |
161 | + # Some filenames have non-ascii characters and encoded in a strange | |
162 | + # encoding, in that cases a UnicodeEncodeError is raised. To avoid | |
163 | + # that we encode in utf-8. | |
164 | + filename = filename.encode('utf-8') | |
161 | 165 | dir_temp = tempfile.mkdtemp(filename) |
162 | 166 | filename_tmp = os.path.join(dir_temp, filename) |
163 | - | |
167 | + | |
164 | 168 | project = {} |
165 | - | |
169 | + | |
166 | 170 | for key in self.__dict__: |
167 | 171 | if getattr(self.__dict__[key], 'SavePlist', None): |
168 | - project[key] = {'#plist': self.__dict__[key].SavePlist(filename_tmp)} | |
172 | + project[key] = {'#plist': | |
173 | + self.__dict__[key].SavePlist(filename_tmp).decode('utf-8')} | |
169 | 174 | else: |
170 | 175 | project[key] = self.__dict__[key] |
171 | 176 | |
172 | 177 | masks = {} |
173 | 178 | for index in self.mask_dict: |
174 | 179 | masks[str(index)] = {'#mask':\ |
175 | - self.mask_dict[index].SavePlist(filename_tmp)} | |
180 | + self.mask_dict[index].SavePlist(filename_tmp).decode('utf-8')} | |
176 | 181 | print index |
177 | 182 | |
178 | 183 | surfaces = {} |
... | ... | @@ -185,9 +190,10 @@ class Project(object): |
185 | 190 | project['mask_dict'] = masks |
186 | 191 | img_file = '%s_%s.vti' % (filename_tmp, 'imagedata') |
187 | 192 | iu.Export(self.imagedata, img_file, bin=True) |
188 | - project['imagedata'] = {'$vti':os.path.split(img_file)[1]} | |
193 | + project['imagedata'] = {'$vti':os.path.split(img_file)[1].decode('utf-8')} | |
194 | + | |
189 | 195 | plistlib.writePlist(project, filename_tmp + '.plist') |
190 | - | |
196 | + | |
191 | 197 | path = os.path.join(dir_,filename) |
192 | 198 | Compress(dir_temp, path)#os.path.join("~/Desktop/","teste.inv3")) |
193 | 199 | shutil.rmtree(dir_temp) | ... | ... |
invesalius/reader/dicom.py
... | ... | @@ -90,6 +90,7 @@ class Parser(): |
90 | 90 | |
91 | 91 | def __init__(self): |
92 | 92 | self.filename = "" |
93 | + self.encoding = "" | |
93 | 94 | self.vtkgdcm_reader = vtkgdcm.vtkGDCMImageReader() |
94 | 95 | |
95 | 96 | def GetAcquisitionDate(self): |
... | ... | @@ -1136,7 +1137,8 @@ class Parser(): |
1136 | 1137 | data = self.vtkgdcm_reader.GetMedicalImageProperties()\ |
1137 | 1138 | .GetPatientName() |
1138 | 1139 | if (data): |
1139 | - return data.strip() | |
1140 | + # Returns a unicode decoded in the own dicom encoding | |
1141 | + return data.strip().decode(self.GetEncoding()) | |
1140 | 1142 | return "" |
1141 | 1143 | |
1142 | 1144 | def GetPatientID(self): |
... | ... | @@ -1150,7 +1152,8 @@ class Parser(): |
1150 | 1152 | data = self.vtkgdcm_reader.GetMedicalImageProperties()\ |
1151 | 1153 | .GetPatientID() |
1152 | 1154 | if (data): |
1153 | - return data | |
1155 | + # Returns a unicode decoded in the own dicom encoding | |
1156 | + return data.decode(self.GetEncoding()) | |
1154 | 1157 | return "" |
1155 | 1158 | |
1156 | 1159 | |
... | ... | @@ -1486,7 +1489,20 @@ class Parser(): |
1486 | 1489 | data = str(ds.GetDataElement(tag).GetValue()) |
1487 | 1490 | if (data): |
1488 | 1491 | return data |
1489 | - return "" | |
1492 | + return "" | |
1493 | + | |
1494 | + def GetEncoding(self): | |
1495 | + """ | |
1496 | + Return the dicom encoding | |
1497 | + DICOM standard tag (0x0008, 0x0005) was used. | |
1498 | + """ | |
1499 | + if self.encoding: | |
1500 | + return self.encoding | |
1501 | + tag = gdcm.Tag(0x0008, 0x0005) | |
1502 | + ds = self.gdcm_reader.GetFile().GetDataSet() | |
1503 | + if ds.FindDataElement(tag): | |
1504 | + self.encoding = str(ds.GetDataElement(tag).GetValue()) | |
1505 | + return self.encoding | |
1490 | 1506 | |
1491 | 1507 | |
1492 | 1508 | class DicomWriter: | ... | ... |