Brooke's World The life and ramblings of Brooke.

December 21, 2020

Learning about the Pillow Python Library

Filed under: Uncategorized — Brooke @ 11:41 am

Spending some time brushing up on Python.  Spent some time learning about the Pillow 8.0.1 library in Python 3.9.1.

The image I started with.

from PIL import Image
import os

if __name__ == '__main__':
    # Open an image to get some basic info
    infile = "images/numbers-tiger-populations.jpg"
    im = Image.open(infile)
    print(f"""
bits {im.bits}
mode {im.mode}
size {im.size}
    """)

    # Show the image using the local viewer
    input("Press enter when you are ready to see the image.")
    im.show()

    # Create a thumbnail
    size = (128, 128)
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    with Image.open(infile) as im:
        im.thumbnail(size)
        im.save(outfile, "JPEG")

    # Show the thumbnail
    im = Image.open(outfile)
    input("Press enter when you are ready to see the thumbnail.")
    im.show()

    # convert to grayscale
    im = Image.open(infile).convert('LA')

    # Show the grayscale
    input("Press enter when you are ready to see the grayscale.")
    im.show()

Powered by WordPress