Recently, my child wanted to make a coloring for a character from his favorite cartoon. The obvious solution was to use some kind of graphical editor for Linux (because I'm 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 tutorial ). Then you need to install and open JupyterLab and Pillow - a 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 images from Creative Commons or other open source licenses so you don't bother with copyright. In this example, I used an open source 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 = Image.open("deer.jpg")
pic.size
(3561, 5342)
This is perhaps a bit too much for our task. These high-resolution images are good for books, not coloring pages. Reduce the image size:
x, y = pic.size
x //= 10
y //= 10
smaller = pic.resize((x, y))
The size has become 10 times smaller.
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)
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()
bands[0]
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)
outline
Now let's pay attention that there is a lot of empty space in the picture, so we will crop it:
outline.crop((10, 200, 300, 400))
Well, that's all. Let's save the picture in PDF format (for printing):
outline.save("deer.pdf")
This is how you can easily make your own coloring pages for your children: minimum routine, maximum programming!
Advertising
Epic Servers are Linux or Windows VPSs with powerful AMD EPYC processors and very fast Intel NVMe drives. Disperse like hot cakes!