Python Pil. Image and OpenCV Image formats convert to each other

Binary open picture file, base64 codec into Opencv format:

# coding: utf-8
import base64
import numpy as np
import cv2
 
img_file = open('1.jpg'.'rb')   Open the image file in binary
img_b64encode = base64.b64encode(img_file.read())  # base64 encoding
img_file.close()  # file close
img_b64decode = base64.b64decode(img_b64encode)  # base64 decoding
 
img_array = np.fromstring(img_b64decode,np.uint8) # Convert NP sequence
img=cv2.imdecode(img_array,cv2.COLOR_BGR2RGB)  # Convert Opencv format
 
cv2.imshow("img",img)
cv2.waitKey()
Copy the code

Binary open Image file, base64 codec, convert to pil. Image format:

# coding: Utf-8 # Python base64 codec, convert to Opencv, Pil. Image import base64 import IO from PIL import Image img_file = open('1.jpg','rb') # binary open Image file img_b64encode = Base64.b64encode (img_file.read()) # base64 encode img_file.close() # file close img_b64decode = base64.b64decode(img_b64encode) # Image = io.bytesio (img_b64decode) img = image.open (image) img.show()Copy the code

Welcome to pay attention to the official number: learning log of algorithm engineers, to obtain learning materials of algorithm engineers. If you have technical consultation, we will provide paid consultation, contact QQ (1762016542) or leave a message on the official account