opencv and PIL of pillow Conversion Operations in python
- 2021-10-13 07:50:46
- OfStack
opencv > pil
import cv2
from PIL import Image
img = cv2.imread("test.png")
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
pil > opencv
import cv2
from PIL import Image
image = Image.open("test.png")
img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
Supplement: skimage and opencv picture format conversion
skimage is an image library of python, which is often used as the basic library of video and image classes together with matplotlib1. As a very popular visual library, opencv is very common in image processing. This paper introduces the conversion of image formats between the two libraries.
skimage
Image format
Channel: RGB
Pixel value: [0, 1]
Opencv
Image format
Channel: BGR
Pixel value: [0,255]
Conversion
The conversion process is now encapsulated as follows:
def skimage2opencv(src):
src *= 255
src.astype(int)
cv2.cvtColor(src,cv2.COLOR_RGB2BGR)
return src
def opencv2skimage(src):
cv2.cvtColor(src,cv2.COLOR_BGR2RGB)
src.astype(float32)
src /= 255
return src