亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

用Python監控NASA TV直播畫面的實現步驟

瀏覽:4日期:2022-06-19 09:12:44
目錄演示地址:關于程序開發環境資源和依賴包NASA TV feed 流Python第三方庫完整代碼演示地址:

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

用Python監控NASA TV直播畫面的實現步驟

這是一個具有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 BytesIOfrom datetime import datetime, timedeltafrom pathlib import Pathimport requestsfrom requests.exceptions import Timeoutfrom PIL import Imageimport PySimpleGUI as sgFEED_URL = ’https://science.ksc.nasa.gov/shuttle/countdown/video/chan2large.jpg’# Frame size without and with 16:9 aspect ratio correctionWIDTH = 704HEIGHT = 480HEIGHT_16_9 = 396# Minimum, default, and maximum autoreload interval in secondsMIN_DELTA = 45DELTA = MIN_DELTAMAX_DELTA = 300class 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 frameoriginal : PIL.Image Original frame with wchich the instance is initialized, cached in case of resizing to the original sizeMethods -------bytes : Return the raw bytesresize : Resize the screenshotnew_size : Calculate new aspect ratio ''' def __init__(self, image):'''Convert the image to PNG and cache the converted original.Parameters---------- image : PIL.ImageImage to store'''self.image = imageself._topng()self.original = self.image def _topng(self):'''Convert image format of frame to PNG.Returns------- StillFrameFrame 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_imagereturn self def bytes(self):'''Return raw bytes of a frame image.Returns------- bytesByte 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-tupleNew size'''size = self.image.sizeoriginal_size = self.original.sizenew_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-tupleNew sizeReturns------- StillFrameFrame 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 sizeReturns -------PIL.Image Blank image ''' image = Image.new(’RGB’, size=size, color=’blue’) return imagedef download_image(url): '''Download current NASA TV image. Parameters ----------url : str URL to download the image fromReturns -------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 imagedef refresh(window, resize=False, feed=FEED_URL): '''Display the latest still frame in window.Parameters ----------window : sg.Window Window to display the still tofeed : string Feed URLReturns -------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 stilldef 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 stillnew_size : 2-tuple New size of the stillReturns -------StillFrame Frame containing the resized image ''' resized_still = still.resize(new_size) window[’-IMAGE-’].update(data=resized_still.bytes()) return resized_stilldef save(still, path): '''Save still to a file. Parameters ----------still : StillFrame Still to savepath : string File nameReturns -------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 saveddef next_timeout(delta): '''Return the moment in time right now + delta seconds from now. Parameters ----------delta : int Time in seconds until the next timeoutReturns -------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.datetimeReturns -------bool True if the next timeout is due ''' rightnow = datetime.now() return rightnow >= next_timeoutdef 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 timeoutReturns -------int Time in seconds until the next timeoutbool 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 isvalidLAYOUT = [[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’): breakelif ((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 windowif __name__ == ’__main__’: main(LAYOUT)

以上就是用 Python 監控 NASA TV 直播畫面的實現步驟的詳細內容,更多關于Python 監控 NASA TV 直播畫面的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产成人亚洲合集青青草原精品 | 亚洲1区 | 国产精品久久福利新婚之夜 | 黄色录像一级毛片 | 亚洲精品网站在线 | 欧美综合一区 | 国产精品jizz在线观看网站 | 亚洲国产成人久久精品影视 | 亚洲码欧美码一区二区三区 | 在线看黄色| 模特尤妮丝凹凸福利视频 | 在线黄观看 | 男女动态视频在线观看 | 黄色网在线免费观看 | 久久综合精品国产一区二区三区无 | 黄色片网站在线免费观看 | 久久久精品日本一区二区三区 | 91亚洲国产在人线播放午夜 | 尤物视频在线看 | 国产综合色在线视频播放线视 | 看真人一级毛片 | 日韩欧美亚洲综合久久99e | 日高千晶在线观看 | 爱爱网站在线观看免费 | 日韩大尺度无遮挡理论片 | 久久国模 | 成人男女啪啪免费观看网站 | 日本乱理伦中文三区 | 91精品免费不卡在线观看 | 欧美特级毛片 | 中文字幕在线播放第一页 | 在线成人免费观看国产精品 | 国产在线视频在线 | 亚洲精品一区二区三区人妖 | 久久精品视频99精品视频150 | 亚洲国产色综合有声小说 | a级毛片免费在线观看 | 黄色免费网站在线看 | 国产 日韩 欧美在线 | 中国黄色片一级 | 成人免费一区二区三区 |