Coverage for pyguymer3/image/dict2exif.py: 5%

21 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 dict2exif( 

5 exif, 

6 /, 

7 *, 

8 mode = "RGB", 

9): 

10 """Convert a dictionary to an EXIF class 

11 

12 This function accepts a dictionary of key/value pairs and uses it to 

13 construct an EXIF class suitable for PIL to write to an image upon saving. 

14 

15 Parameters 

16 ---------- 

17 exif : dict 

18 the dictionary 

19 mode : str, optional 

20 the mode of the temporary image that is created to initialize the EXIF class 

21 

22 Returns 

23 ------- 

24 exifClass : PIL.Image.Exif 

25 the EXIF class 

26 

27 Notes 

28 ----- 

29 Copyright 2017 Thomas Guymer [1]_ 

30 

31 References 

32 ---------- 

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

34 """ 

35 

36 # Import special modules ... 

37 try: 

38 import PIL 

39 import PIL.ExifTags 

40 import PIL.Image 

41 PIL.Image.MAX_IMAGE_PIXELS = 1024 * 1024 * 1024 # [px] 

42 except: 

43 raise Exception("\"PIL\" is not installed; run \"pip install --user Pillow\"") from None 

44 

45 # Create an empty EXIF class (by obtaining the EXIF data from a blank image) ... 

46 # NOTE: See https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.getexif 

47 # NOTE: See https://pillow.readthedocs.io/en/stable/releasenotes/6.0.0.html#added-exif-class 

48 exifClass = PIL.Image.new(mode, (1, 1)).getexif() 

49 

50 # Check if the user supplied any EXIF tags ... 

51 if exif is not None: 

52 # Loop over user-supplied EXIF tags ... 

53 for userKey, userValue in exif.items(): 

54 # Initialize flag ... 

55 found = False 

56 

57 # Loop over standard EXIF tags ... 

58 # NOTE: See https://www.exiftool.org/TagNames/EXIF.html 

59 for exifKey, exifValue in PIL.ExifTags.TAGS.items(): 

60 # Skip this standard EXIF tag if it is not this user-supplied 

61 # EXIF tag ... 

62 if exifValue != userKey: 

63 continue 

64 

65 # Set flag and standard EXIF tag then stop looping ... 

66 found = True 

67 exifClass[exifKey] = userValue 

68 break 

69 

70 # Check if the standard EXIF tag was not found for this 

71 # user-supplied EXIF tag ... 

72 if not found: 

73 print(f"WARNING: No standard EXIF tag was found for the user-supplied EXIF tag \"{userKey}\".") 

74 

75 # Return answer ... 

76 return exifClass