python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python OpenCV处理图像

python中 OpenCV和Pillow处理图像操作及时间对比

作者:Liekkas Kono

这篇文章主要介绍了python中OpenCV和Pillow处理图像操作及时间对比,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

引言

OpenCV和Pillow的优缺点对比

优点缺点
OpenCV由C和C++编写,跨平台,有着多个语言的实现,部署比较方便对显示中文支持较差、Python下常用函数不是太好看-_-!
Pillow常用函数操作封装较好,对显示中文字体有着很好的支持处理时间较慢

测试环境:

测试图像 :

读取图像的通道顺序区别:

获得图像shape区别:

示例代码:

import cv2
from PIL import Image

img_path = 'images/test_demo.png'

cv_img = cv2.imread(img_path)
height, width, channel = cv_img.shape

pillow_img = Image.open(img_path)
width, height = pillow_img.size

读写图像

读图像

示例代码:

import cv2
from PIL import Image
import numpy as np

png_img_path = 'images/test_demo.png'
jpg_img_path = 'images/test_demo.jpg'

# 由jupyter notebook中魔法命令:%%timeit测得
# 169 ms ± 1.68 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
cv_img = cv2.imread(png_img_path)    

# 52.9 ms ± 541 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
cv_img = cv2.imread(jpg_img_path)

# 300 ms ± 8.45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
pillow_img = Image.open(png_img_path)
pillow_img = np.array(pillow_img)

# 47.4 ms ± 1.87 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
pillow_img = Image.open(jpg_img_path)
pillow_img = np.array(pillow_img)

小结:

写图像

示例代码:

save_png_path = 'output/result.png'
save_jpg_path = 'output/result.jpg'

cv_img = cv2.imread(png_img_path)
pillow_img = Image.open(png_img_path)

# 346 ms ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
cv2.imwrite(save_png_path, cv_img)

# 158 ms ± 4.03 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
cv2.imwrite(save_jpg_path, cv_img)

# 2.81 s ± 38.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
pillow_img.save(save_png_path)

# 51.3 ms ± 1.72 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
t = pillow_img.convert('RGB') 
t.save(save_jpg_path)

小结:

缩放图像

示例代码:

png_img_path = 'images/test_demo.png'

resize_shape = (2048, 2048)
cv_img = cv2.imread(png_img_path)
pillow_img = Image.open(png_img_path)

# 6.93 ms ± 173 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
cv2.resize(cv_img, resize_shape, interpolation=cv2.INTER_CUBIC)

# 151 ms ± 2.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
pillow_img.resize(resize_shape, resample=Image.Resampling.BICUBIC)

小结: OpenCV速度完胜Pillow

旋转图像

示例代码:

angle = 38

# 23.6 ms ± 732 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
h, w = cv_img.shape[:2]
M = cv2.getRotationMatrix2D((w / 2, h / 2), angle, 1)
rot_img = cv2.warpAffine(cv_img, M, (w, h))

# 82.1 ms ± 2.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
rot_img_pillow = np.array(pillow_img.rotate(angle))

小结:OpenCV速度完胜Pillow 

总结:

到此这篇关于python中 OpenCV和Pillow处理图像操作及时间对比的文章就介绍到这了,更多相关python OpenCV处理图像 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文