Wallpapers With Python

Najeem Muhammed
The Startup
Published in
6 min readJul 30, 2020

Creating geometric wallpapers using python and Pillow library

I used python for a lot of tasks, both at work and for fun. Here’s a project which is quite fun and a good one for anyone who’s a beginner in python; creating wallpapers for your phone, tab or laptop. You can as well use them as background images for web pages.

Intended Audience

Anyone interested. You should be comfortable in issuing commands on a command line as well as write and execute code in a script file. Some basic experience in python and some math will be helpful.

Tools required

You should have python3 installed in your computer. You should also have access to a terminal emulator where you’ll write the commands to create a virtual environment, install the dependencies etc.

So let’s start.

Create a virtual env and install Pillow

I always recommend using virtual environments when you’re installing python libraries, as this will make sure the dependencies are not getting messed up. To create a virtual env, open up a terminal and type the following commands.

mkdir python_wallpapers
cd python_wallpapers
python3 -m venv .venv
source .venv/bin/activate

If you’re on windows swap out the last line and type in

.venv/bin/activate.bat

Click here if you want to know more details of creating a virtual environment.

Now let’s install the Pillow library.

pip install --upgrade Pillow

Let’s draw a circle

The Pillow library is a fork of Python Imaging Library (or PIL). It’s created for batch processing images using python. We will use it to draw shapes on a blank background.

Open up a text editor and type in the following basic script.

There are sub-modules in Pillow named Image and ImageDraw. The Image module helps in opening existing images or creating new images. In this script, we are creating a blank new image of size 500px by 500px.

The ImageDraw module helps us create simple 2d graphics. We need a Draw objet to draw onto an Image. So we used the Draw function and passed in our img image to it.

The next step is to draw a circle. For this, we used the ellipse function of the Draw object. (Since circle is a type of ellipse). We passed in the the coordinates of the top left and bottom right coordinates of the extent of our circle.

Finally we save the image as basic.png. To execute this script, first save the script file and execute it as

python3 pilwall_basic.py

This will create a new PNG file in your folder with the name basic.png and looks something like.

Hurray! but……

Yes, the edges dont look so nice. That’s because Pillow cannot draw with anti-aliasing. But there is something it can do with anti-aliasing. That’s resizing an image. So that’s what we’ll do.

Let’s modify our basic script slightly.

I have made some minor modifications so that the initial image is 4 times what we desire as our final image size, draw the circle and then use the resize function of img with a resample flag set to Image.ANTIALIAS. Let’s see how the new image look like.

Ok, that looks much better.

Based on my experience, any resizing scale larger than 4 works quite well.

Let’s test couple more interesting aspects before we move on to creating stunning wallpapers. That’s colors and opacity!

Colors and Opacity

As you have already seen in the basic script, I had specified the circle to have a fill color of RED. The ellipse function will take colors in tuples (of RGB) or hex colors as well. Let’s see an example where I use random colors for multiple concentric circles.

I have created a convenience function to easily draw a circle with a specified center, radius and color.

As you can see, I have imported randint function from the random module and used it to create a random RGB tuple. Let’s see how colorful my image is.

Ok, let’s agree that it doesn’t look so spectacular. But I guess the example was enough to convey the idea. Now let’s look at opacity.

Opacity requires a minor tweak to our Draw object. Instead of using the default RGB(which it gets from the mode of img), we will use RGBA. Also, while passing in the tuple for colors, we will pass an extra value which will denote the opacity. Here’s an example.

It should create concentric circles with increasing opacity and decreasing radius. I had create it such that it increases towards the center or else, the final large, fully opaque circle will cover all the other partially opaque circles.

That looks pretty cool! So I guess we’re ready to create some cool wallpapers.

A simple wallpaper

I’ll use the above code itself to create a very simple wallpaper for my laptop. I’ll modify the size of the canvas, move the center and draw a lot more circles with increasing opacity.

So here, I created a function to create the concentric circles and iterated through a list of centers. Here’s the result.

Looks good, but not so impressive. Let’s get some better colors.

Better colors

For colors, I always refer to colorlovers. It’s an awesome resource for colors, pallets, patterns etc. So we’ll use some pallet from there. Interestingly, they also have a web API! So let’s do things the pythonic way. That is; get our palletes using python (and not copy paste).

For getting data from an API, we will need the requests module. So let’s install that in our venv. In your terminal (which already has the venv activated) type

pip install requests

Now we can use request module to get the top pallets. Since the output of the web API is xml, we’ll have to parse that too. So the following function will query the API and get a particular top palette.

Now we’ll use our newly obtained awesome pallet to create a little more impressive image. I also did some refactoring of my original code and moved the draw_circle and concentric_circles function into a separate python file so that I can import them into my script.

Getting Better

Now to make things better, we will split our canvas into smaller tiles and draw in steps. Since I have chosen full HD resolution, it has an aspect ratio of 16x9. So I will split the whole canvas in to 16x9 tiles with each tile having a size of 120px by 120px. Remember that we have to scale this size by the scale_factor when using distances in our wallpaper.

Here’s a try with a pallet from colorlovers.

I have added couple of extra circles at the right and bottom so that we don’t leave any blank space at the edges. And here’s the image.

Not too bad. If your colors are different play around with the pallet that you picked.

We can make it a lot more interesting by playing around with the position of the circle center, draw lines/hexagons instead of circles, add outline to the circles etc. Here are some interesting ones which I created.

This is my favorite

If you play around a bit with math and some other python libraries, you can create interesting patterns like below.

I leave it for you to find out how I would have achieved this!

I have a github repository for the examples I shared (not all of them).

Have fun!

--

--