Python练习题(三):PIL和OpenCV提取图像大小的方法和区别

在使用Python图像处理时,最常用的有PIL的image.size,以及OpenCV的image.shape,两者有什么区别呢,可以看看下方程序运行的结果。

btw:运行下方程序的前提是系统已经安装了Pillow和OpenCV依赖库,安装的方法参考:https://blog.csdn.net/alice_tl/article/details/89291235

from PIL import Image
image = Image.open('/Users/alice/Documents/Develop/PythonCode/imagetest.JPG')
print(image.size)

执行的结果为:

(1672, 1364)

from PIL import Image

image = Image.open('/Users/alice/Documents/Develop/PythonCode/imagetest.JPG')
weight,high = image.size
print('(%s,%s)' % (high,weight))
print(image.size)

执行的结果为:

(1364,1672)
(1672, 1364)

import cv2
image = cv2.imread('/Users/alice/Documents/Develop/PythonCode/imagetest.JPG')
print (image.shape)
# (640, 780, 3)
print (image.size)
# 1228800
print (image.dtype)
# uint8

执行的结果为:

(1364, 1672, 3)
6841824
uint8

可以看到PIL的提取图像大小用的是image.size,而OpenCV对应的是image.shape。

另外image.size默认输出的结果是宽和高,而OPenCV默认输出的结果是高和宽。

猜你喜欢

转载自blog.csdn.net/alice_tl/article/details/89296110