Coverage for /home/ubuntu/hidebound/python/hidebound/core/traits.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-05 23:50 +0000

1from typing import Union # noqa F401 

2 

3from pathlib import Path 

4 

5import skimage.io 

6# ------------------------------------------------------------------------------ 

7 

8 

9''' 

10The traits module contains functions that return file traits give a filepath. 

11These traits are used for validation of specifications. 

12''' 

13 

14 

15def get_image_width(filepath): 

16 # type: (Union[str, Path]) -> int 

17 ''' 

18 Gets the width of the given image. 

19 

20 Args: 

21 filepath (str or Path): filepath to image file. 

22 

23 Returns: 

24 int: Image width. 

25 ''' 

26 img = skimage.io.imread(Path(filepath).as_posix()) 

27 return img.shape[1] 

28 

29 

30def get_image_height(filepath): 

31 # type: (Union[str, Path]) -> int 

32 ''' 

33 Gets the height of the given image. 

34 

35 Args: 

36 filepath (str or Path): filepath to image file. 

37 

38 Returns: 

39 int: Image height. 

40 ''' 

41 img = skimage.io.imread(Path(filepath).as_posix()) 

42 return img.shape[0] 

43 

44 

45def get_num_image_channels(filepath): 

46 # type: (Union[str, Path]) -> int 

47 ''' 

48 Gets the number of channels of the given image. 

49 

50 Args: 

51 filepath (str or Path): filepath to image file. 

52 

53 Returns: 

54 int: Number of channels. 

55 ''' 

56 img = skimage.io.imread(Path(filepath).as_posix()) 

57 if len(img.shape) > 2: 

58 return img.shape[2] 

59 return 1