import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

img_url = r'E://0.png'
with open(img_url, 'rb') as f:
a = f.read()

# 二进制数据流转np.ndarray [np.uint8: 8位像素]
img = cv2.imdecode(np.frombuffer(a, np.uint8), cv2.IMREAD_COLOR)
# # 将bgr转为rbg
rgb_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
print(rgb_img)
# np.ndarray转IMAGE
a = Image.fromarray(rgb_img)
print(a)
# 显示图片
a.show()

"""
这个模块真牛逼;
"""
from io import BytesIO
with open("E://0.png","rb")as f:
bt = BytesIO(f.read())


import cv2
img = cv2.imread("E://0.png")
a = img[0:500,0:500]

# cv2.imwrite("E://ssss.png",a)
# print(type(a))


# b_arr = a.tobytes()
# print(type(b_arr))


success,encoded_image = cv2.imencode(".png",a)
print(success,encoded_image)
#将数组转为bytes
img_bytes = encoded_image.tostring()
with open("E://shabi.png","wb")as f:
f.write(img_bytes)

-----------------------------------------------------------------------------------------------------------------------------------------