from PIL import Image
# 打开图像文件
img = Image.open("example.jpg")
# 显示图像
img.show()
# 获取图像的基本信息
print(f"图像格式: {img.format}")
print(f"图像大小: {img.size}")
print(f"图像模式: {img.mode}")
# 保存图像为其他格式
img.save("example.png")
# 创建一个新的白色背景的图像
new_img = Image.new('RGB', (500, 500), color = 'white')
# 裁剪图像
box = (100, 100, 400, 400) # 左上角和右下角坐标
region = img.crop(box)
# 旋转图像
rotated_img = img.rotate(45)
# 粘贴图像到另一个图像上
new_img.paste(region, box)
# 调整图像大小
resized_img = img.resize((300, 300))
# 翻转图像
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
# 应用滤镜
from PIL import ImageFilter
blurred_img = img.filter(ImageFilter.BLUR)
# 将图像转换为灰度
gray_img = img.convert('L')
# 保存所有修改后的图像
rotated_img.save("rotated_example.jpg")
resized_img.save("resized_example.jpg")
flipped_img.save("flipped_example.jpg")
blurred_img.save("blurred_example.jpg")
gray_img.save("gray_example.jpg")
上一篇:python download
下一篇:python换行符 怎么用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站