Coverage for pyguymer3/image/exiftool.py: 80%

15 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 exiftool( 

5 fname, 

6 /, 

7 *, 

8 exiftoolPath = None, 

9 timeout = 60.0, 

10): 

11 """ 

12 "exiftool" does not modify, and it does not touch, the image even if it 

13 cannot strip anything, therefore it is safe to keep on running "exiftool" on 

14 the same image over and over again. 

15 """ 

16 

17 # Import standard modules ... 

18 import os 

19 import shutil 

20 import subprocess 

21 

22 # ************************************************************************** 

23 

24 # Try to find the paths if the user did not provide them ... 

25 if exiftoolPath is None: 

26 exiftoolPath = shutil.which("exiftool") 

27 assert exiftoolPath is not None, "\"exiftool\" is not installed" 

28 

29 # Check that the image exists ... 

30 if not os.path.exists(fname): 

31 raise Exception(f"\"{fname}\" does not exist") from None 

32 

33 # Extract file extension ... 

34 ext = os.path.splitext(fname)[1] 

35 

36 # Strip all metadata depending on the file extension ... 

37 match ext.lower(): 

38 case ".gif" | ".jpg" | ".jpeg" | ".png": 

39 subprocess.run( 

40 [ 

41 exiftoolPath, 

42 "-overwrite_original", 

43 "-all=", 

44 fname, 

45 ], 

46 check = True, 

47 encoding = "utf-8", 

48 stderr = subprocess.DEVNULL, 

49 stdout = subprocess.DEVNULL, 

50 timeout = timeout, 

51 ) 

52 case _: 

53 # Crash ... 

54 raise ValueError(f"\"ext.lower()\" is an unexpected value ({repr(ext.lower())})") from None