如何在 Python 中使用 scikit-learn 库获取图像的分辨率?


数据预处理基本上是指将所有数据(从各种资源或单个资源收集)收集到通用格式或统一数据集(取决于数据类型)的任务。由于现实世界的数据从来都不是理想的,因此数据可能存在缺失单元格、错误、异常值、列不一致等等。有时,图像可能未正确对齐,或者可能不清楚,或者可能尺寸过大。预处理的目标是消除这些差异和错误。

要获取图像的分辨率,可以使用名为“shape”的内置函数。读取图像后,像素值将以数组的形式存储。此数组只不过是一个 NumPy 数组。读取图像并将其转换为数组后,可以在此图像上调用 shape 函数以了解其分辨率。

让我们以上传图像并在控制台上使用 scikit-learn 库获取图像分辨率为例:

示例

from skimage import io
path = "path to puppy.PNG"
img = io.imread(path)
print("Image being read")
io.imshow(img)
print("Image printed on console")
print("The image resolution is ")
print(img.shape)

输出

Image being read
Image printed on console
The image resolution is
(397, 558, 4)

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

解释

  • 导入所需的库。
  • 定义图像存储的路径。
  • 使用“imread”函数访问路径并读取图像。
  • 使用“imshow”函数在控制台上显示图像。
  • 使用“shape”函数获取图像的分辨率。
  • 输出的第三个值显示为 4,这意味着它有 4 个通道 - R、G、B 和 alpha 值。
  • 在控制台上显示数据。

更新于: 2020-12-10

351 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告