Coverage for pyguymer3/image/save_array_as_PNG.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def save_array_as_PNG( 

5 img, 

6 fname, 

7 /, 

8 *, 

9 calcAdaptive: bool = True, 

10 calcAverage: bool = True, 

11 calcNone: bool = True, 

12 calcPaeth: bool = True, 

13 calcSub: bool = True, 

14 calcUp: bool = True, 

15 debug: bool = __debug__, 

16 dpi: None | int = None, 

17 modTime = None, 

18): 

19 """Save an array as a PNG image. 

20 

21 Parameters 

22 ---------- 

23 img : numpy.ndarray 

24 a 3D NumPy array of uint8 type with shape (ny,nx,nc) 

25 fname : str 

26 output file name 

27 calcAdaptive : bool, optional 

28 See :py:func:`pyguymer3.image.makePng` for the documentation. 

29 calcAverage : bool, optional 

30 See :py:func:`pyguymer3.image.makePng` for the documentation. 

31 calcNone : bool, optional 

32 See :py:func:`pyguymer3.image.makePng` for the documentation. 

33 calcPaeth : bool, optional 

34 See :py:func:`pyguymer3.image.makePng` for the documentation. 

35 calcSub : bool, optional 

36 See :py:func:`pyguymer3.image.makePng` for the documentation. 

37 calcUp : bool, optional 

38 See :py:func:`pyguymer3.image.makePng` for the documentation. 

39 debug : bool, optional 

40 Print debug messages. 

41 dpi : None or float or int, optional 

42 See :py:func:`pyguymer3.image.makePng` for the documentation. 

43 modTime : None or datetime.datetime, optional 

44 See :py:func:`pyguymer3.image.makePng` for the documentation. 

45 

46 Notes 

47 ----- 

48 Copyright 2017 Thomas Guymer [1]_ 

49 

50 References 

51 ---------- 

52 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

53 """ 

54 

55 # Import standard modules ... 

56 import sys 

57 

58 # Import sub-functions ... 

59 from .makePng import makePng 

60 

61 # ************************************************************************** 

62 

63 # Check system ... 

64 assert sys.byteorder == "little", "the system is not little-endian" 

65 

66 # Check input ... 

67 assert img.dtype == "uint8", f"the NumPy array is not 8-bit (\"{img.dtype}\")" 

68 assert img.ndim == 3, f"the NumPy array is not 3D (\"{img.ndim:d}\")" 

69 assert img.shape[2] == 3, "the NumPy array does not have 3 colour channels" 

70 

71 # ************************************************************************** 

72 

73 # Make PNG source ... 

74 src = makePng( 

75 img, 

76 calcAdaptive = calcAdaptive, 

77 calcAverage = calcAverage, 

78 calcNone = calcNone, 

79 calcPaeth = calcPaeth, 

80 calcSub = calcSub, 

81 calcUp = calcUp, 

82 choices = "all", 

83 debug = debug, 

84 dpi = dpi, 

85 levels = [9,], 

86 memLevels = [9,], 

87 modTime = modTime, 

88 palUint8 = None, 

89 strategies = None, 

90 wbitss = [15,], 

91 ) 

92 

93 # Write PNG ... 

94 with open(fname, "wb") as fObj: 

95 fObj.write(src)