Python OpenCV load image from byte string

前端 未结 3 1231
小鲜肉
小鲜肉 2020-12-01 01:55

I\'m trying to load image from string like as PHP function imagecreatefromstring

How can I do that?

I have MySQL blob field image. I\'m using

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 02:40

    I think this answer provided on this stackoverflow question is a better answer for this question.

    Quoting details (borrowed from @lamhoangtung from above linked answer)

    import base64
    import json
    import cv2
    import numpy as np
    
    response = json.loads(open('./0.json', 'r').read())
    string = response['img']
    jpg_original = base64.b64decode(string)
    jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
    img = cv2.imdecode(jpg_as_np, flags=1)
    cv2.imwrite('./0.jpg', img)
    

提交回复
热议问题