import base64
import happybase
import numpy as np
import cv2 

def decode_image_b64(b64_data):
    """ Returns opencv image from b64 buffer.
    """
    data = base64.b64decode(b64_data)
    img_array = np.asarray(bytearray(data), dtype=np.uint8)
    # see CV_LOAD_IMAGE_XXX flags in documentation of ``imread`` fucnction at:
    # http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html
    return cv2.imdecode(img_array, 1)

if __name__=="__main__":
    tab_image = 'image_cache'    
    conn = happybase.Connection(host='10.1.94.57')
    image_sha1 = '0000007031E3FA80C97940017253BEAB542EA334'
    table = conn.table(tab_image)
    row = table.row(image_sha1)
    img = decode_image_b64(row['image:binary'])
    cv2.imwrite(image_sha1+'.jpeg',img)

@@ -47,21 +47,108 @@
  np%0A
-import cv2 %0A%0A
+from StringIO import StringIO%0A#import cv2 %0Afrom PIL import Image%0Afrom PIL.ImageFile import Parser%0A%0A#
 def 
@@ -164,23 +164,29 @@
 age_
-b64(b64
+opencv(binary
 _data):%0A
     
@@ -181,16 +181,17 @@
 _data):%0A
+#
     %22%22%22 
@@ -217,18 +217,21 @@
 e from b
-64
+inary
  buffer.
@@ -235,16 +235,17 @@
 er.%0A
+#
     %22%22%22%0A
     
@@ -244,46 +244,9 @@
 %22%22%22%0A
-    data = base64.b64decode(b64_data)%0A
+#
     
@@ -301,16 +301,17 @@
 .uint8)%0A
+#
     # se
@@ -381,16 +381,17 @@
 ion at:%0A
+#
     # ht
@@ -477,16 +477,17 @@
 eo.html%0A
+#
     retu
@@ -515,16 +515,358 @@
 rray, 1)
+%0A    %0Adef decode_image_PIL_parser(binary_data):%0A    %22%22%22 Returns PIL image from binary buffer.%0A    %22%22%22%0A    img_parser = Parser()%0A    img_parser.feed(data)%0A    return img_parser.close()%0A%0Adef decode_image_PIL(binary_data):%0A    %22%22%22 Returns PIL image from binary buffer.%0A    %22%22%22%0A    f = StringIO(binary_data)%0A    img = Image.open(f)%0A    return img
 %0A%0Aif __n
@@ -884,16 +884,39 @@
 ain__%22:%0A
+    use_opencv = False%0A
     tab_
@@ -1052,16 +1052,77 @@
 2EA334'%0A
+    #image_sha1 = '000001EC5DD154E58B72326EFC26A41A4C8E9586'%0A
     tabl
@@ -1187,88 +1187,335 @@
     
-img = decode_image_b64(row%5B'image:binary'%5D)%0A    cv2.imwrite(image_sha1+'.jpeg',img)
+b64_data = row%5B'image:binary'%5D%0A    size = (int(row%5B'image:width'%5D),int(row%5B'image:height'%5D))%0A    # assuming all images are jpeg%0A    if use_opencv:%0A        img = decode_image_opencv(b64_data)%0A        cv2.imwrite(image_sha1+'.jpeg',img)%0A    else:%0A        img = decode_image_PIL(b64_data)%0A        img.save(image_sha1+'.jpeg',%22JPEG%22)%0A
 %0A

read OK