# 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.

`python_gauge.py`

```"""
"""

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 = Image.open('needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc)  # Rotate needle

gauge = Image.open('gauge.png')
gauge.paste(dial, mask=dial)  # Paste needle onto gauge
gauge.save(output_file_name)
```

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).

### Tags

python   gauge   speedometer   graphic   image   article