-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_viewer.py
57 lines (52 loc) · 2.07 KB
/
image_viewer.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
import pyglet
from pyglet.gl import *
import math
import numpy as np
import sys
class SimpleImageViewer(object):
def __init__(self, display=None, maxwidth=500):
self.window = None
self.isopen = False
self.display = display
self.maxwidth = maxwidth
def imshow(self, arr):
if self.window is None:
height, width, _channels = arr.shape
if width > self.maxwidth:
scale = self.maxwidth / width
width = int(scale * width)
height = int(scale * height)
self.window = pyglet.window.Window(width=width, height=height,
display=self.display, vsync=False, resizable=True)
self.width = width
self.height = height
self.isopen = True
@self.window.event
def on_resize(width, height):
self.width = width
self.height = height
@self.window.event
def on_close():
self.isopen = False
assert len(arr.shape) == 3, "You passed in an image with the wrong shape"
assert arr.shape[2] < 5 and arr.shape[2] > 0, "You passed in an image with the wrong number of channels"
formats = ["L","LA","RGB","RGBA"]
format = formats[arr.shape[2]-1]
image = pyglet.image.ImageData(arr.shape[1], arr.shape[0],
format, arr.tobytes(), pitch=arr.shape[1]*-arr.shape[2])
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
texture = image.get_texture()
texture.width = self.width
texture.height = self.height
self.window.clear()
self.window.switch_to()
self.window.dispatch_events()
texture.blit(0, 0) # draw
self.window.flip()
def close(self):
if self.isopen and sys.meta_path:
# ^^^ check sys.meta_path to avoid "ImportError: sys.meta_path is None, Python is likely shutting down"
self.window.close()
self.isopen = False
def __del__(self):
self.close()