Using Golang For Custom Wallpapers

If you’re like me, then you like to constantly have new wallpapers. I often even have scripts to automatically switch my wallpapers. So naturally, I decided that I wanted to always have custom wallpapers with my name on them. And, what better way to achieve this than with Golang’s draw libraries? So here were my project goals:
1. Allow for custom text and icons.
2. Allow for easy scripting.
3. Allow different image sizes

The code for this post is available on my Github page. I highly recommend the images from unsplash for your wallpaper needs. (That is where I downloaded the wallpapers for this post!) Now, before we get started,

Here’s our mask.
image mask

and here’s a sample output.
Sample Image
The go app takes the input image, flips it, inverts the colors, then draws this new image on the original in the area allowed by our mask. Let’s start by writing the code to read in our source images.

Here, we are using our command line arguments as file paths and passing them to our “readImage” function. This function simply reads in the file data and converts it to an Image.

Next, we need to scale our mask so that it will fit when our wallpaper is larger or smaller. We will use our “dst” image’s size.

Now we can actually transform our image. I decided to move these image functions to a different go package. Let’s start by flipping our “dst” image.

This function simply switches the pixels horizontally and vertically.

Next, let’s invert the colors in our image.

Here we make a struct to represent a pixel’s colors. We then simply make a new image and set each pixel to the corresponding pixel from our input with inverse RGB values.

Now that our image functions are done, we can continue. One small thing we need to take care of is converting our “dst” object from an “image.Image” to an “image.RGBA”. We do this by creating a new RGBA object and drawing our image.

Finally we can actually draw our image.

We pass our converted dst image and it’s bounds, our modified image, the image zero point, our mask, the image zero point (again), and finally the “Over” operation. This call does the actual work of drawing our modified image over our original through the mask we provided.

All that’s left to do is to save our final image.

And we should now have a fancy wallpaper to use. Here are some more sample outputs.

bridge

bridge2

hill

jump

trees

I hope this post was a good introduction into Golang’s image and draw libraries. I went over just a couple of possible transforms one could apply to an image. A challenge to the reader is to add different image manipulations to create even cooler output images. Also, remember that the code for this post can be found on my Github page.

Thanks for reading,

John