📅  最后修改于: 2023-12-03 15:34:16.714000             🧑  作者: Mango
PIL (Python Imaging Library) 是一个开源的 Python 图像处理库,提供了丰富的图像处理功能,包括图像的读取、创建、格式转换、裁剪、旋转、缩放、滤镜等。在 PIL 中,图像被视为一个 Image 对象,可以对 Image 对象进行各种操作。
在本文中,我们将介绍 PIL 中常用的属性,包括 Image 对象的大小、模式、格式等信息。
Image 对象的 size 属性是一个元组,包含了图像的宽度和高度信息。例如:
from PIL import Image
im = Image.open('lena.png')
print(im.size)
输出:
(512, 512)
Image 对象的 mode 属性是一个字符串,表示了图像的模式。不同的模式表示了不同的颜色空间和像素格式。常见的模式有:
例如:
from PIL import Image
im = Image.open('lena.png')
print(im.mode)
输出:
'RGB'
Image 对象的 format 属性是一个字符串,表示了图像的格式。常见的格式有:
例如:
from PIL import Image
im = Image.open('lena.png')
print(im.format)
输出:
'PNG'
除了 size 属性外,Image 对象也提供了 width 和 height 属性,分别表示图像的宽度和高度。例如:
from PIL import Image
im = Image.open('lena.png')
print(im.width, im.height)
输出:
512 512
Image 对象还提供了 filename 和 format_description 属性,分别表示所打开的图像文件名和格式的描述语句。例如:
from PIL import Image
im = Image.open('lena.png')
print(im.filename)
print(im.format_description)
输出:
lena.png
Portable network graphics
本文介绍了 PIL 中常用的 Image 对象属性,包括 size、mode、format、width、height、filename 和 format_description。通过使用这些属性,我们可以方便地获取到图像的各种信息,方便进行后续的图像处理。