下载Himawari8拍摄的实时地球图片

Posted in 2017-3-22 22:10 | Category: Python | Tags: earth himawari8 python

设置壁纸

feh --bg-scale /tmp/earth.png

设置桌面大小,及图片放大倍数

SCALE = 4
WIDTH = 1368
HEIGHT = 768

直接给出源码

from PIL import Image
from io import BytesIO
from urllib.request import Request, urlopen
from datetime import datetime
import json

SCALE = 4
WIDTH = 1368
HEIGHT = 768


def get_info():
    url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json"
    request = Request(url)
    response = urlopen(request, timeout=10)
    return json.loads(response.read())


def download():
    png = Image.new('RGB', (550 * SCALE, 550 * SCALE))
    desktop = Image.new('RGB', (WIDTH, HEIGHT))
    url_format = 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/{}d/{}/{}_{}_{}.png'
    info = get_info()
    date = datetime.strptime(info['date'], '%Y-%m-%d %H:%M:%S')
    for x in range(SCALE):
        for y in range(SCALE):
            url = url_format.format(SCALE, 550,
                                    date.strftime("%Y/%m/%d/%H%M%S"), x, y)
            print(url)
            request = Request(url)
            response = urlopen(request, timeout=10)
            img = Image.open(BytesIO(response.read()))
            png.paste(img, (550 * x, 550 * y, 550 * (x + 1), 550 * (y + 1)))
    png.thumbnail((HEIGHT, HEIGHT), Image.ANTIALIAS)
    desktop.paste(png, ((WIDTH - HEIGHT) // 2, 0))
    desktop.save('/tmp/earth.png', "PNG")

if __name__ == '__main__':
    download()

或许应该找一张有星星的照片 img

img