Make an Image Editor with Python

Ian Salazar Jara
4 min readJan 11, 2023

Maybe you are wonderer “why I would code and image editor when I could use Photoshop, Gimp or any other tool…” Well, if you are trying to expand your knowledge; Practice with python, discover new libraries, possibilities, or of course trying to do your own Image Editor that will fullfil your requirements, with this article you’ll have the bases to start to do it, still you’ll have to know some basics of programming.

Developing your own image editor has some advantages:

Photos or pictures that you have maybe are always being edited with the same “settings”, can be very tedius to do every time the same task, even if you have an application on your smartphone, or doing it from a website app, probably you’ll can’t apply the same edit to a 100 photos at same time.

Also the apps mentioned above have some limitations; Time response(If web), low quality of the image, file size max, filters/settings, etc… And of course you’ll have to pay a suscription to unlock all those shiny, fancy “filters” and “vip pressets”. And maybe you only wanted to use one of those filter, so it wouldn’t be worth it, whatever the case is…

Let’s get in to it!

Necessary requirements:

  • Python 3.10+ (It’s not mandatory but recommended)
  • pip (Python Package)
  • Pillow (Python Library)

Starting a new project is always a puzzle. You never know how to organize your files. However, once you have a proper understanding of the best practice out there, it’s pretty simple. Although in this project will be a simple folder structure.

— ProjectName
|— venv (Virtual enviroment)
|— src (Sources folder)
 — |— edited_images
 — |— raw_images
| — photo_editor.py (main file)

You can also add a README.md where you can add information to the users about the project.

It’s important to know that when you make a “folder” it can be a module for python if you add a __init__.py file, in this case is not necessary, except on the src folder.

Virtual Enviroment

To create a virtual enviroment go to the project folder with the terminal:
- cd pathToFolder

Then create the venv with the following command…

  • For Windows:
    - python -m venv venv (the second venv its the name of the folder with the virtual enviroment, you can change to whatever you want, for me best practice its to have it just like that.)
    - To activate the venv: venv/Scripts/activate
  • For Linux:
    - python3 -m venv venv
    - To activate the venv: source venv/bin/activate
  • To deactivate the venv in both (windows & linux) type:
    - deactivate or venv/Scripts/deactivate on windows.

Starting the Magic

Now that we have the venv ready, let’s go to the main file photo_editor.py .

First we need to install Pillow which will be the main library for this tutorial.

  • On windows pip install pillow or pip3 install pillow on linux.

Now lets add the necessary imports:

# PIL is the Python Imaging Library
from importlib.resources import path
from PIL import Image, ImageEnhance, ImageFilter

# For File Management
import os

We need to define the paths for the raw images and the modified.

path = './src/raw_images/'
pathOut = './src/edited_images/'

Add a image or images to the raw_images folder that you want to be edited.

Then add the following code below. You can change it to your liking to add more features or whatever you want!

You can find a lot more functionalities in the official documentation of Pillow:
> Reference — Pillow (PIL Fork) 9.4.0 documentation

for filename in os.listdir(path):
# will open the file based on the paths given before.
img = Image.open(f"{path}/{filename}")
# this is the filter that is being used.
edit = img.filter(ImageFilter.SHARPEN) # You can play with the edit.
# for example with .rotate(-90) the rotation is used to make the image upright

# this is the factor that is being used to increase the brightness of the image.
factor = 1.5
# can be contrasted, brightness, or sharpness.
enhancer = ImageEnhance.Contrast(edit)
# this is the image that is being enhanced.
edit = enhancer.enhance(factor)

# this is the name of the image without the extension which is .jpg and is split into an array.
clean_name = os.path.splitext(filename)[0]
# this is the name of the image that is being saved with the edited name.
edit.save(f"{pathOut}/{clean_name}_edited.jpg")

Here is the explanation for the function code above:
1. The loop starts by importing the Image and ImageEnhance modules.
2. Then goes through all the files in the directory and opens them.
3. Then filters the image and rotates it to make it upright.
4. Then enhances the image and increases the brightness.
5. Finally saves the image.

Now you can go to the edited_images folder a see your new modified image!

Conclusions

I hope you find this little guide useful, it gave you the bases to make a much bigger python editor and fulfill your needs.

Give it some claps 👏to make others find it too! Make sure to follow me on medium to not miss anything and see the new post that will come!

--

--

Ian Salazar Jara
0 Followers

Enthusiastic. I like to learn good practices and useful knowledge to share it with the world!