Python3:获取实时地球照片并设为电脑壁纸

看过刘慈欣的《三体》后,对宇宙有种无比的向往,晚上仰视群星,白天俯瞰地球。


Python3:获取实时地球照片并设为电脑壁纸_第1张图片

早上8:50的地球


Python3:获取实时地球照片并设为电脑壁纸_第2张图片
中午1:00的地球

代码:

#安装pip3.6 install Pillow

#安装pip3.6 install Requests

#安装pip3.6 install python-dateutil

#安装pip3.6 install pypiwin32  可能需要重启电脑

import sys

import time

import json

import requests

from PIL import Image

from io import BytesIO

from datetime import datetime

from dateutil import tz

import win32api, win32con, win32gui

conf ={

'last_refresh_url': 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json',  # 最新图片

'img_url_pattern': 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/%id/550/%s_%i_%i.png',    # scale, time, row, col

'scale': 1,    # scale 设置结果图片的大小, 图片的宽和高都为 scale×550 。 scale 可以是 1, 2, 4, 8, 16, 20

}

def get_last_time():

r = requests.get(conf['last_refresh_url'])

resp = json.loads(r.text)

last_refresh_time = datetime.strptime(resp['date'], '%Y-%m-%d %H:%M:%S')

return last_refresh_time

# 转换时间

def utf2local(utc):

from_zone = tz.tzutc()

to_zone = tz.tzlocal()

utc = utc.replace(tzinfo=from_zone)

return utc.astimezone(to_zone)

def download(args):

scale = args['scale']

png = Image.new('RGB', (550*scale, 550*scale))

for row in range(scale):

for col in range(scale):

#print ('下载中 %i  %i ...' %(row*scale + col + 1, scale*scale))

strtime = args['time'].strftime("%Y/%m/%d/%H%M%S")

url = conf['img_url_pattern'] % (args['scale'], strtime, row, col)

r = requests.get(url)

tile = Image.open(BytesIO(r.content))

png.paste(tile, (550 * row, 550 * col, 550 * (row + 1), 550 * (col + 1)))

if 'fout' in args:

fpath = args['fout']

else:

fpath = "%s.png" % utf2local(args['time']).strftime("%Y/-%m/-%d/ %H.%M.%S").replace('/', '')

print ('下载完成, 保存图片为 %s' % fpath)

png.save(fpath, "PNG")

set_wallpaper(fpath)

def get_last_image(fout=None, scale=1):

#    print ('output[%s] scale[%i]' %(fout, scale))

last_refresh_time = get_last_time()

args = {'time': last_refresh_time}

args['scale'] = scale

if fout is not None:

args['fout'] = fout

download(args)

# 以需要的时间间隔执行某个命令

def re_exe(inc):

count=0

while True:

count += 1

print ('下载第'+str(count)+'张...')

if len(sys.argv) == 1:

get_last_image()

elif len(sys.argv) == 2:

get_last_image(fout=sys.argv[1])

elif len(sys.argv) == 3:

get_last_image(fout=sys.argv[1], scale=int(sys.argv[2]))

time.sleep(inc)

#把下载的地球图片 设置桌面壁纸-----------------------------------------------------------------------------

def set_wallpaper(fpath):

print ('把'+fpath+'设置为桌面壁纸 \n ')

# 打开指定注册表路径img_path

reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)

# 最后的参数:2拉伸,0居中,6适应,10填充,0平铺

win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "0")

# 最后的参数:1表示平铺,拉伸居中等都是0

win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")

# 刷新桌面

win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, "C:\\Users\\panx\\Desktop\\实时地球图片壁纸\\"+fpath, win32con.SPIF_SENDWININICHANGE)

re_exe(600)#10分钟执行一次

喜欢的自行优化代码。

                                                                                                                                              ——Javen

                                                                                                                                              2017.11.3

你可能感兴趣的:(Python3:获取实时地球照片并设为电脑壁纸)