Automating image processing with Jupyter and Python

You probably won’t want to deal with image processing in graphic editors if you know how to do it using open source libraries for Python.

Recently, my child wanted to make a coloring for a character from his favorite cartoon. The obvious solution was to use some kind of graphics editor for Linux (because I am a Linux user), but then I remembered that I am a lazy person 🙂

Fortunately, I am familiar with Python and JupyterLab. Let’s see how Jupyter makes the task easier.

First of all, we will of course need a modern version of Python (if you are a macOS user you can follow this leadership). Then you need to install and open JupyterLab and Pillow – fork of the Python Imaging Library (PIL):

$ python -V

Python 3.8.5

$ pip install jupyterlab pillow


$ jupyter lab

In this case, we want to paint with the image of a deer. First, let’s download the image and save it locally. It is best to use pictures with Creative Commons or another open source license so you don’t bother with copyright. In this example, I used an open license image from Unsplash and named it deer.jpg.

Let’s start by importing:

from PIL import Image

Let’s open the image and check its size:

pic ="deer.jpg")
(3561, 5342)

This is perhaps a bit too much for our task. These high-resolution images are good for books, not coloring pages. Reduce image size:

x, y = pic.size

x //= 10

y //= 10

smaller = pic.resize((x, y))

The size has become 10 times smaller.


Perfectly. Moving on.

This deer should be easy prey for the boundary detection algorithm. We need to clean up the image to make coloring easy. And, fortunately, there is a ready-made algorithm for this too:

from PIL import ImageFilter
edges = smaller.filter(ImageFilter.FIND_EDGES)


This is probably the most important step. We will remove all extraneous details and leave clear lines. The color is a little strange, but this problem is not difficult to solve. Divide the image into color stripes and select the one with the clearest lines:

bands = edges.split()

Now the lines are clear, but the palette itself is not yet suitable for printing, because your printer will run out of ink, and your child will not like to paint a picture on a black background. So we invert the colors. Set the thresholds for x to max-black and max-white to make the lines even more distinct:

outline = bands[0].point(lambda x: 255 if x<100 else 0)

Now let's pay attention that there is a lot of empty space left in the picture, so let's crop it:

outline.crop((10, 200, 300, 400))

Well, that's all. Let's save the picture in PDF format (for printing):"deer.pdf")

This is how you can easily make your own coloring pages for your children: minimum routine, maximum programming!


Epic servers - this is VPS on Linux or Windows with powerful AMD EPYC processors and very fast Intel NVMe drives. Disperse like hot cakes!

Similar Posts

Leave a Reply