himawari8图片下载改进版


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

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

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

每次只要运行一下

1python himawari8.py

源码

 1from PIL import Image, ImageOps, ImageDraw
 2from io import BytesIO
 3from urllib.request import Request, urlopen
 4from datetime import datetime, timedelta
 5import json
 6
 7SCALE = 2
 8WIDTH = 1368
 9HEIGHT = 768
10
11
12def get_info():
13    url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json"
14    request = Request(url)
15    response = urlopen(request, timeout=10)
16    return json.loads(response.read())
17
18
19def download():
20    png = Image.new('RGB', (550 * SCALE, 550 * SCALE))
21    # desktop = Image.new('RGB', (WIDTH, HEIGHT))
22    desktop = Image.open('/home/jianglin/Pictures/308556.png')
23    url_format = 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/{}d/{}/{}_{}_{}.png'
24    info = get_info()
25    # date = datetime.strptime(info['date'], '%Y-%m-%d %H:%M:%S') + timedelta(
26    #     hours=-8)
27    date = datetime.strptime(info['date'], '%Y-%m-%d %H:%M:%S')
28    for x in range(SCALE):
29        for y in range(SCALE):
30            url = url_format.format(SCALE, 550,
31                                    date.strftime("%Y/%m/%d/%H%M%S"), x, y)
32            print(url)
33            request = Request(url)
34            response = urlopen(request, timeout=10)
35            img = Image.open(BytesIO(response.read()))
36            png.paste(img, (550 * x, 550 * y, 550 * (x + 1), 550 * (y + 1)))
37    png = circle(png)
38    desktop.paste(png, (160, 160), png)
39    desktop.save('/tmp/earth.png', "PNG")
40    set_background()
41
42
43def circle(img):
44    width = SCALE * 550
45    height = SCALE * 550
46    size = (width, height)
47    mask = Image.new('L', size, 0)
48    draw = ImageDraw.Draw(mask)
49    draw.ellipse((3, 3, width - 3, height - 3), fill=255)
50    output = ImageOps.fit(img, mask.size, centering=(0.5, 0.5))
51    output.putalpha(mask)
52    output.thumbnail((214, 214), Image.ANTIALIAS)
53    return output
54
55
56def convert():
57    # size = (768, 768)
58    # mask = Image.new('L', size, 0)
59    # draw = ImageDraw.Draw(mask)
60    # draw.ellipse((3, 3, 765, 765), fill=255)
61    # im = Image.open('/tmp/earth.png')
62    # output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
63    # output.putalpha(mask)
64    # output.thumbnail((256, 256), Image.ANTIALIAS)
65    # output.save('output.png')
66    # output.convert("RGBA")
67    output = Image.open('output.png')
68    output.thumbnail((214, 214), Image.ANTIALIAS)
69    desktop = Image.open('/home/jianglin/Pictures/308556.png')
70    desktop.paste(output, (160, 160), output)
71    desktop.save('/tmp/earch.png', "PNG")
72
73
74def set_background():
75    import os
76    os.system('feh --bg-scale /tmp/earth.png')
77
78
79if __name__ == '__main__':
80    download()

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

加载评论