From JPG to b64encode to cv2.imread()

后端 未结 2 1860
面向向阳花
面向向阳花 2020-12-03 16:18

For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without savi

相关标签:
2条回答
  • 2020-12-03 16:57

    From the OpenCV documentation we can see that:

    imread : Loads an image from a file.

    imdecode : Reads an image from a buffer in memory.

    Seem a better way to do what you want.

    0 讨论(0)
  • 2020-12-03 16:58

    You can get a numpy array from you decoded data using:

    import numpy as np
    ...
    img = base64.b64decode(msg.payload)
    npimg = np.fromstring(img, dtype=np.uint8)
    

    Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.

    So:

    import numpy as np
    ...
    def on_message(client, userdata, msg): # msg.payload is incoming data
        img = base64.b64decode(msg.payload); 
        npimg = np.fromstring(img, dtype=np.uint8); 
        source = cv2.imdecode(npimg, 1)
    
    0 讨论(0)
提交回复
热议问题