-
-
Save bdcheung/9078b25a71bf6d9ab61c to your computer and use it in GitHub Desktop.
Fetch and untile tiled Himawari-8 images from the http://himawari8.nict.go.jp PNG endpoint, then set them as desktop background on OSX
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
import requests | |
import sys | |
from datetime import datetime, timedelta | |
import pytz | |
from PIL import Image | |
from StringIO import StringIO | |
import os | |
# python himawari.py | |
# stolen from https://gist.github.com/celoyd/39c53f824daef7d363db | |
# Fetch Himawari-8 full disks at a given zoom level and set as desktop. | |
# Valid zoom levels seem to be powers of 2, 1..16, and 20. | |
# | |
# To do: | |
# - Better errors (e.g., catch the "No Image" image). | |
# - Librarify. | |
# Tile size for this dataset: | |
width = 550 | |
height = 550 | |
# time = parse(sys.argv[1]) | |
tz = pytz.timezone('UTC') | |
time = datetime.now(tz) - timedelta(minutes=20) | |
scale = 8 | |
out = '/path/to/script/desktop.png' | |
base = 'http://himawari8.nict.go.jp/img/D531106/%sd/550' % (scale) | |
tiles = [[None] * scale] * scale | |
def pathfor(t, x, y): | |
return "%s/%s/%02d/%02d/%02d%02d00_%s_%s.png" \ | |
% (base, t.year, t.month, t.day, t.hour, (t.minute / 10) * 10, x, y) | |
sess = requests.Session() # so requests will reuse the connection | |
png = Image.new('RGB', (width*scale, height*scale)) | |
for x in range(scale): | |
for y in range(scale): | |
try: | |
path = pathfor(time, x, y) | |
# print("fetching %s" % (path)) | |
tiledata = sess.get(path).content | |
tile = Image.open(StringIO(tiledata)) | |
png.paste(tile, (width*x, height*y, width*(x+1), height*(y+1))) | |
except requests.exceptions.ConnectionError as e: | |
print(e) | |
png.save(out, 'PNG') | |
os.system("osascript -e 'tell application \"Finder\" to set desktop picture to \"" + out + "\" as POSIX file'") | |
# if you have multiple monitors and want to set all of the backgrounds, replace the above with this: | |
# os.system("osascript -e 'tell application \"System Events\" to set picture of every desktop to \"" + out + "\" as POSIX file'") |
Ohh... silly me. I was running it with python3 and that's why (t.minute / 10) * 10 yielded 57 and not 50.
I couldn't get the image because they're only available for every ten minutes...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, cool stuff! But it doesn't work for me.
When I uncomment line 50 I get output like:
fetching http://himawari8.nict.go.jp/img/D531106/8d/550/2016/02/05/105700_0_3.png
However, when i visit the URL with a browser, I get "The requested URL /img/D531106/8d/550/2016/02/05/105700_0_3.png was not found on this server."
Does this still work for you? Can you access the URL?