在Python中读取PNG图像,您可以使用Pillow库,它是Python Imaging Library(PIL)的一个分支,专门用于处理图像。以下是使用Pillow库读取PNG图像的基本步骤和示例代码:
1. 首先,确保已经安装了Pillow库。如果尚未安装,可以使用以下命令进行安装:
pip install pillow
2. 然后,您可以使用以下代码读取PNG图像:
from PIL import Image
打开图像文件
image = Image.open('image.png')
显示图像大小和格式
print('图像大小:', image.size)
print('图像格式:', image.format)
显示图像
image.show()
关闭图像
image.close()
请确保将代码中的`image.png`替换为您要读取的PNG图像文件的路径。