Neemah commited on
Commit
44d5b58
·
verified ·
1 Parent(s): 7d1a171

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +19 -10
utils.py CHANGED
@@ -9,20 +9,29 @@ from reportlab.lib.units import inch
9
 
10
  def load_dicom(filepath):
11
  """
12
- Reads a DICOM file and returns a PIL Image.
 
13
  """
14
- dicom = pydicom.dcmread(filepath)
 
 
 
15
 
16
- # Extract the pixel array
17
- pixel_array = dicom.pixel_array.astype(float)
18
 
19
- # Normalize to 0-255 range
20
- pixel_min = pixel_array.min()
21
- pixel_max = pixel_array.max()
22
- normalized = (pixel_array - pixel_min) / (pixel_max - pixel_min) * 255
 
 
 
 
23
 
24
- # Convert to uint8 RGB image
25
- image = Image.fromarray(normalized.astype(np.uint8)).convert("RGB")
 
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