Python3:获取地球实时卫星照片

利用Python获取地球实时卫星照片最详细攻略,附代码。

一、基础信息

环境:Python3

卫星向日葵8号主页

卫星接口

http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json

a.实时图片接口示例

注:2020/02/27为日期,0550为格林威治时间

http://himawari8-dl.nict.go.jp/himawari8/img/D531106/thumbnail/550/2020/02/27/055000_0_0.png

b.实时视频接口

注:20200227为日期

http://himawari8-dl.nict.go.jp/himawari8/movie/720/20200227_pifd.mp4

http://himawari8-dl.nict.go.jp/himawari8/movie/720/coastline/20200227_pifd.mp4

二、代码

import datetime
import gc
import time
import urllib.request

from PIL import Image, ImageFile
from retrying import retry


def getDate():
    localTime = datetime.datetime.now() - datetime.timedelta(minutes=30)
    zeroTime = localTime - datetime.timedelta(hours=8)
    localDate = "{}年{}月{}日\n  {}时{}分00秒" \
        .format(localTime.year, '0' * (2 - len(str(localTime.month))) + str(localTime.month),
                '0' * (2 - len(str(localTime.day))) + str(localTime.day),
                '0' * (2 - len(str(localTime.hour))) + str(localTime.hour),
                str(localTime.minute // 10) + '0' * (2 - len(str(localTime.minute // 10))))
    zeroDate = {'year': str(zeroTime.year), 'month': '0' * (2 - len(str(zeroTime.month))) + str(zeroTime.month),
                'day': '0' * (2 - len(str(zeroTime.day))) + str(zeroTime.day),
                'hour': '0' * (2 - len(str(zeroTime.hour))) + str(zeroTime.hour),
                'minute': str(zeroTime.minute // 10) + '0' * (2 - len(str(zeroTime.minute // 10)))}
    return localDate, zeroDate


def getURL(multiple, zeroDate, i, j):
    rawURL = 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/{}d/550/{}/{}/{}/{}{}00_{}_{}.png'
    earthURL = rawURL.format(multiple, zeroDate['year'], zeroDate['month'], zeroDate['day'], zeroDate['hour'],
                             zeroDate['minute'], i, j)
    return earthURL


def downloadImg(multiple, zeroDate):
    earth = Image.new('RGB', (multiple * 550, multiple * 550))
    for i in range(multiple):
        for j in range(multiple):
            URL = getURL(multiple, zeroDate, i, j)
            print('{}/{} picture started to download\n'
                  '    URL->{}'.format(i * multiple + j + 1, pow(multiple, 2), URL))
            image = down(URL)
            f = ImageFile.Parser()
            f.feed(image)
            tempImg = f.close()
            earth.paste(tempImg, (i * 550, j * 550, (i + 1) * 550, (j + 1) * 550))
            print('{}/{} picture has been download'.format(i * multiple + j + 1, pow(multiple, 2)))
    return earth


# @retry(wait_random_min=0, wait_random_max=3000)
@retry
def down(URL):
    print('    Try')
    headers = {'Connection': 'Keep-Alive',
               'Accept': 'text/html, application/xhtml+xml, */*',
               'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
               'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
               }
    req = urllib.request.Request(URL, headers=headers)
    image = urllib.request.urlopen(req, timeout=1).read()
    return image



def start():
    # multiple 可以是 1, 2, 4, 8, 16, 20,数字越大图片质量越好
    multiple = 8
    localDate, zeroDate = getDate()
    earth = downloadImg(multiple, zeroDate)
    return earth

if __name__ == '__main__':
    while True:
        try:
            a = time.time()
            gc.disable()
            earth = start()
            earth.save('earth.jpg')

            gc.enable()
            b = time.time()
            print('{}s'.format(b - a))
        except Exception as e:
            print(e)
        finally:
            gc.collect()
            c = time.time()
            print('{}s'.format(c - b))
            print('------picture has been saved------')
        print('Start waiting')
        time.sleep(300)
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值