将opencv图像格式转换为PIL图像格式? [英] Convert opencv image format to PIL image format?

查看:557
本文介绍了将opencv图像格式转换为PIL图像格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转换加载的图片

TestPicture = cv2.imread("flowers.jpg")

我想运行PIL过滤器( http://pillow.readthedocs.io/en/4.0.x/reference/ImageFilter.html )就像示例 https://wellfire.co/learn/python-image-enhancements/ 变量

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

TestPicture

但我无法将其转换回来,在这些类型之间转换为第四。

but I'm unable to convert it back and fourth between these types.

有没有办法进行这种转换?

Is there a way to do this conversion??

opencv可以执行PIL包中的所有图像过滤器吗?

Can opencv do all of the image filters that are in the PIL package?

推荐答案

是的OpenCV是更强大和灵活,可以执行大部分可用的图像处理程序,所以可能这个过滤器可以b完成OpenCV>然而,可能没有一个简单的API。

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转换为PIL,您可以使用 Image.fromarray as:

Anyways, as far as the conversion of image format from OpenCV to PIL is concerned you may use Image.fromarray as:

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)

但你必须记住,OpenCV遵循 BGR 约定和 PIL 遵循 RGB 颜色约定,因此为了保持一致,您可能需要在转换之前使用 cv2.cvtColor()

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图像格式转换为PIL图像格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆