Skip to content

Instantly share code, notes, and snippets.

@mumrah
Created February 5, 2016 00:45
Show Gist options
  • Save mumrah/3e369d05a513bcf342db to your computer and use it in GitHub Desktop.
Save mumrah/3e369d05a513bcf342db to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from __future__ import print_function
from json import loads
from io import BytesIO
from urllib import urlopen
from time import strptime, strftime
import os
from appscript import app, mactypes
from PIL import Image
# Configuration
# =============
# Increases the quality and the size. Possible values: 4, 8, 16, 20
level = 4
# ==============================================================================
def main():
width = 550
height = 550
ratio = 0.375 # 9/16
total_h = int(height*level)
total_w = int(total_h / ratio)
x_shift = int((total_w - width*level) / 2.0)
print("Updating...")
latest_json = urlopen("http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json")
latest = strptime(loads(latest_json.read().decode("utf-8"))["date"], "%Y-%m-%d %H:%M:%S")
print("Latest version: {} GMT\n".format(strftime("%Y/%m/%d/%H:%M:%S", latest)))
url_format = "http://himawari8.nict.go.jp/img/D531106/{}d/{}/{}_{}_{}.png"
png = Image.new('RGB', size=(total_w, total_h), color=(0,0,0))
print("Downloading tiles: 0/{} completed".format(level*level))
for x in range(level):
for y in range(level):
print(url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y))
tile_w = urlopen(url_format.format(level, width, strftime("%Y/%m/%d/%H%M%S", latest), x, y))
tiledata = tile_w.read()
tile = Image.open(BytesIO(tiledata))
png.paste(tile, (x_shift + width*x, height*y, x_shift + width*(x+1), height*(y+1)))
print("Downloading tiles: {}/{} completed".format(x*level + y + 1, level*level), end="\r")
print("\nDownloaded\n")
png.save("himawari-latest.png", "PNG")
se = app('System Events')
for desktop in se.desktops.get():
desktop.picture.set(os.path.abspath("himawari-latest.png"))
print("Done!\n")
if __name__ == "__main__":
main()
@feifanzhou
Copy link

For python3, you'll want from urllib.request import urlopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment