Package pyforms :: Package web :: Package Controls :: Module ControlImage |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.web.Controls.ControlImage

     1  from pyforms.web.Controls.ControlBase import ControlBase
     2  import cv2
     3  import base64
     4  import numpy as np
     5  import StringIO
     6  from PIL import Image
     7
     8 -class ControlImage(ControlBase):
     9
    10      _filename = ''
    11
    12 -    def initControl(self):
    13          return "controls.push(new ControlImage('"+self._name+"','%s'));" %  self.help
    14
    15 -    def save(self, data):
    16          if self.value!=None: data['value'] = self._value
    17
    18 -    def load(self, data):
    19          if 'value' in data: self.value = data['value']
    20
    21 -    def repaint(self): pass
    22
    23      @property
    24 -    def value(self):
    25          result = { 'image': '', 'filename': self._filename }
    26          image = ControlBase.value.fget(self)
    27          if isinstance(image, np.ndarray):
    28              if len(image.shape)>2: image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    29              image = Image.fromarray(image)
    30              buff = StringIO.StringIO()
    31              image.save(buff, format="PNG")
    32              content = buff.getvalue()
    33              buff.close()
    34              result['image'] = base64.b64encode(content)
    35          return result
    36
    37
    38      @value.setter
    39 -    def value(self, value):
    40          if isinstance(value, np.ndarray):
    41              self._value = value
    42
    43          if isinstance( value, (str, unicode) ):
    44              self._value = cv2.imread(value)
    45              self._filename = value
    46          if isinstance( value, dict ):
    47              self._value =  cv2.imread(value['filename'] )
    48              self._filename = value['filename']
    49