from PIL import Image
# 打开图片文件
image = Image.open('example.jpg')
# 显示图片
image.show()
# 获取图片的基本信息
print(f"图片格式: {image.format}")
print(f"图片尺寸: {image.size}")
print(f"图片模式: {image.mode}")
# 将图片转换为灰度图像
gray_image = image.convert('L')
gray_image.show()
# 保存处理后的图片
gray_image.save('gray_example.jpg')
PIL
库中的 Image
模块来处理图片。PIL
是 Python Imaging Library 的缩写,它提供了强大的图像处理功能。Image.open()
方法打开指定路径的图片文件(例如 'example.jpg'
)。image.show()
方法在默认图像查看器中显示图片。image.format
, image.size
, 和 image.mode
获取图片的格式、尺寸和颜色模式,并打印出来。image.convert('L')
将图片转换为灰度图像('L' 表示灰度模式)。gray_image.save()
方法将处理后的图片保存到指定路径。如果需要进一步的操作或不同类型的图片处理,请根据需求调整代码。
上一篇:python判断变量的类型
下一篇:python字典get函数用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站