Skip to content

Instantly share code, notes, and snippets.

@izawa
Created August 20, 2018 01:49
Show Gist options
  • Save izawa/ab615a06ee0bb036fe2b51b3d66f8a06 to your computer and use it in GitHub Desktop.
Save izawa/ab615a06ee0bb036fe2b51b3d66f8a06 to your computer and use it in GitHub Desktop.
xband radar image (Ishikawa Pref.) generator for iTerm2
from datetime import datetime
from datetime import timedelta
import iterm2_tools
import urllib.request
import io
from PIL import Image
###################
## http utility function
def getbody(url):
req = urllib.request.Request(url)
try:
with urllib.request.urlopen(req) as res:
body = res.read()
except urllib.error.HTTPError as err:
print(err.code)
except urllib.error.URLError as err:
print(err.reason)
return(body)
###################
## build white map
def get_basemap():
basemap_urls = [
[ "http://www.river.go.jp/x/map/pale/10/899/397.png",
"http://www.river.go.jp/x/map/pale/10/900/397.png",
"http://www.river.go.jp/x/map/pale/10/901/397.png"],
[ "http://www.river.go.jp/x/map/pale/10/899/398.png",
"http://www.river.go.jp/x/map/pale/10/900/398.png",
"http://www.river.go.jp/x/map/pale/10/901/398.png"],
[ "http://www.river.go.jp/x/map/pale/10/899/399.png",
"http://www.river.go.jp/x/map/pale/10/900/399.png",
"http://www.river.go.jp/x/map/pale/10/901/399.png"],
[ "http://www.river.go.jp/x/map/pale/10/899/400.png",
"http://www.river.go.jp/x/map/pale/10/900/400.png",
"http://www.river.go.jp/x/map/pale/10/901/400.png"],
[ "http://www.river.go.jp/x/map/pale/10/899/401.png",
"http://www.river.go.jp/x/map/pale/10/900/401.png",
"http://www.river.go.jp/x/map/pale/10/901/401.png"]
]
basemaps = [[Image.open(io.BytesIO(getbody(url))) for url in urls] for urls in basemap_urls]
basemap = Image.new('RGBA', (basemaps[0][0].width *3, basemaps[0][0].height * 5),(255,255,255,255))
for i in range(5):
for j in range(3):
basemap.paste(basemaps[i][j], (j*basemaps[i][j].width, i*basemaps[i][j].height))
return(basemap)
###################
## build xband mp radar map (3 minutes ago)
def get_xbandmap():
now = datetime.now() - timedelta(minutes = 3)
d = "{:4d}{:02d}{:02d}".format(now.year, now.month, now.day)
t = "{:02d}{:02d}00".format(now.hour, now.minute)
x01 = Image.open(io.BytesIO(getbody("http://www.river.go.jp/x/rd/39/82/{}/{}/01.png".format(d,t))))
x02 = Image.open(io.BytesIO(getbody("http://www.river.go.jp/x/rd/39/82/{}/{}/02.png".format(d,t))))
x03 = Image.open(io.BytesIO(getbody("http://www.river.go.jp/x/rd/39/82/{}/{}/03.png".format(d,t))))
x17 = Image.open(io.BytesIO(getbody("http://www.river.go.jp/x/rd/39/83/{}/{}/17.png".format(d,t))))
xbandmap = Image.new('RGBA', (x02.width,x03.height + x02.height + x01.height),(255,255,255,0))
xbandmap.paste(x02,(0, x03.height))
xbandmap.paste(x01,(0, x02.height + x03.height))
xbandmap.paste(x17,(x01.width, x02.height + x03.height))
xbandmap.paste(x03,(0,0))
return(xbandmap)
###################
## main
if __name__ == '__main__':
basemap = get_basemap()
xbandmap = get_xbandmap()
xbandimage = xbandmap.resize((int(xbandmap.width*2.2), int(xbandmap.height*1.8)))
mask = xbandimage.split()[3]
xbandimage_alpha = xbandimage.copy()
xbandimage_alpha.putalpha(128)
blank = Image.new('RGBA',xbandimage.size)
out = Image.composite(xbandimage_alpha, blank, mask)
resized_xbandmap = Image.new('RGBA', (basemap.width,basemap.height),(255,255,255,0))
resized_xbandmap.paste(out, (-750,-450))
resized_xbandmap_mask = resized_xbandmap.split()[3]
basemap.paste(resized_xbandmap, (0,0), resized_xbandmap_mask)
mod = io.BytesIO()
basemap.save(mod, format='PNG')
print(iterm2_tools.images.display_image_bytes(mod.getvalue()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment