-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomradargif_STATIC.py
83 lines (69 loc) · 2.89 KB
/
bomradargif_STATIC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import sys
sys.path = ['/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
import io
import ftplib
from PIL import Image
product_id = 'IDR034' # The ID for our radar image.
frames = [] # List to store the images
# The layers that we want
#layers = ['background', 'catchments', 'topography', 'roads', 'waterways']
layers = ['roads']
# Add the locally stored map background
filename = f"/home/pi/bom-radar-gif/bomradarfiles/{product_id}.Background1.png"
base_image = Image.open(filename).convert('RGBA')
# connect to the BOM ftp server to grab the layers
ftp = ftplib.FTP('ftp.bom.gov.au')
ftp.login()
ftp.cwd('anon/gen/radar_transparencies/')
for layer in layers:
filename = f"{product_id}.{layer}.png"
file_obj = io.BytesIO()
ftp.retrbinary('RETR ' + filename, file_obj.write)
if layer == 'background':
base_image = Image.open(file_obj).convert('RGBA')
else:
image = Image.open(file_obj).convert('RGBA')
base_image.paste(image, (0,0), image)
# Access the FTP server to get the radar images
ftp = ftplib.FTP('ftp.bom.gov.au')
ftp.login()
ftp.cwd('anon/gen/radar/')
# List comprehension to filter out the images we need
# Make sure the filename starts with the radar ID, and it ends with .png
# Take the last 5 images, since it is the most recent ones
files = [file for file in ftp.nlst() \
if file.startswith(product_id) \
and file.endswith('.png')][-5:]
# Loop over the files and append the image data into our image list
for file in files:
file_obj = io.BytesIO()
try:
ftp = ftplib.FTP('ftp.bom.gov.au')
ftp.login()
ftp.cwd('anon/gen/radar/')
ftp.retrbinary('RETR ' + file, file_obj.write)
image = Image.open(file_obj).convert('RGBA')
frame = base_image.copy()
frame.paste(image, (0,0),image)
frames.append(frame)
# get locations from BOM FTP. Paste location pic on top of radar images (so the radar doesn't cover the location names)
# ftp = ftplib.FTP('ftp.bom.gov.au')
# ftp.login()
# ftp.cwd('anon/gen/radar_transparencies/')
# filename = f"{product_id}.locations.png"
# file_obj = io.BytesIO()
# ftp.retrbinary('RETR ' + filename, file_obj.write)
# image = Image.open(file_obj).convert('RGBA')
# use local stored image for locations (must be transparent). This will paste locations on top of radar images
filename = f"/home/pi/bom-radar-gif/bomradarfiles/{product_id}.locations1.png"
image = Image.open(filename).convert('RGBA')
frame.paste(image, (0,0), image)
frames.append(frame)
except ftplib.all_errors:
pass
ftp.quit()
# Store the result as a GIF file in web accessible folder
frames[0].save('/var/www/html/radar_images/radar.gif', format='GIF', save_all=True, append_images=frames[1:]+[frames[-1],frames[-1]], duration=400, loop=0)
#used for debugging to see how many pics have been appended.
#print(frames)