Exif.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import subprocess, os, sys
  4. # ------------------------------------------------------------------------------------------
  5. # MIAM project 2020
  6. # ------------------------------------------------------------------------------------------
  7. # author: remi.cozot@univ-littoral.fr
  8. # ------------------------------------------------------------------------------------------
  9. class Exif(object):
  10. """store exif data read image file with exiftool"""
  11. def __init__(self):
  12. """ constructor """
  13. # attributes
  14. self.imageName = None # image file name (String)
  15. self.exif = None # exif data (dict)
  16. def buildFromFileName(filename):
  17. """ return Exif (object) from image file """
  18. res = Exif()
  19. exifDict = dict()
  20. # reading metadata
  21. if os.path.exists(filename):
  22. try:
  23. exifdata = subprocess.check_output(['exiftool.exe','-a',filename],
  24. shell=True,
  25. universal_newlines=True,
  26. stdin=subprocess.PIPE,
  27. stderr=subprocess.PIPE)
  28. exifdata = exifdata.splitlines()
  29. except:
  30. print("ERROR[miam.iamge.Exif.buildFromFileName(",filename,"): can not be read!]")
  31. exifdata =[]
  32. # buid exif dict
  33. for each in exifdata:
  34. # tags and values are separated by a colon
  35. tag,val = each.split(':',1) # '1' only allows one split
  36. exifDict[tag.strip()] = val.strip()
  37. # set attributes
  38. res.imageName = filename
  39. res.exif = exifDict
  40. else:
  41. print("ERROR[miam.iamge.Exif.buildFromFileName(",filename,"): file not found]")
  42. sys.exit()
  43. return res
  44. def __repr__(self):
  45. resKeys = ""
  46. for k in self.exif:
  47. resKeys = resKeys+" "+k+": "+self.exif[k]+"\n"
  48. res = "Exif{ \n " + \
  49. " .imageName:" + self.imageName +"\n" + \
  50. " .exifDict:{ \n" + \
  51. resKeys + "}}"
  52. return res
  53. def __str__(self): return self.__repr__()