Coverage for pyguymer3/image/save_array_as_PPM.py: 78%

9 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_PPM( 

5 img, 

6 fname, 

7 /, 

8): 

9 """ 

10 Save an array as a PPM image. 

11 

12 img -- a 3D NumPy array of type uint8 with shape (ny,nx,3) 

13 fname -- output file name 

14 """ 

15 

16 # Find image size ... 

17 ny, nx, nc = img.shape 

18 

19 # Check image ... 

20 if not img.dtype == "uint8": 

21 raise TypeError("\"img\" must be a \"uint8\" array") from None 

22 if nc != 3: 

23 raise Exception("\"img\" must be a 3-channel array") from None 

24 

25 # Write out PPM ... 

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

27 fObj.write(f"P6 {nx:d} {ny:d} 255 ".encode("utf-8")) 

28 img.tofile(fObj)