Coverage for tests/images/test_utils.py: 100%

30 statements  

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

1import numpy as np 

2import imageio.v3 as iio 

3import base64 

4from qubalab.images.utils import bytes_to_image, base64_to_image 

5from ..res import multi_resolution_uint8_3channels 

6 

7 

8def test_uri_to_image(): 

9 level = 0 

10 downsample = multi_resolution_uint8_3channels.get_downsamples()[level] 

11 path = multi_resolution_uint8_3channels.get_path() 

12 image_shape = multi_resolution_uint8_3channels.get_shapes()[level] 

13 expected_image = np.array( 

14 [[[multi_resolution_uint8_3channels.get_pixel_value(downsample, x, y, c) 

15 for x in range(image_shape.x)] 

16 for y in range(image_shape.y)] 

17 for c in range(image_shape.c)], 

18 multi_resolution_uint8_3channels.get_dtype() 

19 ) 

20 

21 image = bytes_to_image(path, False, image_shape) 

22 

23 np.testing.assert_array_equal(image, expected_image) 

24 

25 

26def test_bytes_to_image(): 

27 level = 0 

28 downsample = multi_resolution_uint8_3channels.get_downsamples()[level] 

29 image_shape = multi_resolution_uint8_3channels.get_shapes()[level] 

30 expected_image = np.array( 

31 [[[multi_resolution_uint8_3channels.get_pixel_value(downsample, x, y, c) 

32 for x in range(image_shape.x)] 

33 for y in range(image_shape.y)] 

34 for c in range(image_shape.c)], 

35 multi_resolution_uint8_3channels.get_dtype() 

36 ) 

37 bytes = iio.imwrite("<bytes>", expected_image, extension=".tiff", photometric='rgb') 

38 

39 image = bytes_to_image(bytes, False, image_shape) 

40 

41 np.testing.assert_array_equal(image, expected_image) 

42 

43 

44def test_base64_to_image(): 

45 level = 0 

46 downsample = multi_resolution_uint8_3channels.get_downsamples()[level] 

47 image_shape = multi_resolution_uint8_3channels.get_shapes()[level] 

48 expected_image = np.array( 

49 [[[multi_resolution_uint8_3channels.get_pixel_value(downsample, x, y, c) 

50 for x in range(image_shape.x)] 

51 for y in range(image_shape.y)] 

52 for c in range(image_shape.c)], 

53 multi_resolution_uint8_3channels.get_dtype() 

54 ) 

55 bytes = iio.imwrite("<bytes>", expected_image, extension=".tiff", photometric='rgb') 

56 base64_image = base64.b64encode(bytes) 

57 

58 image = base64_to_image(base64_image, False, image_shape) 

59 

60 np.testing.assert_array_equal(image, expected_image)