Write OpenCV image in memory to BytesIO or Tempfile

前端 未结 1 505
忘掉有多难
忘掉有多难 2020-12-17 18:43

I need to write an OpenCV image that sits in memory to a BytesIO or Tempfile object for use elsewhere.

I am concerned this is a dead end question, because cv2.

相关标签:
1条回答
  • 2020-12-17 19:10

    cv2.imencode may help you:

    import numpy as np
    import cv2
    import io
    
    img = np.ones((100, 100), np.uint8)
    
    # encode
    is_success, buffer = cv2.imencode(".jpg", img)
    io_buf = io.BytesIO(buffer)
    
    # decode
    decode_img = cv2.imdecode(np.frombuffer(io_buf.getbuffer(), np.uint8), -1)
    
    print(np.allclose(img, decode_img))   # True
    
    0 讨论(0)
提交回复
热议问题