vincentgao95 commited on
Commit
e3b93fc
·
verified ·
1 Parent(s): d7aaffb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -28,27 +28,34 @@ def extract_slice(data, view, slice_index):
28
  elif view == 'Sagittal':
29
  return data[slice_index, :, :]
30
 
31
- def resize_slice(slice_image, view, data_shape):
32
- if view == 'Axial':
33
- return slice_image # No resizing needed for Axial view
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
- # For Coronal and Sagittal views, find the two largest dimensions
36
- if view == 'Coronal':
37
- resize_dims = (data_shape[0], data_shape[2]) # (x, z)
38
- elif view == 'Sagittal':
39
- resize_dims = (data_shape[1], data_shape[2]) # (y, z)
40
-
41
- # Resize the slice image while maintaining the aspect ratio
42
- resized_image = Image.fromarray(slice_image).resize(resize_dims, Image.ANTIALIAS)
43
- return np.array(resized_image)
44
 
45
  def visualize_slice(file_obj, view, slice_index):
46
  data = load_nrrd(file_obj)
47
- data_shape = data.shape # Get the original x, y, z dimensions
48
  slice_image = extract_slice(data, view, slice_index)
49
 
50
- # Resize the slice image if necessary
51
- slice_image = resize_slice(slice_image, view, data_shape)
52
 
53
  # Plot the slice
54
  fig, ax = plt.subplots()
 
28
  elif view == 'Sagittal':
29
  return data[slice_index, :, :]
30
 
31
+ def resize_slice(slice_image, max_size=(500, 200)):
32
+ # Convert the NumPy array to a PIL image
33
+ slice_pil = Image.fromarray(slice_image)
34
+
35
+ # Get original dimensions
36
+ original_width, original_height = slice_pil.size
37
+
38
+ # Calculate aspect ratio
39
+ aspect_ratio = original_width / original_height
40
+
41
+ # Determine new dimensions based on the aspect ratio
42
+ if original_width > original_height:
43
+ new_width = min(max_size[0], original_width)
44
+ new_height = int(new_width / aspect_ratio)
45
  else:
46
+ new_height = min(max_size[1], original_height)
47
+ new_width = int(new_height * aspect_ratio)
48
+
49
+ # Resize the image
50
+ resized_image = slice_pil.resize((new_width, new_height), Image.ANTIALIAS)
51
+ return np.array(resized_image)
 
 
 
52
 
53
  def visualize_slice(file_obj, view, slice_index):
54
  data = load_nrrd(file_obj)
 
55
  slice_image = extract_slice(data, view, slice_index)
56
 
57
+ # Resize the slice image while maintaining aspect ratio
58
+ slice_image = resize_slice(slice_image)
59
 
60
  # Plot the slice
61
  fig, ax = plt.subplots()