将cv2读取的图片变为PIL Image or ndarray
时间: 2024-04-29 08:25:25 浏览: 70
python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结
将cv2读取的图片变为PIL Image:
```python
import cv2
from PIL import Image
img_cv2 = cv2.imread("image.jpg")
img_pil = Image.fromarray(cv2.cvtColor(img_cv2,cv2.COLOR_BGR2RGB))
```
将cv2读取的图片变为ndarray:
```python
import cv2
import numpy as np
img_cv2 = cv2.imread("image.jpg")
img_ndarray = np.array(img_cv2)
```
阅读全文