Coverage for collectra/displays/image_segmentation.py: 100.00%

15 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 04:26 +0000

1"""GUI mask descriptors; extraction continues to use transparent cropped images.""" 

2 

3from PIL import Image 

4from .preview import bounded 

5 

6 

7def display_bounds(item): 

8 """Keep the full source available for expanding a segmentation mask.""" 

9 from collectra.types.images import ImageSegmentation 

10 

11 if isinstance(item, ImageSegmentation): 

12 return (0, 0, item.raw_width, item.raw_height) 

13 return item.display_bounds() 

14 

15 

16def segmentation(context, item): 

17 alpha = item._mask_image().rotate(item.orientation.to_degree(), expand=True) 

18 left, top, right, bottom = alpha.getbbox() 

19 width, height = alpha.size 

20 alpha = bounded(alpha, mask=True, max_pixels=context.max_pixels) 

21 mask = Image.new("RGBA", alpha.size, "white") 

22 mask.putalpha(alpha) 

23 return { 

24 "mask_source": context.publish_image(mask), 

25 "width": width, 

26 "height": height, 

27 "preview_width": alpha.width, 

28 "preview_height": alpha.height, 

29 "orientation": item.orientation.to_string(), 

30 "bounds": {"x": left, "y": top, "w": right - left, "h": bottom - top}, 

31 }