This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
forked from martinohmann/mpi-mandelbrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowimages.py
executable file
·99 lines (83 loc) · 3.13 KB
/
showimages.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
#!/usr/bin/python3
# use a tkinter label as a panel/frame with a background image
# note that tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
# give tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.ttk import Frame, Button, Style
import time
import glob
import os
import sys
class ShowLatestImage():
def __init__(self):
self.root = tk.Tk()
# pick an image file you have .bmp .jpg .gif. .png
# load the file and covert it to a Tkinter image object
imageFile = self.getLatestImage()
self.latestImage = self.readImage(imageFile)
self.currentImage = imageFile
self.root.title(imageFile)
print("Will display %s" % self.currentImage)
# get the image size
w = self.latestImage.width()
h = self.latestImage.height()
# position coordinates of root 'upper left corner'
x = 0
y = 0
# make the root window the size of the image
self.root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# root has no image argument, so use a label as a panel
self.panel1 = tk.Label(self.root, image=self.latestImage)
# Keep a reference to the image to prevent it from being
# garbage cleaned
self.panel1.image = self.latestImage
self.panel1.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
print("Starting to display %s" % imageFile)
self.root.after(30, self.updateImage)
self.root.mainloop()
def readImage(self, imageFile):
readOk = False
readCount = 0
while not readOk:
try:
image = ImageTk.PhotoImage(Image.open(imageFile))
readOk = True
except:
readCount = readCount + 1
if readCount >5:
print("Too many file reading failures on %s" % imageFile)
sys.exit()
readOk = False
time.sleep(0.02)
pass
return image
def getLatestImage(self):
while 1:
listOfFiles = glob.glob('*.bmp')
if listOfFiles:
break
time.sleep(0.05)
latestFile = max(listOfFiles, key=os.path.getctime)
while os.path.exists(latestFile + '.lock'):
time.sleep(0.01)
return latestFile
def updateImage(self):
imageFile = self.getLatestImage()
if imageFile != self.currentImage:
self.currentImage = imageFile
self.latestImage = self.readImage(imageFile)
print("Display %s" % imageFile)
self.root.title(imageFile)
self.panel1.configure(image=self.latestImage)
# Keep a reference to the image to prevent it from being
# garbage cleaned
self.panel1.image = self.latestImage
self.root.after(30, self.updateImage) # Set to call itself again
def main():
app = ShowLatestImage()
if __name__ == '__main__':
main()