Demo Resize Image

Download the image used: ludlow_1200x478.png

#!/usr/bin/python3
# ==================================================================
# create an image scaled to a specific size
#
# python-pillow.org/                         # pillow home
# pillow.readthedocs.io/en/stable/handbook/  # pillow handbook
#
# -------------------------------------------------------------------
# this code assumes pillow is installed:
#
# python3 -m pip install --upgrade pip      # upgrade pip
# python3 -m pip install --upgrade Pillow   # upgrade pillow
# ===================================================================

import PIL
from PIL import Image


infile  = 'ludlow_1200x478.png'

outfile = 'ludlow_resized.png'


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

# ---- pillow version

print(f'PIL version: {PIL.VERSION}')


# ---- process image

with Image.open(infile) as img:

    ##img.show()

    # ---- resize image and maintain aspect ratio

    width, height = img.size

    img = img.resize((width//2, height//2))

    img.show()

    # ---- save resized image

    img.save(outfile)

# ---- final messages

print()
print(f'input  image {infile} size ({width},{height})')
print(f'output image {outfile} resized to ({width/2},{height/2})')
print()