jp4_composite.py 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
'''
SPDX-License-Identifier: GPL-3.0-or-later
Copyright (c) 2020, Elphel, Inc.

Author:      Oleg Dzhimiev <oleg@elphel.com>
Description: Class for converting composite JP4 raw images to
             JPEG for previewing.
             Composite images consist of 2-3 single ones stacked vertically - 
             for example in Eyesis4Pi panoramic camera.
             The methods here are specifically for Eyesis4Pi - channels are
             hardcoded.
'''
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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

import re
import os
from PIL import Image, ImageOps
import numpy as np

from jp4 import JP4

class JP4_C(JP4):

  def __init__(self,filename):

    JP4.__init__(self,filename)
    self.filename = filename
    # filename includes path
    # if multiple cameras the files will be indexed: basename_{i}.ext
    self.model = "Unknown"
    self.basename = self.__basename(filename)
    self.subnames = self.__subnames()


  # extract basename if part of indexed set
  def __basename(self,s):

    m = re.search('_\d+\.',s)
    # likely
    if m:
      basename = os.path.basename(s[:m.start(0)])
    # unlikely
    else:
      basename = os.path.splittext(os.path.basename(s))[0]

    return basename

  # based on application name
  def __subnames(self):

    subnames = [0,1,2]

    # model tag has camera name and its channel
    model = self.exif.data['Model']

    m = re.search('^EYESIS',model)
    if m:

      self.model = "EYESIS"

      m = re.search('CHN(\d+)$',model)
      if m:
        chn = int(m.group(1))

      if chn==109:
        subnames = [24,25]
      else:
        subnames = [chn-101,chn-101+8,chn-101+16]

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
69 70
    else:

71 72 73 74
      if self.exif.data['Page']:
        subnames = self.exif.data['Page'][0]
      else:
        subnames = 0
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
75

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
    return subnames


  # based on exif MakeNote
  # return array of subframes - flipped and rotated
  def extract_subframes(self):

    # convert to PIL format and reorder
    # TODO: change stacking in jp4.py to RGB
    I = Image.fromarray(self.image[...,[2,1,0]].astype(np.uint8))

    exif = self.exif
    mn = exif.MakerNote

    subframes = []

    W = mn['WOI_WIDTH']
    H = mn['WOI_HEIGHT']
    FH = mn['FLIPH']
    FV = mn['FLIPV']

    v_offset = 0

    for i in range(1,4):

      try:
        height = mn['HEIGHT'+str(i)]
        #fliph  = mn['FLIPH'+str(i)]
        flipv  = mn['FLIPV'+str(i)]
      except KeyError:
        # break for '_9'
        break

      # actual break for '_9'
      if height==0:
        break

      try:
        blank = mn['BLANK'+str(i)]
      except KeyError:
        blank = 0

      crop = I.crop((0,v_offset,W,v_offset+height))

      if flipv:
        crop = ImageOps.flip(crop)

      # if eyesis4pi, 1&2 - rotate 90CW, 3 - 90CCW
      if i<3:
        # special case
        if self.model=="EYESIS" and self.subnames[i-1]==25:
          crop = crop.rotate(90,expand=1)
        else:
          crop = crop.rotate(-90,expand=1)
      else:
        crop = crop.rotate(90,expand=1)

      subframes.append(crop)

      #crop.save("result_"+str(i)+".jpeg")

      v_offset += height + blank

    return subframes