@@ -731,6 +731,7 @@ def annotate_frame_with_component_legend(
731731 draw_lut = True ,
732732 transparent_background = False ,
733733 bg_thresh = 2 ,
734+ z_idx = None ,
734735):
735736 frame_rgba_u8 = _ensure_rgba_u8 (frame_rgba_u8 )
736737
@@ -758,11 +759,20 @@ def annotate_frame_with_component_legend(
758759 lut_w , lut_h = 140 , 10
759760 lut_pad = 10
760761
761- # Measure box
762- lines = [title ] + labels
762+ # Measure box - title, optional z-index, then labels
763+ lines = [title ]
764+ if z_idx is not None :
765+ # Label position in microns.
766+ # lines.append(f"Z-position: {z_idx * 0.065 / 4.2} µm")
767+ lines .append (f"Z-position: { z_idx * 0.15 / 4.2 :.2f} µm" )
768+ lines .extend (labels )
769+
763770 max_w , total_h = 0 , 0
764771 for i , line in enumerate (lines ):
765- font = title_font if i == 0 else label_font
772+ # Title and z-index use title_font, labels use label_font
773+ # Check if we're past the title (and optional z-index line)
774+ z_line_count = 2 if z_idx is not None else 1
775+ font = title_font if i < z_line_count else label_font
766776 bbox = draw .textbbox ((0 , 0 ), line , font = font )
767777 tw , th = bbox [2 ] - bbox [0 ], bbox [3 ] - bbox [1 ]
768778 extra = (lut_w + lut_pad ) if (draw_lut and i > 0 ) else 0
@@ -779,6 +789,14 @@ def annotate_frame_with_component_legend(
779789 draw .text ((x , cy ), title , font = title_font , fill = title_rgba )
780790 cy += (draw .textbbox ((0 , 0 ), title , font = title_font )[3 ]) + line_gap
781791
792+ # Z-index (if provided)
793+ if z_idx is not None :
794+ # z_text = (f"Z-position: {z_idx * 0.065 / 4.2} µm")
795+ z_text = (f"Z-position: { z_idx * 0.15 / 4.2 :.2f} µm" )
796+
797+ draw .text ((x , cy ), z_text , font = title_font , fill = title_rgba )
798+ cy += (draw .textbbox ((0 , 0 ), z_text , font = title_font )[3 ]) + line_gap
799+
782800 # Labels (each colored by its own LUT + gamma)
783801 for label in labels :
784802 if label not in viewer .layers :
@@ -807,7 +825,8 @@ def annotate_frame_with_component_legend(
807825
808826
809827def save_frame (
810- viewer , out_path : str , comp : str , groups : dict , transparent_background = True
828+ viewer , out_path : str , comp : str , groups : dict , transparent_background = True ,
829+ z_idx = None
811830):
812831 frame = viewer .screenshot (canvas_only = True , flash = False , size = (4000 , 4000 ))
813832 labels = groups .get (comp , [])
@@ -820,6 +839,7 @@ def save_frame(
820839 draw_lut = True ,
821840 transparent_background = transparent_background ,
822841 bg_thresh = 2 ,
842+ z_idx = z_idx ,
823843 )
824844
825845 Image .fromarray (frame2 , mode = "RGBA" ).save (out_path )
0 commit comments