import cv2 import numpy as np from PIL import Image img = cv2.imread("path/to/img.png") # You may need to convert the color. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(img) # For reversing the operation: im_np = np.asarray(im_pil)
Here is what the above code is Doing:
1. Reads the image into a numpy array.
2. Converts the color.
3. Converts the numpy array into a PIL image.
4. Converts the PIL image back into a numpy array.