himawari8图片下载改进版


第一版下载图片后设为壁纸,四周都是黑黑的不好看, 所以结合本地壁纸将两张图片进行合成

只要计算一下要缩减的大小与要放置的位置

我的方法是:打开 gimp,合成两张图片,记下缩减的大小(214,214)与位置(160,160)

每次只要运行一下

python himawari8.py

源码

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

SCALE = 2
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))
    desktop = Image.open('/home/jianglin/Pictures/308556.png')
    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') + timedelta(
    #     hours=-8)
    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 = circle(png)
    desktop.paste(png, (160, 160), png)
    desktop.save('/tmp/earth.png', "PNG")
    set_background()


def circle(img):
    width = SCALE * 550
    height = SCALE * 550
    size = (width, height)
    mask = Image.new('L', size, 0)
    draw = ImageDraw.Draw(mask)
    draw.ellipse((3, 3, width - 3, height - 3), fill=255)
    output = ImageOps.fit(img, mask.size, centering=(0.5, 0.5))
    output.putalpha(mask)
    output.thumbnail((214, 214), Image.ANTIALIAS)
    return output


def convert():
    # size = (768, 768)
    # mask = Image.new('L', size, 0)
    # draw = ImageDraw.Draw(mask)
    # draw.ellipse((3, 3, 765, 765), fill=255)
    # im = Image.open('/tmp/earth.png')
    # output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
    # output.putalpha(mask)
    # output.thumbnail((256, 256), Image.ANTIALIAS)
    # output.save('output.png')
    # output.convert("RGBA")
    output = Image.open('output.png')
    output.thumbnail((214, 214), Image.ANTIALIAS)
    desktop = Image.open('/home/jianglin/Pictures/308556.png')
    desktop.paste(output, (160, 160), output)
    desktop.save('/tmp/earch.png', "PNG")


def set_background():
    import os
    os.system('feh --bg-scale /tmp/earth.png')


if __name__ == '__main__':
    download()

作者: honmaple
链接: https://honmaple.me/articles/2017/03/himawari8图片下载改进版.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论