Resize image using Python
Resize image in python using pillow library
1
pip install pillow
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import PIL
from PIL import Image
MAXWIDTH = 450
filename = '1.jpg'
img = Image.open(filename)
image_bytes = len(img.fp.read())
print(f'image size = {image_bytes}')
s= img.size
ratio = MAXWIDTH/s[0];
print(f'ratio = {ratio}')
newimg = img.resize((int(s[0]*ratio), int(s[1]*ratio)) , Image.NEAREST)
newimg.save('resized_1.jpg')
print(f'new image size = {newimg.size}')
This post is licensed under CC BY 4.0 by the author.