from PIL import Image
# 打开图像文件
img = Image.open("example.jpg")
# 显示图像
img.show()
# 调整图像大小
new_img = img.resize((500, 500))
# 保存调整大小后的图像
new_img.save("resized_example.jpg")
# 将图像转换为黑白
gray_img = img.convert('L')
# 保存黑白图像
gray_img.save("black_white_example.jpg")
# 翻转图像
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
# 保存翻转后的图像
flipped_img.save("flipped_example.jpg")
from PIL import Image 导入了Pillow库中的Image模块,用于处理图像。Image.open("example.jpg") 打开了一个名为 example.jpg 的图像文件。img.show() 显示图像。img.resize((500, 500)) 将图像调整为500x500像素的大小。new_img.save("resized_example.jpg") 将调整大小后的图像保存为 resized_example.jpg。img.convert('L') 将图像转换为灰度(黑白)模式。gray_img.save("black_white_example.jpg") 将黑白图像保存为 black_white_example.jpg。img.transpose(Image.FLIP_LEFT_RIGHT) 水平翻转图像。flipped_img.save("flipped_example.jpg") 将翻转后的图像保存为 flipped_example.jpg。这些代码展示了如何使用Pillow库进行基本的图像处理操作。
上一篇:python import用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站