Pillow
和OpenCV
使用不同格式的图像。因此,您不能只在Pillow
读取图像并使用它在OpenCV中操纵图像。 Pillow使用RGB
格式(突出显示@ZdaR),而OpenCV
使用BGR
格式。因此,您需要一个转换器将一种格式转换为另一种格式。
要将PIL
图像转换为OpenCV
使用:
import cv2
import numpy as np
from PIL import Image
pil_image=Image.open("demo2.jpg") # open image using PIL
# use numpy to convert the pil_image into a numpy array
numpy_image=numpy.array(pil_img)
# convert to a openCV2 image, notice the COLOR_RGB2BGR which means that
# the color is converted from RGB to BGR format
opencv_image=cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
要将OpenCV
图像转换为PIL
图像,请使用:
import cv2
import numpy as np
from PIL import Image
opencv_image=cv2.imread("demo2.jpg") # open image using openCV2
# convert from openCV2 to PIL. Notice the COLOR_BGR2RGB which means that
# the color is converted from BGR to RGB
pil_image=Image.fromarray(
cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
)
0
我想转换加载的图像
我想像示例https://wellfire.co/learn/python-image-enhancements一样运行PIL过滤器( http://pillow.readthedocs.io/en/4.0.x/reference/ImageFilter.html ) /与变量
但我无法在这些类型之间将其转换为第四位。
有没有办法进行这种转换?
opencv可以做PIL软件包中的所有图像过滤器吗?