-
-
Save blahgeek/29418ce011fd25cbc7339e25393e514f 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 os | |
import requests | |
from datetime import datetime, timedelta | |
from PIL import Image | |
from io import BytesIO | |
import logging | |
import argparse | |
TILE_SIZE = 550 | |
def fetch_image(time, scale, paddings): | |
def pathfor(t, x, y): | |
return "http://himawari8.nict.go.jp/img/D531106/%sd/550/%s/%02d/%02d/%02d%02d00_%s_%s.png" \ | |
% (scale, t.year, t.month, t.day, t.hour, (t.minute // 10) * 10, x, y) | |
sess = requests.Session() | |
png = Image.new('RGB', (TILE_SIZE*scale+paddings[1]+paddings[3], TILE_SIZE*scale+paddings[0]+paddings[2])) | |
for x in range(scale): | |
for y in range(scale): | |
logging.info('Fetching tile {}-{}'.format(x, y)) | |
path = pathfor(time, x, y) | |
tiledata = sess.get(path).content | |
tile = Image.open(BytesIO(tiledata)) | |
png.paste(tile, (TILE_SIZE*x+paddings[3], TILE_SIZE*y+paddings[0], | |
TILE_SIZE*(x+1)+paddings[3], TILE_SIZE*(y+1)+paddings[0])) | |
return png | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--ago', type=int, default=40, help='Fetch images from how many minutes ago') | |
parser.add_argument('--scale', type=int, default=8, help='Image scale, from 1 to 16 (powers of 2), and 20') | |
parser.add_argument('--padding', default='100,820,100,820', help='Padding') | |
parser.add_argument('-v', action='store_true', help='Verbose') | |
parser.add_argument('output', help='Output dir') | |
args = parser.parse_args() | |
if args.v: | |
logging.basicConfig(level=logging.INFO) | |
time = datetime.utcnow() - timedelta(minutes=args.ago) | |
image = fetch_image(time, args.scale, list(map(int, args.padding.split(',')))) | |
image.save(os.path.join(args.output, time.strftime('%Y%m%d-%H%M') + '.png')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment