Python 将opencv图像格式转换为PIL图像格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43232813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert opencv image format to PIL image format?
提问by Arsenal Fanatic
I want to convert an image loaded
我想转换加载的图像
TestPicture = cv2.imread("flowers.jpg")
I would like to run a PIL filter (http://pillow.readthedocs.io/en/4.0.x/reference/ImageFilter.html) like on the example https://wellfire.co/learn/python-image-enhancements/with the variable
我想运行一个 PIL 过滤器(http://pillow.readthedocs.io/en/4.0.x/reference/ImageFilter.html)就像示例https://wellfire.co/learn/python-image-enhancements /与变量
TestPicture
but I'm unable to convert it back and fourth between these types.
但我无法在这些类型之间将其转换为第四位。
Is there a way to do this conversion??
有没有办法进行这种转换?
Can opencv do all of the image filters that are in the PIL package?
opencv 可以做 PIL 包中的所有图像过滤器吗?
回答by ZdaR
Yes OpenCV is more robust and flexible and can perform most of the image processing routines which are available out there, So probably this filter can be done with OpenCV> However, there may not be a straightforward API for that.
是的,OpenCV 更健壮和灵活,可以执行大多数可用的图像处理例程,所以这个过滤器可能可以用 OpenCV 来完成> 但是,可能没有直接的 API。
Anyways, as far as the conversion of image format from OpenCV to PIL is concerned you may use Image.fromarray
as:
无论如何,就图像格式从 OpenCV 到 PIL 的转换而言,您可以使用Image.fromarray
:
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)
But you must keep in mind that, OpenCV follows BGR
convention and PIL
follows RGB
color convention, so to keep the things consistent you may need to do use cv2.cvtColor()
before conversion.
但是您必须记住,OpenCV 遵循BGR
约定并PIL
遵循RGB
颜色约定,因此为了保持一致,您可能需要cv2.cvtColor()
在转换前使用。
回答by alpha_989
Pillow
and OpenCV
use different formats of images. So you can't just read a image in Pillow
and use it manipulate the image in OpenCV.
Pillow uses theRGB
format as @ZdaR highlighted, and OpenCV
uses the BGR
format. So to you need a convertor to convert from one format to another.
Pillow
并OpenCV
使用不同格式的图像。因此,您不能只读取图像Pillow
并使用它在 OpenCV 中操作图像。Pillow 使用RGB
@ZdaR 突出显示OpenCV
的BGR
格式,并使用该格式。因此,您需要一个转换器来从一种格式转换为另一种格式。
To convert from PIL
image to OpenCV
use:
要从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)
To convert from OpenCV
image to PIL
image use:
要将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)
)