10/27/2019

Python: Create in Memory Zip File

simple example to make zip in memory

..
import io
import zipfile

def makeZip(data_list):
    zip_buffer = io.BytesIO()
    with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, Falseas zip_file:
        for file_name, data in data_list:
            zip_file.writestr(file_name, data.getvalue())
    return zip_buffer

data_list = [('1.txt', io.BytesIO(b'111')), ('2.txt', io.BytesIO(b'222'))]
zip_buffer = makeZip(data_list)
with open('./b.zip''wb'as f:
    f.write(zip_buffer.getvalue())
..

Thank you.

10/19/2019

Byte 2 opencv Mat


refer to below source code. ^^

import base64
import numpy as np
import cv2

def byte2Mat(data):
    imgdata = base64.b64decode(data)
    nparr = np.frombuffer(imgdata, np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    print(img.shape)
    
    return img