2948 explained code solutions for 75 technologies


python-pillowHow to resize an image and maintain aspect ratio


from PIL import Image, ImageDraw, ImageFont

im = Image.open('/var/www/examples/heroine.png')
width = 100
height = width * im.size[1]/im.size[0]
im = im.resize((width, height))

im.show()ctrl + c
PIL

import Pillow package modules

Image.open

open given image with Pillow

100

resized image width

im.size

returns original image width and height

width * im.size[1]/im.size[0]

calculates height so that aspect ratio is maintained

.resize(

resizes given image to the specified size

.show()

displays resulting image


How to resize an image and maintain aspect ratio, python pillow

Usage example

from PIL import Image, ImageDraw, ImageFont

im = Image.open('/var/www/examples/heroine.png')
width = 150
height = round(width * im.size[1]/im.size[0])
im = im.resize((width, height))
im.show()