Coverage for tests/res/single_resolution_rgb_image.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-31 11:24 +0000

1import tifffile 

2import os 

3import numpy as np 

4from qubalab.images.metadata.image_shape import ImageShape 

5 

6 

7def get_name() -> str: 

8 return "single_resolution_rgb.ome.tif" 

9 

10 

11def get_path() -> str: 

12 return os.path.realpath(os.path.join(os.path.realpath(__file__), os.pardir, get_name())) 

13 

14 

15def get_shapes() -> tuple[ImageShape, ...]: 

16 return ( 

17 ImageShape(32, 16, c=3), 

18 ) 

19 

20 

21def get_pixel_size_x_y_in_micrometers() -> float: 

22 return 0.1 

23 

24 

25def get_dtype(): 

26 return np.uint8 

27 

28 

29def get_downsamples() -> tuple[float, ...]: 

30 return tuple([get_shapes()[0].x / shape.x for shape in get_shapes()]) 

31 

32 

33def get_pixel_value(x: int, y: int, c: int) -> int: 

34 return pixels[c, y, x] 

35 

36 

37def _get_pixels() -> np.array: 

38 width = get_shapes()[0].x 

39 height = get_shapes()[0].y 

40 

41 pixels = [] 

42 for c in range(get_shapes()[0].c): 

43 channel = [] 

44 for y in range(height): 

45 row = [] 

46 for x in range(width): 

47 if c == 0: 

48 row.append(int(255 * x / width)) 

49 elif c == 1: 

50 row.append(int(255 * y / height)) 

51 else: 

52 row.append(int(255 * x / width * y / height)) 

53 channel.append(row) 

54 pixels.append(channel) 

55 return np.array(pixels, get_dtype()) 

56 

57 

58def _write_image(pixels: np.array): 

59 metadata = { 

60 'PhysicalSizeX': get_pixel_size_x_y_in_micrometers(), 

61 'PhysicalSizeXUnit': 'µm', 

62 'PhysicalSizeY': get_pixel_size_x_y_in_micrometers(), 

63 'PhysicalSizeYUnit': 'µm' 

64 } 

65 

66 with tifffile.TiffWriter(get_path()) as tif: 

67 number_of_pixels_per_cm = 1e4 / get_pixel_size_x_y_in_micrometers() 

68 

69 tif.write( 

70 pixels, 

71 metadata=metadata, 

72 photometric='rgb', 

73 resolution=(number_of_pixels_per_cm, number_of_pixels_per_cm), 

74 resolutionunit=3 # indicate that the resolution above is in cm^-1 

75 ) 

76 

77 

78pixels = _get_pixels() 

79_write_image(pixels)