Edit Images

Caveat: Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL if it currently installed. Please read the installation instructions.

Support for the original Python Imaging Library was discontinued in 2011. A project named Pillow forked the original PIL project and added Python3 support.

Introduction

Pillow is an Imaging Library which provides image editing capabilities for Python.

Project #1

Create two directories. One for input image files, and one for output image files.

Create a program to do the following:

Download several test images from the web. See Wikimedia Commons .

Project #2

What else can you do to images with Pillow? Demonstrate these capabilities by adding them to Project #1.

Allow the user to select which edit capabilities to use? And which order?

Project #3

This might be a good time to implement the "Automated Picture Frame" project?

Links

Pillow (PIL Fork) (documentation)

Python Pillow - Quick Guide

How to use Pillow (PIL: Python Imaging Library)

Example Code - a place to start

#!/usr/bin/python3
# ===================================================================
# from: www.youtube.com/watch?v=vEQ8CXFWLZU
#       3 Python Automation Projects
# ===================================================================

from PIL import Image, ImageEnhance, ImageFilter
import re
import os

dirIn  = './images-in'
dirOut = './images-out'
imgPat = [ r'\.jpg$',   # file name regular expresion patterns
           r'\.png$' ]

# ---- string match -------------------------------------------------

def string_match(patterns,str):

    for p in patterns:
        if re.search(p,str,re.IGNORECASE):
            return True
    return False

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

for filename in sorted(os.listdir(dirIn)):

    if not string_match(imgPat,filename):
        continue

    print(f'IN: {filename}')

    img = Image.open(f'{dirIn}/{filename}')

    ###edit = img.filter(ImageFilter.SHARPEN).convert('L').rotate(90)
    edit = img.filter(ImageFilter.SHARPEN).convert('L')

    factor = 1.5
    enhancer = ImageEnhance.Contrast(edit)
    edit = enhancer.enhance(factor)    

    # os.path.splitext() method is used to split the path name into
    # root and ext. ext (extension) is the file type. Root is
    # everything except the ext.
    clean_name = os.path.splitext(filename)[0]

    ##edit.save(f'.{dirOut}/{clean_name}_edited.jpg')
    edit.save(f'{dirOut}/{clean_name}_edited.png')

    img.close()