Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fetch and untile tiled Himawari-8 images from the http://himawari8.nict.go.jp PNG endpoint, then set them as desktop background on OSX
import requests
import sys
from datetime import datetime, timedelta
import pytz
from PIL import Image
from StringIO import StringIO
import os
import logging
# 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=40)
scale = 8
out = sys.argv[1]
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))
def fetch_and_set():
for x in range(scale):
for y in range(scale):
path = pathfor(time, x, y)
tiledata = sess.get(path).content
tile = Image.open(StringIO(tiledata))
png.paste(tile, (width*x, height*y, width*(x+1), height*(y+1)))
png.save(out, 'PNG')
os.system("osascript -e 'tell application \"Finder\" to set desktop picture to POSIX file \"" + out + "\"'")
os.system("killall Dock")
try:
fetch_and_set()
except requests.exceptions.ConnectionError:
logging.exception('')
try:
fetch_and_set()
except requests.exceptions.ConnectionError:
logging.exception('')
@HadesArchitect

This comment has been minimized.

Copy link

@HadesArchitect HadesArchitect commented Feb 9, 2016

image

What exactly should be used as an argument? Filename?

Thank you!

@HadesArchitect

This comment has been minimized.

Copy link

@HadesArchitect HadesArchitect commented Feb 9, 2016

image

Image is generated, but script can't set it as a wallpaper. Any ideas? OS version is Yosemite, 10.10.4 (14E46).

@sureshg

This comment has been minimized.

Copy link

@sureshg sureshg commented Feb 11, 2016

Just ported this to Kotlin - https://github.com/sureshg/Himawari.Kt 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.