用Python监控NASA TV直播画面的实现步骤

演示地址:

https://replit.com/@PaoloAmoroso/spacestills

这是一个具有GUI的简单系统,它访问feed流并从Web下载数据。该程序仅需350行代码,并依赖于一些开源的Python库。

关于程序

Spacestills会定期从feed流中下载NASA TV静止帧并将其显示在GUI中。
该程序可以校正帧的纵横比,并将其保存为PNG格式。它会自动下载最新的帧,并提供手动重新加载,禁用自动重新加载或更改下载频率的选项。
Spacestillsis是一个比较初级的版本,但是它可以做一些有用的事情:捕获并保存NASA TV直播的太空事件图像。太空爱好者经常在社交网络或论坛共享他们从NASA TV手动获取的屏幕截图。Spacestills节省了使用屏幕捕获工具的时间,并保存了可供共享的图像文件。您可以在Replit上在线运行Spacestills。

开发环境

笔者用Replit开发了Spacestills。Replit是云上的开发,部署和协作环境,它支持包括Python在内的数十种编程语言和框架。作为Chrome操作系统和云计算爱好者,笔者非常喜欢Replit,因为它可以在浏览器中完全正常运行,无需下载或安装任何内容。

资源和依赖包

Spacestills依赖于一些外部资源和Python库。

NASA TV feed 流

肯尼迪航天中心的网站上有一个页面,其中包含精选的NASA视频流,包括NASA电视公共频道。feed流显示最新的静止帧并自动更新。
每个feed都带有三种尺寸的帧,Spacestills依赖于具有704x408像素帧的最大NASA TV feed流。最大更新频率为每45秒一次。因此,检索最新的静止帧就像从feed流的URL下载JPEG图像一样简单。
原始图像被垂直拉伸,看起来很奇怪。因此,该程序可以通过压缩图像并生成未失真的16:9版本来校正纵横比。

Python

因PySimpleGUI的原因需要安装 Python 3.6 版本。

第三方库

  • Pillow:图像处理
  • PySimpleGUI:GUI框架(Spacestills使用Tkinter后端)
  • Request:HTTP请求

完整代码

from io import BytesIO
from datetime import datetime, timedelta
from pathlib import Path
import requests
from requests.exceptions import Timeout
from PIL import Image
import PySimpleGUI as sg

FEED_URL = 'https://science.ksc.nasa.gov/shuttle/countdown/video/chan2large.jpg'

# Frame size without and with 16:9 aspect ratio correction
WIDTH = 704
HEIGHT = 480
HEIGHT_16_9 = 396

# Minimum, default, and maximum autoreload interval in seconds
MIN_DELTA = 45
DELTA = MIN_DELTA
MAX_DELTA = 300

class StillFrame():
    """Holds a still frame.
    
    The image is stored as a PNG PIL.Image and kept in PNG format.
    Attributes
    ----------
        image : PIL.Image
            A still frame
        original : PIL.Image
            Original frame with wchich the instance is initialized, cached in case of
            resizing to the original size
    
    Methods
    -------
        bytes : Return the raw bytes
        resize : Resize the screenshot
        new_size : Calculate new aspect ratio
    """

    def __init__(self, image):
        """Convert the image to PNG and cache the converted original.
        Parameters
        ----------
            image : PIL.Image
                Image to store
        """
        self.image = image
        self._topng()
        self.original = self.image

    def _topng(self):
        """Convert image format of frame to PNG.
        Returns
        -------
            StillFrame
                Frame with image in PNG format
        """
        if not self.image.format == 'PNG':
            png_file = BytesIO()
            self.image.save(png_file, 'png')
            png_file.seek(0)
            png_image = Image.open(png_file)
            self.image = png_image
        return self

    def bytes(self):
        """Return raw bytes of a frame image.
        
        Returns
        -------
            bytes
                Byte stream of the frame image
        """
        file = BytesIO()
        self.image.save(file, 'png')
        file.seek(0)
        return file.read()

    def new_size(self):
        """Return image size toggled between original and 16:9.
        
        Returns
        -------
            2-tuple
                New size
        """
        size = self.image.size
        original_size = self.original.size
        new_size = (WIDTH, HEIGHT_16_9) if size == original_size else (WIDTH, HEIGHT)
        return new_size

    def resize(self, new_size):
        """Resize frame image.
        
        Parameters
        ----------
            new_size : 2-tuple
                New size
        Returns
        -------
            StillFrame
                Frame with image resized
        """
        if not(self.image.size == new_size):
            self.image = self.image.resize(new_size)
        return self
    

def make_blank_image(size=(WIDTH, HEIGHT)):
    """Create a blank image with a blue background.
    
    Parameters
    ----------
        size : 2-tuple
            Image size
    
    Returns
    -------
        PIL.Image
            Blank image
    """
    image = Image.new('RGB', size=size, color='blue')
    return image

def download_image(url):
    """Download current NASA TV image.
    Parameters
    ----------
        url : str
            URL to download the image from
    
    Returns
    -------
        PIL.Image
            Downloaded image if no errors, otherwise blank image
    """
    try:
        response = requests.get(url, timeout=(0.5, 0.5))
        if response.status_code == 200:
            image = Image.open(BytesIO(response.content))
        else:
            image = make_blank_image()
    except Timeout:
        image = make_blank_image()
    return image

def refresh(window, resize=False, feed=FEED_URL):
    """Display the latest still frame in window.
    
    Parameters
    ----------
        window : sg.Window
            Window to display the still to
        feed : string
            Feed URL
    
    Returns
    -------
        StillFrame
            Refreshed screenshot
    """
    still = StillFrame(download_image(feed))
    if resize:
        still = change_aspect_ratio(window, still, new_size=(WIDTH, HEIGHT_16_9))
    else:
        window['-IMAGE-'].update(data=still.bytes())
    return still

def change_aspect_ratio(window, still, new_size=(WIDTH, HEIGHT_16_9)):
    """Change the aspect ratio of the still displayed in window.
    
    Parameters
    ----------
        window : sg.Window
            Window containing the still
        new_size : 2-tuple
            New size of the still
    
    Returns
    -------
        StillFrame
            Frame containing the resized image
    """
    resized_still = still.resize(new_size)
    window['-IMAGE-'].update(data=resized_still.bytes())
    return resized_still

def save(still, path):
    """Save still to a file.
    Parameters
    ----------
        still : StillFrame
            Still to save
        path : string
            File name
    
    Returns
    -------
        Boolean
            True if file saved with no errors
    """
    filename = Path(path)
    try:
        with open(filename, 'wb') as file:
            file.write(still.bytes())
        saved = True
    except OSError:
        saved = False
    return saved

def next_timeout(delta):
    """Return the moment in time right now + delta seconds from now.
    Parameters
    ----------
        delta : int
            Time in seconds until the next timeout
    
    Returns
    -------
        datetime.datetime
            Moment in time of the next timeout
    """
    rightnow = datetime.now()
    return rightnow + timedelta(seconds=delta)

def timeout_due(next_timeout):
    """Return True if the next timeout is due.
    Parameters
    ----------
        next_timeout : datetime.datetime
    
    Returns
    -------
        bool
            True if the next timeout is due
    """
    rightnow = datetime.now()
    return rightnow >= next_timeout

def validate_delta(value):
    """Check if value is an int within the proper range for a time delta.
    Parameters
    ----------
        value : int
            Time in seconds until the next timeout
    
    Returns
    -------
        int
            Time in seconds until the next timeout
        bool
            True if the argument is a valid time delta
    """
    isinteger = False
    try:
        isinteger = type(int(value)) is int
    except Exception:
        delta = DELTA
    delta = int(value) if isinteger else delta
    isvalid = MIN_DELTA <= delta <= MAX_DELTA
    delta = delta if isvalid else DELTA
    return delta, isinteger and isvalid

LAYOUT = [[sg.Image(key='-IMAGE-')],
          [sg.Checkbox('Correct aspect ratio', key='-RESIZE-', enable_events=True),
           sg.Button('Reload', key='-RELOAD-'),
           sg.Button('Save', key='-SAVE-'),
           sg.Exit()],
          [sg.Checkbox('Auto-reload every (seconds):', key='-AUTORELOAD-',
                       default=True),
           sg.Input(DELTA, key='-DELTA-', size=(3, 1), justification='right'),
           sg.Button('Set', key='-UPDATE_DELTA-')]]

def main(layout):
    """Run event loop."""
    window = sg.Window('Spacestills', layout, finalize=True)
    current_still = refresh(window)

    delta = DELTA
    next_reload_time = datetime.now() + timedelta(seconds=delta)

    while True:
        event, values = window.read(timeout=100)
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        elif ((event == '-RELOAD-') or
                (values['-AUTORELOAD-'] and timeout_due(next_reload_time))):
            current_still = refresh(window, values['-RESIZE-'])
            if values['-AUTORELOAD-']:
                next_reload_time = next_timeout(delta)
        elif event == '-RESIZE-':
            current_still = change_aspect_ratio(
                window, current_still, current_still.new_size())
        elif event == '-SAVE-':
            filename = sg.popup_get_file(
                'File name', file_types=[('PNG', '*.png')], save_as=True,
                title='Save image', default_extension='.png')
            if filename:
                saved = save(current_still, filename)
                if not saved:
                    sg.popup_ok('Error while saving file:', filename, title='Error')
        elif event == '-UPDATE_DELTA-':
            # The current cycle should complete at the already scheduled time. So
            # don't update next_reload_time yet because it'll be taken care of at the
            # next -AUTORELOAD- or -RELOAD- event.
            delta, valid = validate_delta(values['-DELTA-'])
            if not valid:
                window['-DELTA-'].update(str(DELTA))

    window.close()
    del window

if __name__ == '__main__':
    main(LAYOUT)

以上就是用 Python 监控 NASA TV 直播画面的实现步骤的详细内容,更多关于Python 监控 NASA TV 直播画面的资料请关注我们其它相关文章!

(0)

相关推荐

  • python中watchdog文件监控与检测上传功能

    引言 上一篇介绍完了观察者模式的原理,本篇想就此再介绍一个小应用,虽然我也就玩了一下午,是当时看observer正好找到的,以及还有Django-observer,但Django很久没用了,所以提下这个作为一个笔记. watchdog介绍 Watchdog的中文的"看门狗",有保护的意思.最早引入Watchdog是在单片机系统中,由于单片机的工作环境容易受到外界磁场的干扰,导致程序"跑飞",造成整个系统无法正常工作,因此,引入了一个"看门狗",对

  • python 监控logcat关键字功能

    本文主要介绍使用Python调用ADB命令实现实时监控logcat关键字的功能 采用多进程,可同时监控多个设备,监控多个关键字. 需要配置ADB环境,具体配置就不多介绍,随便搜一下一大把,直接上代码 通过一个全局变量控制开启和关闭监控功能, INSTRUCTION 用于根据指令获取对应的方法名 import os, threading, datetime # 获取当前文件所在目录,拼接出LOG路径 LOG_PATH = os.path.join(os.path.dirname(os.path.a

  • Python实战之能监控文件变化的神器—看门狗

    一.前言 假设现在有一个应用场景,需要对文件系统进行监控,发生变化时产生日志,对新增的文件做一些相应的操作. 比如说应用到我们之前的音乐高潮提取器:若当前文件夹下增加了一个音乐文件,监控器就调用音乐高潮提取器,自动提取该音乐文件的高潮部分. 这样的监控器写起来也不难,但是很花时间,有许多情况要考虑.不过幸好我们是写Python的,有许多轮子可以使用. 二.准备 开始之前,你要确保Python和pip已经成功安装在电脑上噢. Windows环境下打开Cmd(开始-运行-CMD),苹果系统环境下请打

  • python使用pynput库操作、监控你的鼠标和键盘

    楔子 python是一门很神奇的语言,原因在于它有很多的库可以实现各种意想不到的功能.当然我们这次介绍的库所实现的功能却是已经很常见了,就是操作.监控你的鼠标和键盘.如果你写过游戏,那么即使不用下面即将介绍的库也可以实现对鼠标.键盘的操作以及监控. 当然我们下面介绍库:pynput,是专门针对鼠标和键盘的,至于pygame.pyglet等游戏框架虽然也提供了鼠标.键盘的监控事件,但它们毕竟是用来开发游戏的,还提供了创建窗口.图形绘制.物体的碰撞检测等等很多复杂的功能.如果只是单纯的操作鼠标和键盘

  • 用pushplus+python监控亚马逊到货动态推送微信

    xbox series和ps5发售以来,国内黄牛价格一直居高不下.虽然海外amazon上ps5补货很少而且基本撑不过一分钟,但是xbox series系列明显要好抢很多. 日亚.德亚的xbox series x/s都可以直邮中国大陆,所以我们只需要借助脚本,监控相关网页的动态,在补货的第一时刻通过微信告知我们,然后迅速人工购买即可! 需求:pushplus(需要微信关注公众号).python3 一.pushplus相关介绍 pushplus提供了免费的微信消息推送api,具体内容可以参考他的官网

  • 如何基于Python和Flask编写Prometheus监控

    介绍 Prometheus 的基本原理是通过 HTTP 周期性抓取被监控组件的状态. 任意组件只要提供对应的 HTTP 接口并且符合 Prometheus 定义的数据格式,就可以接入 Prometheus 监控. Prometheus Server 负责定时在目标上抓取 metrics(指标)数据并保存到本地存储.它采用了一种 Pull(拉)的方式获取数据,不仅降低客户端的复杂度,客户端只需要采集数据,无需了解服务端情况,也让服务端可以更加方便地水平扩展. 如果监控数据达到告警阈值,Promet

  • 用python监控服务器的cpu,磁盘空间,内存,超过邮件报警

    监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警: 邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:"Linux使用mailx发邮件",脚本如下: 一.cpu ideal值,不小于20% #!/usr/bin/python # -*- coding: utf-8 -*-   import datetime import os     f = os.p

  • python 自动监控最新邮件并读取的操作

    我就废话不多说了,大家还是直接看代码吧~ #zmail库:可以用几行代码帮我们收取一封邮件 import zmail #输入账号和密码 server=zmail.server('13163964546@qq.com','jie110341') #获取最新的一封邮件 mail=server.get_latest() #读取邮件 #zmail.show(mail) #读取邮件的部分内容 print(mail['subject']) ...... #读取附件 邮件 存放路径 如果有同名文件则覆盖 zm

  • 教你怎么用Python监控愉客行车程

    一.愉客行车程监控并通知 大概思路:用户填写指定信息在config.json文件中,通过定时访问网页,获取指定信息,从而达到对指定车程的监控 1.分析网页 按下F12,打开开发者工具,再刷新一下网页 找到我们需要的信息 然后再分析一下它的请求方式 很直观的就看到了几条主要的信息 第一条和第三条是null不重要 第二条是起始站 第四条是终点站 第五条是个数字,经过反复尝试,发现是固定参数 第六条乍一看应该是时间戳,经过验证,的确是车票指定日期零点的时间戳 2.请求头伪装.带参访问指定网页,获取信息

  • 用Python监控NASA TV直播画面的实现步骤

    演示地址: https://replit.com/@PaoloAmoroso/spacestills 这是一个具有GUI的简单系统,它访问feed流并从Web下载数据.该程序仅需350行代码,并依赖于一些开源的Python库. 关于程序 Spacestills会定期从feed流中下载NASA TV静止帧并将其显示在GUI中. 该程序可以校正帧的纵横比,并将其保存为PNG格式.它会自动下载最新的帧,并提供手动重新加载,禁用自动重新加载或更改下载频率的选项. Spacestillsis是一个比较初级

  • python监控linux内存并写入mongodb(推荐)

    (需要安装psutil 用来获取服务器资源,以及pymongo驱动)#pip install psutil #pip install pymongo #vim memory_monitory.py 文件内容如下 #!/usr/bin/env python # -*- coding: UTF-8 -*- import psutil import socket import time from pymongo import MongoClient mongodbIp = '192.168.200.1

  • python监控网站运行异常并发送邮件的方法

    本文实例讲述了python监控网站运行异常并发送邮件的方法.分享给大家供大家参考.具体如下: 这是一个简单的python开发的监控程序,当指定网页状态不正常是通过smtp发送通知邮件 复制代码 代码如下: #!/usr/bin/env python # -*- coding: UTF-8 -*- #author  libertyspy import socket import smtplib import urllib mail_options = {     'server':'smtp.qq

  • python监控键盘输入实例代码

    本文研究的主要是python监控键盘输入的相关代码,用到了os,sys,time等,具体实现代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import tty, termios import time if __name__ == '__main__': print "Reading form keybord" print """ i j k l m&quo

  • python监控进程脚本

    本文实例为大家分享了python监控进程脚本的具体代码,供大家参考,具体内容如下 原理: 监控一个指定进程,每隔5秒钟获取其CPU.内存使用量超过60%即kill掉该进程,获取其句柄数,超过300也kill掉该进程 运行环境是windows 64位系统+python 2.7 64位 ,这里需要使用到psutil 类库,要另外安装.脚本里面可以自动安装,前提是你已经下载好了安装包psutil-3.3.0.win-amd64-py2.7.exe 下面看代码: #!/usr/bin/env pytho

  • 在linux下实现 python 监控usb设备信号

    1. linux下消息记录 关于系统的各种消息一般都会记录在/var/log/messages文件中,有些主机在中默认情况下有可能没有启用,具体配置方法可参考下面这篇博客: 系统日志配置 /var/log/messages 2. python 代码实现 原理其实很简单,就是读/var/log/messages文件,找到有关usb的信息就可以了. #!/usr/bin/env python usbmsg = open("/var/log/messages", "r")

  • python监控进程状态,记录重启时间及进程号的实例

    本脚本为本人在性能测试过程中编写,用于对进程状态的监控,也可以用于日常的监控,适用性一般,扩展性还行 # -*- coding: UTF-8 -*- # author=baird_xiang import os import time import re import copy nginxRestart_num= -1 nginxReload_num= -1 logSender_num= -1 es_num= -1 nginxParent_pid=[] nginxChild_pid=[] log

  • python监控nginx端口和进程状态

    本文实例为大家分享了python监控nginx端口和进程状态的具体代码,供大家参考,具体内容如下 #!/usr/local/bin/python # coding:utf-8 import psutil import sys import os # 获取主机名称 def hostname(): sys = os.name if sys == 'nt': hostname = os.getenv('computername') return hostname elif sys == 'posix'

  • python监控文件并且发送告警邮件

    本文实例为大家分享了python监控文件并发送邮件的具体代码,供大家参考,具体内容如下 一.配置文件 import time,datetime TODAY = time.time() TIME_PATH = str(TODAY.year) + "/" + str(TODAY.month) + "/" + str(datetime.datetime.now().date()) MONITOR_CONFIG = { "monitor_file":[

随机推荐