Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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,
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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
|
51 |
-
slice_image = resize_slice(slice_image
|
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()
|