Update utils.py
Browse files
utils.py
CHANGED
|
@@ -9,20 +9,29 @@ from reportlab.lib.units import inch
|
|
| 9 |
|
| 10 |
def load_dicom(filepath):
|
| 11 |
"""
|
| 12 |
-
|
|
|
|
| 13 |
"""
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
return image
|
| 28 |
|
|
|
|
| 9 |
|
| 10 |
def load_dicom(filepath):
|
| 11 |
"""
|
| 12 |
+
Accepts a list of DICOM file objects.
|
| 13 |
+
Returns a list of PIL Images, one per sequence.
|
| 14 |
"""
|
| 15 |
+
|
| 16 |
+
images=[]
|
| 17 |
+
for file in filepaths:
|
| 18 |
+
dicom = pydicom.dcmread(filepath)
|
| 19 |
|
| 20 |
+
# Extract the pixel array
|
| 21 |
+
pixel_array = dicom.pixel_array.astype(float)
|
| 22 |
|
| 23 |
+
# Normalize to 0-255 range
|
| 24 |
+
pixel_min = pixel_array.min()
|
| 25 |
+
pixel_max = pixel_array.max()
|
| 26 |
+
|
| 27 |
+
if pixel_max - pixel_min == 0:
|
| 28 |
+
continue #to handle sequences not added
|
| 29 |
+
|
| 30 |
+
normalized = (pixel_array - pixel_min) / (pixel_max - pixel_min) * 255
|
| 31 |
|
| 32 |
+
# Convert to uint8 RGB image
|
| 33 |
+
image = Image.fromarray(normalized.astype(np.uint8)).convert("RGB")
|
| 34 |
+
images.append(image)
|
| 35 |
|
| 36 |
return image
|
| 37 |
|