Python: Building an Image Manipulation Tool (Part 1)

A tutorial and case study on developing an image processing application with Python

Introduction

Source Files

$ git clone https://github.com/danebulat/py-image-manip-demo.git

1. Project Overview

> py-image-manip-demo/
--> images/
--> 001.jpg
--> 002.jpg
--> 003.jpg
--> imgmanip/
--> imgmanip.py
--> parser.py
--> handler.py
--> manipulator.py
--> Pipfile
--> Pipfile.lock

Setting Up a Virtual Environment

[packages]
pillow = "*"
[requires]
python_version = "3.9"
"pillow": {
"hashes": [
...
],
"index": "pypi",
"version": "==8.1.0"
}
$ pip install pipenv
$ pipenv --version
-| pipenv, version 2020.11.15
$ cd py-image-manip-demo
$ pipenv install
$ pipenv graph
-| Pillow==8.1.0
$ pipenv run which python pip
-| /home/dane/.local/share/virtualenvs/py-image-manip-demo-
lnYNAppa/bin/python
/home/dane/.local/share/virtualenvs/py-image-manip-demo-
lnYNAppa/bin/pip

$ pipenv run ls -l $(which python pip)
$ pipenv shell

# From here you can work inside the virtual environment
$ python --version && which python && pip --version
$ pip list && pip show pillow
$ exit
$ pipenv --help
pipenv install <package_name>
$ pipenv lock

2. How to Use the Image Manipulation Tool

Invocation

$ pipenv shell
$ which python
-| /Users/dane/.local/share/virtualenvs/py-image-manip-demo-
e67HmAh0/bin/python
>> #!/Users/dane/.local/share/virtualenvs/py-image-manip-demo-
e67HmAh0/bin/python

"""Module for performing a range of image manipulations.
python imgmanip.py <image_files> <flags>
$ chmod u+x imgmanip.py
./imgmanip.py <image_files> <flags>
$ ln -s ../images/001.jpg 001.jpg
$ ln -s ../images/002.jpg 002.jpg
$ ln -s ../images/003.jpg 003.jpg
# One input file
$ ./imgmanip.py 001.jpg -i
-| Filename : 001.jpg
Size : (2560, 1440)
Format : JPEG
Bands : ('R', 'G', 'B')

# Multiple input files
$ ./imgmanip.py 001.jpg 002.jpg 003.jpg -i

# Multiple input files with wildcard character
$ ./imgmanip.py *.jpg -i

Positional Arguments and Optional Arguments

The program’s optional arguments.
Example usage of the program’s optional arguments.
Filters available to the image manipulation tool.
Enhancers available to the image manipulation tool.
$ ./imgmanip.py 001.jpg -f detAIL BLUR SmooTH -e color GREYSCALE    -| DETAIL filter applied to 001.jpg
BLUR filter applied to 001.jpg
SMOOTH filter applied to 001.jpg
COLOR enhancer applied to 001.jpg [factor = 1.2]
GREYSCALE enhancer applied to 001.jpg
New file saved as: 001-manip-0.jpg
./imgmanip.py 001.jpg -f DETAIL banana SMOOTH \
-e BRIGHTNESS apple --factor 1.5
-| DETAIL filter applied to 001.jpg
'BANANA' not a valid filter name. Skipping...
SMOOTH filter applied to 001.jpg
BRIGHTNESS enhancer applied to 001.jpg [factor = 1.5]
'APPLE' not a valid enhancer name. Skipping...
New file saved as: 001-manip-0.jpg

3. Example Commands with Screenshots

$ ./imgmanip.py --help
$ ./imgmanip.py 001.jpg -e greyscale brightness --factor 1.25   -| GREYSCALE enhancer applied to 001.jpg
BRIGHTNESS enhancer applied to 001.jpg [factor = 1.25]
New file saved as: 001-manip-0.jpg
Converting an image into a brightened greyscale version.
$ ./imgmanip.py 002.jpg -f contour detail detail smooth -e greyscale    -| CONTOUR filter applied to 002.jpg
DETAIL filter applied to 002.jpg
DETAIL filter applied to 002.jpg
SMOOTH filter applied to 002.jpg
GREYSCALE enhancer applied to 002.jpg
Combining the contour filter with other effects.
$ ./imgmanip.py 002.jpg --resize-width-proportional 512    -| 002.jpg resized to: (512, 320)
New file saved as: 002-manip-0.jpg
Resizing an image proportionally.
$ ./imgmanip.py 001.jpg --flip horz --rotate 180    -| 003.jpg flipped vertically.
003.jpg flipped horizontally.
003.jpg rotated 180 degrees CCW.
New file saved as: 003-manip-0.jpg
Flipping an image horizontally and rotating it 180 degrees CCW.
$ ./imgmanip.py 001.jpg 002.jpg 003.jpg \
--thumbnail --thumbnail-width 256
-| Thumbnail of 001.jpg generated: 001-256x144-0.thumb
Thumbnail of 002.jpg generated: 002-256x160-1.thumb
Thumbnail of 003.jpg generated: 003-256x173-2.thumb
Generating a thumbnail for three image files.

In Conclusion — Part 1

MSc. Programmer and fan of open source software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store