This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
- 30 Days Of Python 👨💻 - Day 12 - Lambda Expression And Comprehensions
- 30 Days Of Python 👨💻 - Day 13 - Decorators
- 30 Days Of Python 👨💻 - Day 14 - Handling Errors
- 30 Days Of Python 👨💻 - Day 15 - Generators
- 30 Days Of Python 👨💻 - Day 16 - Module Basics
- 30 Days Of Python 👨💻 - Day 17 - External Modules
- 30 Days of Python 👨💻 - Day 18 - File I/O
- 30 Days Of Python 👨💻 - Day 19 - Regular Expressions
- 30 Days Of Python 👨💻 - Day 20 - Debugging And Testing
Today I explored the basics of scripting in Python. Scripting basically means writing programs having a collection of commands that are executed from the command line or an interactive shell to perform useful tasks and automate them. There are a ton of things that can be automated using Python scripts such as processing various types of files such as PDFs, images, excel, CSV etc, send emails, create bots like twitter bot and numerous other things. As a part of this challenge, I decided to learn the basics of scripting so that I get to understand the concepts and then can explore in greater depth in future. My focus today was to find out basic techniques of processing images and PDF files using Python scripts.
Image Processing
Image Processing in simple words is the method or technique to perform some operations on images using programs either for enhancing images or extracting information out of it. There are a lot of popular libraries to perform image processing in Python such as
- Pillow
- OpenCV
- Python Imaging Library (Deprecated)
- scikit-image
I tried out Pillow which is a forked version of the Python Imaging Library (PIL) which is no longer maintained and does not support the latest versions of Python. Thus Pillow is recommended to use over PIL.
The installation and basic usage can be found out in the Pillow docs.
Pillow can be installed using the command line using the command pip install Pillow (Check documentation for specific OS commands).
It’s time to create some image processing scripts. The first one I created is a basic image converter that converts all JPEG format images in a folder to PNG format images and stores them in another folder. I downloaded some JPEG images from https://unsplash.com and stored them in a folder images. The script needs to read all the JPEG images, convert them to PNG and then place them in a new folder generated.
Here’s the code for the script file which I named image_convertor.py Here is the GitHub repository link for the project.
Since the original images are very large in size, I first resized them into a smaller size and then converted them to improve the performance of the script.
image_converter.py
- import os
- from PIL import Image
- # fetch all the files from the source folder
- dirname = 'images'
- output_dirname = 'generated'
- images_list = os.listdir(dirname)
- # check if output folder exits otherwise create it
- if not os.path.exists(output_dirname):
- os.makedirs(output_dirname)
- for image in images_list:
- # split the filename to separate the format and name
- name, format = os.path.splitext(image)
- original = Image.open(f'{dirname}\{image}')
- # resize image to a standard size and to reduce file size
- size = 1000,1000
- # thumbnail maintains aspect ratio
- original.thumbnail(size)
- # save image as png format
- original.save(f'{output_dirname}\{name}.png')
The script can be run from the terminal as python image_converter.py. It should automatically convert the images in the generated folder.
The second script I created is a grayscale converter that converts all the images to black and white images. Using Pillow lot of filters can be applied on the images, grayscale being one of them.
grayscale_converter.py
- import os
- from PIL import Image, ImageFilter
- # fetch all the files from the source folder
- dirname = 'images'
- output_dirname = 'greyscale'
- images_list = os.listdir(dirname)
- # check if output folder exits otherwise create it
- if not os.path.exists(output_dirname):
- os.makedirs(output_dirname)
- for image in images_list:
- # split the filename to separate the format and name
- name, format = os.path.splitext(image)
- original = Image.open(f'{dirname}\{image}')
- # resize image to a standard size and to reduce file size
- size = 1000, 1000
- # thumbnail maintains aspect ratio
- original.thumbnail(size)
- # convert the image to greyscale
- grayscale_image = original.convert('L') # L mode means greyscale
- grayscale_image.save(f'{output_dirname}\{image}')

Join the conversation! Your thoughts help the community grow.