How to create a gauge / speedometer graphic with Python

written by Andrew Shay on 2018-06-30

This article discusses how to easily create a speedometer / gauge image in Python with only the Pillow library.

View source code and images on GitHub: python-gauge

I needed to create a gauge graphic in Python but I found many libraries did not have the graphic I needed or required that the gauge be rendered in a browser since the underlying library used was JavaScript. This script only requires the Pillow library since it is generated using two png images.

The gauge graphic is separated into two images. The first image is the needle that points to a number on the gauge. The second image is the gauge without the needle. The final gauge image is generated by rotating the needle image then pasting it on the gauge.

The script is simple. The gauge is half a circle therefore the needle needs to rotate between 0 and 180 degrees. The gauge goes from 0 to 100. So if you want the gauge to point to 20, the script just takes 20% of 180 degrees to rotate the needle.

Copyright (C) 2018 FireEye, Inc., created by Andrew Shay. All Rights Reserved.

import PIL

from PIL import Image

percent = 20  # Percent for gauge
output_file_name = 'new_gauge.png'

# X and Y coordinates of the center bottom of the needle starting from the top left corner
#   of the image
x = 825
y = 825
loc = (x, y)

percent = percent / 100
rotation = 180 * percent  # 180 degrees because the gauge is half a circle
rotation = 90 - rotation  # Factor in the needle graphic pointing to 50 (90 degrees)

dial ='needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc)  # Rotate needle

gauge ='gauge.png')
gauge.paste(dial, mask=dial)  # Paste needle onto gauge

To modify the gauge image, modify gauge.psd. gauge.png should be the gauge without the needle (hide the needle layer). needle.png should only be the needle on a transparent background and should point at 50 (hide all layers except needle).

How to create a lektor plugin and debug it in PyCharm

written by Andrew Shay on 2018-05-16

I recently created by my first Lektor Plugin, read-full-post, and wanted to create a getting starting guide for creating a plugin and debugging it in PyCharm.

In this article we will pull the lektor project, create a plugin that modifies the body of every blog post, debug the plugin in PyCharm and look at some of the data in the debugger.

Read Full Post

lektor plugin: read full post

written by Andrew Shay on 2018-05-11

I have created a lektor plugin, read full post.
It allows blog listing posts to be shortened with a link to the full post.

Neuron v2 released

written by Andrew Shay on 2018-04-14

Neuron v2 has been released. It contains the following changes

  • Updated to Python 3
  • Uses wxPython
  • Includes script to generate exe and installer (via PyInstaller and NSIS)

Device Checker: New project for detecting changes on Windows

written by Andrew Shay on 2018-03-24

I recently wanted software to notify me when services, installed programs, and startup programs were added, removed, or changed.
I found some wmic commands to give me that information, as well as much more information.

I created a program to simply gather information about a Windows system and then notify the user when it has changed, and view the differences.

It is currently released in beta.

View on GitHub: shayConcepts/device-checker