1、opencv

2、imageio

3、matplotlib

4、scipy


# coding:utf-8

import cv2
import imageio
from scipy import misc
from PIL import Image
from matplotlib import pyplot as plt

image_path = "./images/000011.jpg"

# 使用pillow读取图片,获取图片的宽和高
img_pillow = Image.open(image_path)
img_width = img_pillow.width  # 图片宽度
img_height = img_pillow.height  # 图片高度
print("width -> {}, height -> {}".format(img_width, img_height))

img_cv = cv2.imread(image_path)
img_imageio = imageio.imread(image_path)
img_scipy = misc.imread(image_path)
img_matplot = plt.imread(image_path)

print(img_cv.shape)
print(img_imageio.shape)
print(img_scipy.shape)
print(img_matplot.shape)

输出结果如下:

width -> 2000, height -> 1333
(1333, 2000, 3)
(1333, 2000, 3)
(1333, 2000, 3)
(1333, 2000, 3)

注意事项:读取出的图像矩阵的shape是按 高度、宽度、通道数 这个顺序,图像宽度是第一个维度,其实也就是返回的图像矩阵的行数、列数、通道数。

补充:

在图像处理中通常需要将图像按照宽、高、通道顺序进行存放,可以使用transpose方法进行转换:

# 交换0 1 坐标轴的数据,也就是交换高和宽的顺序为宽和高
img_transpose = img.transpose(1, 0, 2)
print(img_transpose.shape)

输出结果如下:

(2000, 1333, 3)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐