Skip to content

Commit

Permalink
Add PygletThreadedImageViewer to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Feb 9, 2020
1 parent 4fbcc86 commit 21e7659
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/source/reference/io/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ IO Module
imgviz.io.pyplot_to_numpy
imgviz.io.pyglet_imshow
imgviz.io.pyglet_run
imgviz.io.PygletThreadedImageViewer
38 changes: 32 additions & 6 deletions imgviz/_io/_pyglet/pyglet_threaded_image_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@

class PygletThreadedImageViewer(pyglet.window.Window):

def __init__(self, play=True, interval=0.5):
'''Image viewer with threading.'''

def __init__(self, play=True, interval=0.5, **kwargs):
'''Initialize the image viewer.
Parameters
----------
play: bool
If True, it shows automatically for each `imshow()`.
interval: float
Interval in seconds for each `imshow()` call.
**kwargs:
Keyword arguments passed to :meth:`pyglet.window.Window.__init__`.
.. seealso:: :class:`pyglet.window.Window`
'''
self._play = play
self._next = False

Expand All @@ -17,17 +33,23 @@ def __init__(self, play=True, interval=0.5):
self.sprite = None

self.lock = threading.Lock()
self.thread = threading.Thread(target=self._init_and_start_app)
self.thread = threading.Thread(
target=self._init_and_start_app, kwargs=kwargs
)
self.thread.daemon = True # terminate when main thread exit
self.thread.start()

pyglet.clock.schedule_interval(self.on_update, 1 / 100)

def _init_and_start_app(self):
super(PygletThreadedImageViewer, self).__init__()
pyglet.app.run()

def imshow(self, image):
'''Update image on the viewer.
Parameters
----------
image: numpy.ndarray
The image to show on the viewer.
'''
imagedata = _ndarray_to_imagedata(image)
with self.lock:
if self.sprite is None:
Expand All @@ -45,6 +67,10 @@ def imshow(self, image):
self._next = False
break

def _init_and_start_app(self, **kwargs):
super(PygletThreadedImageViewer, self).__init__(**kwargs)
pyglet.app.run()

def on_draw(self):
self.clear()
self.sprite.draw()
Expand Down

0 comments on commit 21e7659

Please sign in to comment.