python中,opencv图片对象如何转化为f.read()这种类型的bytes

我已经知道从numpy.ndarray (图片)到字节流bytes:
x = img.tobytes()

但是我现在想要的是将numpy.ndarray (图片)到f.read()这种类型的bytes

with open("1.png","rb") as f:
    bytes=f.read()

如图,
numpy.ndarray (图片)使用tobytes()到字节流bytes是上面的编码格式

f.read()是下面的字节编码方式

图片说明

import numpy as np
import cv2
import requests


def cv2ImgToBytes(img):
    # 如果直接tobytes写入文件会导致无法打开,需要编码成一种图片文件格式(jpg或png),再tobytes
    # 这里得到的bytes 和 with open("","rb") as f: bytes=f.read()的bytes可能不一样,如果用这里得到的bytes保存过一次,下次就f.read()和cv2ImgToBytes(img)会一样
    return cv2.imencode('.png', img)[1].tobytes()

def bytesToCv2Img(bytes):
    return cv2.imdecode(np.fromstring(r.content,"uint8"), 1)


img = cv2.imread(r"D:\temp\test.png")
cv2.imwrite(r"D:\temp\test1.png",img)
# 等价于
with open(r"D:\temp\test1.png","wb") as f:
    f.write(cv2ImgToBytes(img))


url = 'http://5b0988e595225.cdn.sohucs.com/images/20190623/e185060522bb46a59217ff5d6addfe75.jpeg'
r = requests.get(url)
img=bytesToCv2Img(r.content)
cv2.imshow("Xuan Yi",img)
cv2.waitKey()