Working With Images In Python

working with images in python image


For some good reasons we sometimes happen to be in need of working with images in python related projects. It could be for illustration, making contents less boring or even branding.

However, one might just say why using python instead of using one of the numerous phone apps in the appstore/playstore. Truth’s automation is the new proactive in tech world.

Apart from automating these image manipulations, you get to control whatever comes in from users end and automate the end result or looks on your front-end, without having to worry about specifying the required image specification.


For this particular working with images in python tutorial, am assuming you already know some python basics like: Loops, Importing of modules and similar stuffs. If that’s the case then this should be fun for you.


In this short tutorial we are going to use just two modules: Pillow (PIL) and os. Do yourself some good by checking them out to see their documentation. Learn more about their features and what you can do with them.

Pillow will be used for image manipulation while os will be used for directories and files manipulation.

Let’s start by importing the modules:

#let's import the necessary modules
from PIL import Image, ImageFilter
import os

Let’s create a variable for the image path which we’ll use.

#let's import the necessary modules
from PIL import Image, ImageFilter
import os

file_path, path = os.listdir('/home/you/Documents/image_folder/'), ('/home/you/Documents/image_folder/')

With code above we assign our file path to a variable called file_path, and also assigned the path as a string to path variable which we will use in the opening of multiple img files. Your own path will probably be different but just make sure you got the path right. We had to print the path and it’s type to be sure of what we are dealing with.

Now we’ll create a variable called namer, don’t worry about it’s use for now, you’ll realize shortly. Then we w’ll also create a for loop to edit multiple images.

#let's import the necessary modules
from PIL import Image, ImageFilter
import os

file_path, path = os.listdir('/home/you/Documents/image_folder/'), ('/home/you/Documents/image_folder/')

namer = 1

for im in file_path:
    f = + im)
    new_file = f.resize((1200, 800)).filter(ImageFilter.CONTOUR).filter(ImageFilter.MinFilter(size = 3))'{im}{namer}.png')

As you can see on the code above we created a for loop to access all the images in the file path and make a chained filters on them. The .resize is used to resize images, it takes two arguments, width and height. The .filter is used to add filters in the image. Be sure to checkout more filters in the documentation.

Remember researching more about modules is a necessary part of becoming a better programmer.

The namer variable was used to add 1 to the names of the edited images to differentiate them from the original images. Here we saved all the edited images as png files, you can choose to save yours with your preferred image extension. The files will be saved on the directory where you are running the program.

If everything works fine on your end there’ll be no error neither will there be a return value. Just check your the directory where the program file is and you’ll see the edited images. You can add: print(‘done’) at the end of the program to notify you when it’s done.

Feel free to tweak things up and try new and better stuffs than mine.

Hope you had fun with this tutorial, as you can see, working with images in python could be fun. However, If you happen to run into any puzzle or challenge on this tutorial, please don’t hesitate to write me, or leave a comment at comment section.

To automatically get the first hand information when the next post and subsequent ones gets published, subscribe to techswitch newsletter or join us on telegram.

Get an affordable and seamless python one on one training today from anywhere in the world, location is never a barrier, we have friendly learning tools to make your python programming training a worthwhile experience.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy


Geoff is a software engineer, web content specialist, tech private trainer and an IT virtual assistant.

Leave a Reply