elphel_exif.py 4.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
__author__ = "Oleg Dzhimiev"
__copyright__ = "Copyright (C) 2018 Elphel Inc."
__license__ = "GPL-3.0+"
__email__ = "oleg@elphel.com"

'''
Example usage:
test.py:
  import elphel_exif
  info = elphel_exif.Exif("test.jpeg")
  print(info)
  print(info.MakerNote)
  print(info.exif.toString())
'''

from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS


# Might be needed by some applications like:
#   panoramic of Eyesis4Pi, composite images
def elphel_add_to_PIL_TAGS():
    TAGS[0x0129] = 'Page'
    TAGS[0xC62F] = 'Camera Serial Number'


# Open file and Parse
def elphel_get_exif(f):
  ret = {}

  i = Image.open(f)
  info = i._getexif()

  elphel_add_to_PIL_TAGS()

  for tag, value in info.items():
    decoded = TAGS.get(tag, tag)

    if decoded == 'GPSInfo':
      gret = {}
      for gtag,gval in value.items():
        gdecoded = GPSTAGS.get(gtag,gtag)
        gret[gdecoded] = gval
      value = gret

    if decoded == 'MakerNote':
      value = elphel_MakerNote_parse(value)

    ret[decoded] = value
  return ret


# Init with default values
def elphel_MakerNote_defaults():
  ret = {}
  # defaults
  ret['COLOR_MODE'] = 0
  return ret


# Based on JP46_Reader_camera.java's jp46Reorder()
def elphel_MakerNote_parse(MakerNote):
  ret = {}
  ret = elphel_MakerNote_defaults()

  ret['GAIN_R' ] = MakerNote[0]/65536.0
  ret['GAIN_G']  = MakerNote[1]/65536.0
  ret['GAIN_GB'] = MakerNote[2]/65536.0
  ret['GAIN_B']  = MakerNote[3]/65536.0

  ret['BLACK_R']  = (MakerNote[4]>>24)/256.0
  ret['BLACK_G']  = (MakerNote[5]>>24)/256.0
  ret['BLACK_GB'] = (MakerNote[6]>>24)/256.0
  ret['BLACK_B']  = (MakerNote[7]>>24)/256.0

  ret['GAMMA_R']  = ((MakerNote[4]>>16)&0xff)/100.0
  ret['GAMMA_G']  = ((MakerNote[5]>>16)&0xff)/100.0
  ret['GAMMA_GB'] = ((MakerNote[6]>>16)&0xff)/100.0
  ret['GAMMA_B']  = ((MakerNote[7]>>16)&0xff)/100.0

  ret['GAMMA_SCALE_R']  = MakerNote[4]&0xffff
  ret['GAMMA_SCALE_G']  = MakerNote[5]&0xffff
  ret['GAMMA_SCALE_GB'] = MakerNote[6]&0xffff
  ret['GAMMA_SCALE_B']  = MakerNote[7]&0xffff

  if len(MakerNote)>=14:

    if (MakerNote[10]&0xc0000000)!=0:
      composite = 1
    else:
      composite = 0

    ret['COMPOSITE'] = composite

    if composite:

      ret['HEIGHT1'] = MakerNote[11]&0xffff
      ret['BLANK1'] = (MakerNote[11]>>16)&0xffff

      ret['HEIGHT2'] = MakerNote[12]&0xffff
      ret['BLANK2'] = (MakerNote[12]>>16)&0xffff

      ret['HEIGHT3'] = (MakerNote[9]>>16)-ret['HEIGHT1']-ret['BLANK1']-ret['HEIGHT2']-ret['BLANK2']

      ret['FLIPH1'] = (MakerNote[10]>>24)&0x1
      ret['FLIPV1'] = (MakerNote[10]>>25)&0x1

      ret['FLIPH2'] = (MakerNote[10]>>26)&0x1
      ret['FLIPV2'] = (MakerNote[10]>>27)&0x1

      ret['FLIPH3'] = (MakerNote[10]>>28)&0x1
      ret['FLIPV3'] = (MakerNote[10]>>29)&0x1

    ret['PORTRAIT'] = (MakerNote[13]>>7)&0x1
    ret['YTABLEFORC'] = (MakerNote[13]>>15)&0x1
    ret['QUALITY'] = MakerNote[13]&0x7f
    ret['CQUALITY'] = (MakerNote[13]>>8)&0x7f

    if ret['CQUALITY']==0:
      ret['CQUALITY'] = ret['QUALITY']

    ret['CORING_INDEX_Y'] = (MakerNote[13]>>16)&0x7f
    ret['CORING_INDEX_C'] = (MakerNote[13]>>24)&0x7f

    if ret['CORING_INDEX_C']==0:
      ret['CORING_INDEX_C'] = ret['CORING_INDEX_Y']

  if len(MakerNote)>=12:

    ret['WOI_LEFT']   = MakerNote[8]&0xffff
    ret['WOI_WIDTH']  = MakerNote[8]>>16
    ret['WOI_TOP']    = MakerNote[9]&0xffff
    ret['WOI_HEIGHT'] = MakerNote[9]>>16
    ret['FLIPH']      = MakerNote[10]&0x1
    ret['FLIPV']      = (MakerNote[10]>>1)&0x1
    ret['BAYER_MODE'] = (MakerNote[10]>>2)&0x3

    ret['COLOR_MODE'] = (MakerNote[10]>>4)&0x0f

    ret['DCM_HOR']    = (MakerNote[10]>>8)&0x0f
    ret['DCM_VERT']   = (MakerNote[10]>>12)&0x0f
    ret['BIN_HOR']    = (MakerNote[10]>>16)&0x0f
    ret['BIN_VERT']   = (MakerNote[10]>>20)&0x0f

  # HW specific temperature readings
  if len(MakerNote)>=16:

    ret['TEMP1'] = (MakerNote[14]>> 0)&0xffff
    ret['TEMP2'] = (MakerNote[14]>>16)&0xffff
    ret['TEMP3'] = (MakerNote[14]>> 0)&0xffff
    ret['TEMP4'] = (MakerNote[14]>>16)&0xffff

  return ret


# Class
class Exif:

  def __init__(self,f):
    self.f = f
    self.data = elphel_get_exif(f)
    self.MakerNote = self.data['MakerNote']
    #self.color_mode = self.data['MakerNote']['COLOR_MODE']


  def toString(self):
    res = ''
    for k in self.data:
      if k!='MakerNote':
        res += str(k)+": "+str(self.data[k])+"\n"

    res += "MakerNote: \n"
    for k in self.data['MakerNote']:
      res += "    "+str(k)+": "+str(self.data['MakerNote'][k])+"\n"

    return res[:-1]