pr28416 commited on
Commit
7fc3883
·
1 Parent(s): 3a4d042

Remove size restrictions and fix physical dimensions calculation

Browse files
Files changed (1) hide show
  1. streamlit_app.py +20 -22
streamlit_app.py CHANGED
@@ -322,18 +322,21 @@ if uploaded is not None:
322
  with left:
323
  st.subheader("Slice preview")
324
  st.caption(
325
- "Drag to select a slice (100–1024 px) of the current channel to preview with your settings."
326
  )
327
  current_path = paths.get(sel or "", None)
328
  if current_path:
329
  # Load the same downsampled image that's shown in the viewer
330
  img_array = load_preview(current_path)
331
  pil_img = Image.fromarray(img_array.astype(np.uint8)).convert("L")
332
-
333
  # Get original image dimensions for physical scaling calculation
334
  orig_img = Image.open(current_path).convert("L")
335
- orig_h, orig_w = orig_img.size[1], orig_img.size[0] # PIL uses (W, H) format
336
-
 
 
 
337
  # Get the downsampling scale factor
338
  preview_h, preview_w = pil_img.size[1], pil_img.size[0]
339
  scale_factor = orig_w / preview_w # How much the preview is downsampled
@@ -341,11 +344,8 @@ if uploaded is not None:
341
  slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
342
  snp = np.array(slice_img)
343
  h, w = snp.shape[:2]
344
- if h < 100 or w < 100:
345
- st.warning(
346
- "Selected slice is too small. Increase selection to at least 100×100."
347
- )
348
- else:
349
  # Get original physical dimensions to maintain pixel-to-micron ratio
350
  s = st.session_state.get("_settings", {})
351
  orig_width_um = s.get("width_um", 1705.6)
@@ -354,23 +354,18 @@ if uploaded is not None:
354
  # Calculate pixel size from ORIGINAL image (this is the true pixel size)
355
  true_px_size_x_um = orig_width_um / orig_w
356
  true_px_size_y_um = orig_height_um / orig_h
357
-
358
  # Crop dimensions are from the downsampled preview, so scale them up to original resolution
359
  orig_crop_w = w * scale_factor
360
  orig_crop_h = h * scale_factor
361
 
362
- # Handle downscaling if slice is too large
363
  actual_h, actual_w = h, w
364
- if max(h, w) > 1024:
365
- scale = max(h, w) / 1024.0
366
- actual_h, actual_w = int(h / scale), int(w / scale)
367
- snp = resize(
368
- snp, (actual_h, actual_w), preserve_range=True
369
- ).astype(np.uint8)
370
-
371
- # Calculate effective physical dimensions using the TRUE pixel size from original image
372
- slice_width_um = actual_w * true_px_size_x_um
373
- slice_height_um = actual_h * true_px_size_y_um
374
 
375
  roi_path = os.path.join(prev_dir, "slice.png")
376
  iio.imwrite(roi_path, snp)
@@ -421,7 +416,10 @@ if uploaded is not None:
421
  f"🔧 Debug: Passing width_um={slice_width_um:.2f}, height_um={slice_height_um:.2f}"
422
  )
423
  st.write(
424
- f"🔧 Debug: Slice is {actual_w}×{actual_h} px, downsample={downsample}"
 
 
 
425
  )
426
  calc_px_size_x = slice_width_um / actual_w
427
  calc_px_size_y = slice_height_um / actual_h
 
322
  with left:
323
  st.subheader("Slice preview")
324
  st.caption(
325
+ "Drag to select a slice of the current channel to preview with your settings."
326
  )
327
  current_path = paths.get(sel or "", None)
328
  if current_path:
329
  # Load the same downsampled image that's shown in the viewer
330
  img_array = load_preview(current_path)
331
  pil_img = Image.fromarray(img_array.astype(np.uint8)).convert("L")
332
+
333
  # Get original image dimensions for physical scaling calculation
334
  orig_img = Image.open(current_path).convert("L")
335
+ orig_h, orig_w = (
336
+ orig_img.size[1],
337
+ orig_img.size[0],
338
+ ) # PIL uses (W, H) format
339
+
340
  # Get the downsampling scale factor
341
  preview_h, preview_w = pil_img.size[1], pil_img.size[0]
342
  scale_factor = orig_w / preview_w # How much the preview is downsampled
 
344
  slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
345
  snp = np.array(slice_img)
346
  h, w = snp.shape[:2]
347
+
348
+ if h > 0 and w > 0:
 
 
 
349
  # Get original physical dimensions to maintain pixel-to-micron ratio
350
  s = st.session_state.get("_settings", {})
351
  orig_width_um = s.get("width_um", 1705.6)
 
354
  # Calculate pixel size from ORIGINAL image (this is the true pixel size)
355
  true_px_size_x_um = orig_width_um / orig_w
356
  true_px_size_y_um = orig_height_um / orig_h
357
+
358
  # Crop dimensions are from the downsampled preview, so scale them up to original resolution
359
  orig_crop_w = w * scale_factor
360
  orig_crop_h = h * scale_factor
361
 
362
+ # Save the slice image (keep it at preview resolution for fast processing)
363
  actual_h, actual_w = h, w
364
+ # Note: We keep the slice at preview resolution but calculate correct physical dimensions
365
+
366
+ # Calculate physical dimensions based on ORIGINAL scale crop dimensions
367
+ slice_width_um = orig_crop_w * true_px_size_x_um
368
+ slice_height_um = orig_crop_h * true_px_size_y_um
 
 
 
 
 
369
 
370
  roi_path = os.path.join(prev_dir, "slice.png")
371
  iio.imwrite(roi_path, snp)
 
416
  f"🔧 Debug: Passing width_um={slice_width_um:.2f}, height_um={slice_height_um:.2f}"
417
  )
418
  st.write(
419
+ f"🔧 Debug: Slice image is {actual_w}×{actual_h} px (preview scale)"
420
+ )
421
+ st.write(
422
+ f"🔧 Debug: Represents {orig_crop_w:.0f}×{orig_crop_h:.0f} px in original, downsample={downsample}"
423
  )
424
  calc_px_size_x = slice_width_um / actual_w
425
  calc_px_size_y = slice_height_um / actual_h