Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/implementors/imageviewer/mosaic.py: 32%

56 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-15 02:12 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3import time 

4from PIL import Image 

5 

6from marshmallow import fields 

7 

8from daiquiri.core.components import ComponentActor, ComponentActorSchema 

9 

10 

11class MosaicSchema(ComponentActorSchema): 

12 x1 = fields.Int(required=True, metadata={"title": "X Start"}) 

13 y1 = fields.Int(required=True, metadata={"title": "Y Start"}) 

14 x2 = fields.Int(required=True, metadata={"title": "X End"}) 

15 y2 = fields.Int(required=True, metadata={"title": "Y End"}) 

16 sampleid = fields.Int(required=True, metadata={"title": "Sampleid"}) 

17 steps_x = fields.Int(required=True) 

18 steps_y = fields.Int(required=True) 

19 

20 def time_estimate(self, data): 

21 return data["steps_x"] * data["steps_y"] * 5 

22 

23 

24class MosaicActor(ComponentActor): 

25 schema = MosaicSchema 

26 name = "mosaic" 

27 saving_args = {"data_filename": "{sampleid.name}_mosaic{sampleactionid}"} 

28 

29 def method(self, **kwargs): 

30 print("Add mosaic", kwargs) 

31 

32 x = kwargs["absol"]["axes"].get("x") 

33 if x: 

34 if kwargs["steps_x"] > 1: 

35 sizex = (x["destination"][1] - x["destination"][0]) / ( 

36 kwargs["steps_x"] - 1 

37 ) 

38 else: 

39 sizex = x["destination"][1] - x["destination"][0] 

40 

41 y = kwargs["absol"]["axes"].get("y") 

42 if y: 

43 if kwargs["steps_y"] > 1: 

44 sizey = (y["destination"][1] - y["destination"][0]) / ( 

45 kwargs["steps_y"] - 1 

46 ) 

47 else: 

48 sizey = y["destination"][1] - y["destination"][0] 

49 

50 kwargs["absol"]["move_to"](kwargs["absol"]) 

51 

52 im_w = None 

53 im_h = None 

54 full = None 

55 for sy in range(kwargs["steps_y"]): 

56 for sx in range(kwargs["steps_x"]): 

57 if (sy + 1) % 2 == 0: 

58 ssx = kwargs["steps_x"] - sx - 1 

59 else: 

60 ssx = sx 

61 

62 if x: 

63 x["motor"].move(x["destination"][0] + (sizex * ssx)) 

64 

65 if y: 

66 y["motor"].move(y["destination"][0] + (sizey * sy)) 

67 

68 if x: 

69 x["motor"].wait() 

70 

71 if y: 

72 y["motor"].wait() 

73 

74 time.sleep(kwargs["camera"].get("exposure") * 1.2) 

75 

76 resp = kwargs["save"](ssx, sy) 

77 

78 if sy == 0 and ssx == 0: 

79 first = Image.open(resp["path"]) 

80 im_w = first.size[0] 

81 im_h = first.size[1] 

82 

83 full = Image.new( 

84 "RGB", (im_w * kwargs["steps_x"], im_h * kwargs["steps_y"]) 

85 ) 

86 

87 full.paste(im=Image.open(resp["path"]), box=(ssx * im_w, sy * im_h)) 

88 self.update(full=full)