-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNasaStills_support.py
230 lines (192 loc) · 6.03 KB
/
NasaStills_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# ======================================================
# NasaStills_support.py
# ------------------------------------------------------
# Created for Full Circle Magazine Issue #175 November 2021
# Written by G.D. Walters
# Copyright (c) 2021 by G.D. Walters
# This source code is released under the MIT License
# ======================================================
# Support module generated by PAGE version 6.2
# in conjunction with Tcl version 8.6
# Nov 02, 2021 04:25:56 AM CDT platform: Linux
import sys
from os.path import exists
from datetime import datetime, timedelta
from pathlib import Path
import requests
from requests.exceptions import Timeout
from PIL import Image, ImageTk
import shutil
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
from tkinter import Spinbox, messagebox
from tkinter import font
from tkinter import filedialog
from tkinter import constants
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
def set_Tk_var():
global spinbox
spinbox = tk.StringVar()
spinbox.set('45')
global che47
che47 = tk.IntVar()
global che48
che48 = tk.IntVar()
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
startup()
def startup():
global feed_url, WIDTH, HEIGHT, HEIGHT_16_9, MIN_DELTA, MAX_DELTA, DELTA
global Timer_ID, resize, refresh_time, debug
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
refresh_time = MIN_DELTA
resize = True
spinbox.set(DELTA)
che48.set(0)
che47.set(0)
w.Spinbox1.configure(state=constants.DISABLED)
debug = True
# check for existence of the save file counter
if exists('filecounter.txt'):
pass
else:
filecount = open("filecounter.txt", "w")
filecount.write('0')
filecount.close
# Set up the root.after timer
centre_screen(777, 657)
Timer_ID = root.after(0, on_tick())
def on_chkAspect():
# print('NasaStills_support.on_chkAspect')
# sys.stdout.flush()
pass
def on_chkTime():
# print('NasaStills_support.on_chkTime')
# sys.stdout.flush()
if che48.get() == 1:
w.Spinbox1.configure(state=tk.NORMAL)
else:
w.Spinbox1.configure(state=tk.DISABLED)
refresh_time = spinbox.get()
root.update()
def on_btnExit():
# print('NasaStills_support.on_btnExit')
# sys.stdout.flush()
destroy_window()
def on_btnReload():
# print('NasaStills_support.on_btnReload')
# sys.stdout.flush()
global feed_url
get_image_from_web(feed_url)
def on_btnSave():
# print('NasaStills_support.on_btnSave')
# sys.stdout.flush()
# showinfo("Save", "Save function is not yet implemented")
# get last filenumber
filecount = open('filecounter.txt', 'r+')
global lastfile
lastfile = filecount.read()
lastfilenumber = int(lastfile) + 1
filecount.seek(0)
filecount.write(str(lastfilenumber))
filecount.close
src = 'local_image.png'
dst = f'NasaStills{lastfilenumber}.png'
shutil.copyfile(src, dst)
def on_btnSet():
# print('NasaStills_support.on_btnSet')
# sys.stdout.flush()
showinfo("Set", "Set function is not yet implemented")
def on_spinChange():
# print('NasaStills_support.on_spinChange')
# sys.stdout.flush()
global refresh_time
refresh_time = spinbox.get()
def get_image_from_web(url):
# Attempt to get image from url and place it in w.lblImage
global _img2, debug
pic_url = url
if debug:
print(f'Refresh Time: {refresh_time}')
print(f'Attempting to get {url}')
try:
with open('pic1.jpg', 'wb') as handle:
response = requests.get(url, stream=True)
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
jpgfile = Image.open('pic1.jpg')
jpgfile.save('local_image.png')
original = Image.open('local_image.png')
wid, hei = original.size
if che47.get():
newheight = HEIGHT_16_9
else:
newheight = HEIGHT
if debug:
print(f'Width: {wid} - Height: {hei}')
_img1 = original.resize((WIDTH, newheight), Image.ANTIALIAS)
_img2 = ImageTk.PhotoImage(_img1)
w.labelImage.configure(image=_img2)
_img1.save('local_image.png')
except Exception:
boxTitle = "Image Error"
boxMessage = "An error occured getting the image."
showerror(boxTitle, boxMessage)
if debug:
print("An error occured getting the image")
_img2 = None
w.labelImage.configure(image=_img2)
def on_tick():
global Timer_ID, debug
global feed_url # , WIDTH, HEIGHT, HEIGHT_16_9, MIN_DELTA, MAX_DELTA, DELTA
if debug:
print('Into on_tick')
print(f'RefreshTime = {spinbox.get()}')
print(datetime.now())
rt = int(spinbox.get())
if debug:
print(f'rt: {spinbox.get()}')
get_image_from_web(feed_url)
Timer_ID = root.after(rt * 1000, on_tick)
def showinfo(titl, msg):
messagebox.showinfo(titl, msg, parent=root, icon=messagebox.INFO)
def showerror(titl, msg):
messagebox.showerror(titl, msg, parent=root, icon=messagebox.ERROR)
def centre_screen(wid, hei):
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws / 2) - (wid / 2)
y = (hs / 2) - (hei / 2)
root.geometry('%dx%d+%d+%d' % (wid, hei, x, y))
def destroy_window():
# Function which closes the window.
global top_level
top_level.destroy()
top_level = None
if __name__ == '__main__':
import NasaStills
NasaStills.vp_start_gui()