画像をリサイズする(PythonのPillowを使用)

Python
1
2
3
4
5
6
7
8
9
10
11
12
# 画像をリサイズする
from PIL import Image
 
img = Image.open("neko_neteru.jpg"# 元の画像ファイル
 
print("元のサイズ {}*{}".format(img.size[0], img.size[1]))
 
width = 1280  # 変更したい幅(ピクセル)
 
resize_img = img.resize((width, int(width * img.size[1] / img.size[0])))  # 縦横比固定でリサイズ
resize_img.save("resize.jpg"# リサイズした画像を保存
print("変更後のサイズ {}*{}".format(resize_img.size[0], resize_img.size[1]))

コメント