在Python中调整图像大小可以通过以下几种方法实现:
1. 使用Pillow库:
from PIL import Image
打开原始图片
img = Image.open('image.jpg')
设置新的尺寸
new_size = (800, 600)
调整尺寸
resized_img = img.resize(new_size)
保存调整后的图片
resized_img.save('resized_image.jpg')
2. 使用Matplotlib库:
import matplotlib.pyplot as plt
创建图形时设置尺寸
plt.figure(figsize=(12, 5))
绘制图形
...
保存图形
plt.savefig('resized_figure.png', dpi=300)
3. 使用Seaborn库(在绘制图形时调整尺寸):
import seaborn as sns
import matplotlib.pyplot as plt
绘制图形
...
设置图形尺寸
plt.figure(figsize=(12, 5))
调整布局
plt.tight_layout()
保存图形
plt.savefig('resized_figure.png', dpi=300)
请根据您的需求选择合适的方法。