第16天-使用Python Pillow库常见图像处理场景
1. 打开与显示图像
from PIL import Image# 打开图像文件 img = Image.open("input.jpg")# 显示图像基本信息 print(f"格式: {img.format}") # JPEG print(f"尺寸: {img.size}") # (宽度, 高度) print(f"模式: {img.mode}") # RGB# 显示图像 img.show()
2. 调整图像尺寸
from PIL import Imageimg = Image.open("input.jpg")# 调整到指定尺寸 resized_img = img.resize((800, 600))# 按比例缩放(缩小50%) width, height = img.size scaled_img = img.resize((width//2, height//2))# 保存结果 resized_img.save("resized.jpg") scaled_img.save("scaled.jpg")