Last active
November 4, 2020 21:52
-
-
Save mrmagooey/fc22e9165d62b3ab358f to your computer and use it in GitHub Desktop.
BoM radar gif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# MIT Licence | |
# Gets radar overlays from the Australian Bureau of Meteorology radar pages | |
# and creates an animated gif out of it. | |
# Needs requests and pillow (the python packages) and ImageMagick the standalone graphics | |
# program to be installed and available to work. | |
# All radar images are copyright BoM, and this script should not be used for | |
# commercial purposes, or possibly at all. | |
# Change this url variable to the radar you would like to convert to gif. | |
radar_url = "http://www.bom.gov.au/products/IDR023.loop.shtml#skip" | |
import requests, re, os, subprocess | |
from PIL import Image | |
from StringIO import StringIO | |
# create a new directory to store our data in | |
directory_prefix = 'radar_images' | |
directory_index = 0 | |
directory_name = None | |
while True: | |
directory_name = directory_prefix + str(directory_index) | |
if os.path.exists(directory_name): | |
directory_index += 1 | |
else: | |
os.mkdir(directory_name) | |
break | |
# the png images that make up the radar animation are stored in a js variable | |
# on the BoM radar page called theImageNames | |
IMAGE_URL_REGEX = re.compile(r'theImageNames\[\d\]\s+=\s+"(.*)"') | |
# get the radar page html | |
r = requests.get(radar_url) | |
# fetch the image data and save it | |
images = [] | |
for image_url in IMAGE_URL_REGEX.findall(r.text): | |
img_bytes = requests.get(image_url).content | |
i = Image.open(StringIO(img_bytes)) | |
i.save(os.path.join(directory_name, image_url[-20:])) | |
# use ImageMagick to turn the pngs into gif | |
subprocess_command = ['convert'] | |
convert_options = ['-background', | |
'white', | |
'-alpha', | |
'remove', | |
'-layers', | |
'OptimizePlus', | |
'-delay', | |
'25x100', | |
directory_name+ '/*.png', | |
'-loop', | |
'0', | |
directory_name + '/radar.gif'] | |
subprocess_command.extend(convert_options) | |
# call ImageMagicks convert function to generate a gif from the downloaded pngs | |
subprocess.call(' '.join(subprocess_command), shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment