1. Mutual conversion between PIL and CV2

Import cv2 from PIL import numpy as np # PIL to cv2 img= image.open ("test.jpg") img= cv2.cvtColor(np.asarray(img), Cv2.color_rgb2bgr) print(type(img)) # cv2.imread img= cv2.imread("test.jpg") img= 1 Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) print(type(img))

2. Pil and Bytes transform each other

Pil with open('test.jpg', 'rb') as f: Content = f.read() local_img = image.open (BytesIO(content)) print(type(local_img) Converting bytes pictures on the network for PIL url = 'https://z3.ax1x.com/2021/07/13/WAuYJU.jpg' content = requests. Get (url, Stream =True).content net_img = image.open (BytesIO(content)) # Bytesio = Bytes print(type(net_img)) "PIL  ''' img_bytes = BytesIO() img = Image.open('test.jpg', mode='r') img.save(img_bytes, format='JPEG') img_bytes = img_bytes.getvalue() print(type(img_bytes))

3. CV2 and Bytes are converted to each other

Numpy as np import cv2 # bytes numpy img_buffer_numpy = np.fromBuffer (img_bytes, numpy = np.fromBuffer) Img_numpy = cv2.imdecode(img_buffer_numpy, 1) # Read 1D numpy data from the specified memory cache, # numpy tobytes, img_encode = cv2.imencode('.jpg', img_numpy) img_bytes = img_encode.tobytes()