| 1 |
import sys |
| 2 |
import types |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
import PIL.Image |
| 6 |
|
| 7 |
from ... import utils |
| 8 |
from .base import check_pyglet_available |
| 9 |
from .pyglet_run import pyglet_run |
| 10 |
|
| 11 |
|
| 12 |
def pyglet_imshow(image, caption=None, interval=0.5, keymap=None, hook=None, run=False): |
| 13 |
"""Show image with pyglet. |
| 14 |
|
| 15 |
Parameters |
| 16 |
---------- |
| 17 |
image: numpy.ndarray or list of numpy.ndarray or iterator of numpy.array |
| 18 |
Image or images to show. |
| 19 |
caption: str, optional |
| 20 |
Caption for pyglet window. |
| 21 |
interval: float, optional |
| 22 |
Interval for list or iterator of images. Default is 0.5. |
| 23 |
keymap: dict, optional |
| 24 |
Key mappings for key and function. |
| 25 |
hook: callable, optional |
| 26 |
Hook function called after each image update. |
| 27 |
run: bool, optional |
| 28 |
If True, pyglet_run is called after imshow. Default is False. |
| 29 |
|
| 30 |
Returns |
| 31 |
------- |
| 32 |
None |
| 33 |
|
| 34 |
""" |
| 35 |
if isinstance(image, types.GeneratorType): |
| 36 |
_pyglet_imshow_generator( |
| 37 |
image, |
| 38 |
caption=caption, |
| 39 |
interval=interval, |
| 40 |
keymap=keymap, |
| 41 |
hook=hook, |
| 42 |
) |
| 43 |
elif isinstance(image, np.ndarray): |
| 44 |
_pyglet_imshow_ndarray(image, caption=caption, keymap=keymap, hook=hook) |
| 45 |
else: |
| 46 |
_pyglet_imshow_list( |
| 47 |
image, |
| 48 |
caption=caption, |
| 49 |
interval=interval, |
| 50 |
keymap=keymap, |
| 51 |
hook=hook, |
| 52 |
) |
| 53 |
|
| 54 |
if run: |
| 55 |
pyglet_run() |
| 56 |
|
| 57 |
|
| 58 |
def _pyglet_imshow_list(images, caption=None, interval=0.5, keymap=None, hook=None): |
| 59 |
pyglet = check_pyglet_available() |
| 60 |
|
| 61 |
def usage(): |
| 62 |
print("Usage: ", file=sys.stderr) |
| 63 |
if keymap is not None: |
| 64 |
for key, function in keymap.items(): |
| 65 |
print(" {}: {}".format(key, function)) |
| 66 |
print(" h: show help", file=sys.stderr) |
| 67 |
print(" q: close window", file=sys.stderr) |
| 68 |
print(" n: next image", file=sys.stderr) |
| 69 |
print(" p: previous image", file=sys.stderr) |
| 70 |
print(" s: toggle play", file=sys.stderr) |
| 71 |
|
| 72 |
usage() |
| 73 |
|
| 74 |
if isinstance(images[0], np.ndarray): |
| 75 |
max_image_height, max_image_width = np.max( |
| 76 |
[image.shape[:2] for image in images], axis=0 |
| 77 |
) |
| 78 |
elif isinstance(images[0], PIL.Image.Image): |
| 79 |
max_image_width, max_image_height = np.max( |
| 80 |
[image.size for image in images], axis=0 |
| 81 |
) |
| 82 |
elif hook is None: |
| 83 |
raise ValueError( |
| 84 |
"hook must be specified when images is not list of " |
| 85 |
"numpy.ndarray or PIL.Image.Image" |
| 86 |
) |
| 87 |
else: |
| 88 |
image = hook(images[0]) |
| 89 |
if not isinstance(image, (np.ndarray, PIL.Image.Image)): |
| 90 |
raise TypeError("hook must return numpy.ndarray or PIL.Image.Image") |
| 91 |
max_image_height, max_image_width = image.shape[:2] |
| 92 |
aspect_ratio = max_image_width / max_image_height |
| 93 |
|
| 94 |
index = 0 |
| 95 |
|
| 96 |
window = _initialize_window(caption=caption, aspect_ratio=aspect_ratio) |
| 97 |
window.index = index |
| 98 |
window.play = False |
| 99 |
|
| 100 |
image = _convert_to_imagedata( |
| 101 |
hook(images[window.index]) if hook else images[window.index] |
| 102 |
) |
| 103 |
sprite = pyglet.sprite.Sprite(image) |
| 104 |
|
| 105 |
def _post_image_update(): |
| 106 |
_centerize_sprite_in_window(sprite, window) |
| 107 |
window.set_caption( |
| 108 |
"{} {}/{}".format(images[window.index], window.index + 1, len(images)) |
| 109 |
) |
| 110 |
print( |
| 111 |
"{} {}/{}".format(images[window.index], window.index + 1, len(images)), |
| 112 |
file=sys.stderr, |
| 113 |
) |
| 114 |
|
| 115 |
_post_image_update() |
| 116 |
|
| 117 |
@window.event |
| 118 |
def on_draw(): |
| 119 |
window.clear() |
| 120 |
sprite.draw() |
| 121 |
|
| 122 |
def play_callback(dt): |
| 123 |
if window.play: |
| 124 |
if window.index == len(images) - 1: |
| 125 |
print("Press 'q' to quit", file=sys.stderr) |
| 126 |
window.play = False |
| 127 |
window.index = min(window.index + 1, len(images) - 1) |
| 128 |
sprite.image = _convert_to_imagedata( |
| 129 |
hook(images[window.index]) if hook else images[window.index] |
| 130 |
) |
| 131 |
_centerize_sprite_in_window(sprite, window) |
| 132 |
|
| 133 |
pyglet.clock.schedule_interval(play_callback, interval) |
| 134 |
|
| 135 |
@window.event |
| 136 |
def on_key_press(symbol, modifiers): |
| 137 |
if keymap is not None: |
| 138 |
for key, function in keymap.items(): |
| 139 |
if symbol == getattr(pyglet.window.key, key.upper()): |
| 140 |
function(images, window.index) |
| 141 |
return |
| 142 |
if symbol == pyglet.window.key.Q: |
| 143 |
window.close() |
| 144 |
elif symbol == pyglet.window.key.N: |
| 145 |
if window.index + 1 <= len(images) - 1: |
| 146 |
window.index += 1 |
| 147 |
sprite.image = _convert_to_imagedata( |
| 148 |
hook(images[window.index]) if hook else images[window.index] |
| 149 |
) |
| 150 |
_post_image_update() |
| 151 |
elif symbol == pyglet.window.key.P: |
| 152 |
if window.index - 1 >= 0: |
| 153 |
window.index -= 1 |
| 154 |
sprite.image = _convert_to_imagedata( |
| 155 |
hook(images[window.index]) if hook else images[window.index] |
| 156 |
) |
| 157 |
_post_image_update() |
| 158 |
elif symbol == pyglet.window.key.S: |
| 159 |
window.play = not window.play |
| 160 |
elif symbol == pyglet.window.key.H: |
| 161 |
usage() |
| 162 |
else: |
| 163 |
print("Press 'h' to show help", file=sys.stderr) |
| 164 |
|
| 165 |
|
| 166 |
def _pyglet_imshow_generator( |
| 167 |
images, caption=None, interval=0.5, keymap=None, hook=None |
| 168 |
): |
| 169 |
if keymap is not None: |
| 170 |
raise NotImplementedError |
| 171 |
|
| 172 |
pyglet = check_pyglet_available() |
| 173 |
|
| 174 |
image = hook(next(images)) if hook else images |
| 175 |
if not isinstance(image, (np.ndarray, PIL.Image.Image)): |
| 176 |
raise TypeError("hook must return numpy.ndarray or PIL.Image.Image") |
| 177 |
|
| 178 |
aspect_ratio = image.shape[1] / image.shape[0] |
| 179 |
window = _initialize_window(caption=caption, aspect_ratio=aspect_ratio) |
| 180 |
|
| 181 |
image = _convert_to_imagedata(image) |
| 182 |
sprite = pyglet.sprite.Sprite(image) |
| 183 |
_centerize_sprite_in_window(sprite, window) |
| 184 |
|
| 185 |
window.play = False |
| 186 |
|
| 187 |
@window.event |
| 188 |
def on_draw(): |
| 189 |
window.clear() |
| 190 |
sprite.draw() |
| 191 |
|
| 192 |
def usage(): |
| 193 |
print("Usage: ", file=sys.stderr) |
| 194 |
print(" h: show help", file=sys.stderr) |
| 195 |
print(" q: close window", file=sys.stderr) |
| 196 |
print(" n: next image", file=sys.stderr) |
| 197 |
print(" p: previous image", file=sys.stderr) |
| 198 |
print(" s: toggle play", file=sys.stderr) |
| 199 |
|
| 200 |
def play_callback(dt): |
| 201 |
if window.play: |
| 202 |
try: |
| 203 |
sprite.image = _convert_to_imagedata(next(images)) |
| 204 |
_centerize_sprite_in_window(sprite, window) |
| 205 |
except StopIteration: |
| 206 |
print("Press 'q' to quit", file=sys.stderr) |
| 207 |
window.play = False |
| 208 |
|
| 209 |
pyglet.clock.schedule_interval(play_callback, interval) |
| 210 |
|
| 211 |
@window.event() |
| 212 |
def on_key_press(symbol, modifiers): |
| 213 |
if symbol == pyglet.window.key.Q: |
| 214 |
window.close() |
| 215 |
elif symbol == pyglet.window.key.N: |
| 216 |
try: |
| 217 |
sprite.image = _convert_to_imagedata( |
| 218 |
hook(next(images)) if hook else next(images) |
| 219 |
) |
| 220 |
_centerize_sprite_in_window(sprite, window) |
| 221 |
except StopIteration: |
| 222 |
print("Press 'q' to quit", file=sys.stderr) |
| 223 |
elif symbol == pyglet.window.key.S: |
| 224 |
window.play = not window.play |
| 225 |
elif symbol == pyglet.window.key.H: |
| 226 |
usage() |
| 227 |
else: |
| 228 |
print("Press 'h' to show help", file=sys.stderr) |
| 229 |
|
| 230 |
|
| 231 |
def _pyglet_imshow_ndarray(image, caption=None, keymap=None, hook=None): |
| 232 |
if keymap is not None: |
| 233 |
raise NotImplementedError |
| 234 |
|
| 235 |
pyglet = check_pyglet_available() |
| 236 |
|
| 237 |
image = hook(image) if hook else image |
| 238 |
if not isinstance(image, (np.ndarray, PIL.Image.Image)): |
| 239 |
raise TypeError("hook must return numpy.ndarray or PIL.Image.Image") |
| 240 |
aspect_ratio = image.shape[1] / image.shape[0] |
| 241 |
window = _initialize_window(caption=caption, aspect_ratio=aspect_ratio) |
| 242 |
|
| 243 |
image = _convert_to_imagedata(image) |
| 244 |
sprite = pyglet.sprite.Sprite(image) |
| 245 |
_centerize_sprite_in_window(sprite, window) |
| 246 |
|
| 247 |
@window.event |
| 248 |
def on_draw(): |
| 249 |
window.clear() |
| 250 |
sprite.draw() |
| 251 |
|
| 252 |
def usage(): |
| 253 |
print("Usage: ", file=sys.stderr) |
| 254 |
print(" h: show help", file=sys.stderr) |
| 255 |
print(" q: close window", file=sys.stderr) |
| 256 |
|
| 257 |
@window.event() |
| 258 |
def on_key_press(symbol, modifiers): |
| 259 |
if symbol == pyglet.window.key.Q: |
| 260 |
window.close() |
| 261 |
elif symbol == pyglet.window.key.H: |
| 262 |
usage() |
| 263 |
else: |
| 264 |
print("Press 'h' to show help", file=sys.stderr) |
| 265 |
|
| 266 |
|
| 267 |
def _initialize_window(caption, aspect_ratio): |
| 268 |
pyglet = check_pyglet_available() |
| 269 |
|
| 270 |
display = pyglet.canvas.Display() |
| 271 |
screen = display.get_default_screen() |
| 272 |
|
| 273 |
max_window_width = int(round(screen.width * 0.75)) |
| 274 |
max_window_height = int(round(screen.height * 0.75)) |
| 275 |
|
| 276 |
|
| 277 |
if aspect_ratio > 1: |
| 278 |
window_width = max_window_width |
| 279 |
window_height = int(round(window_width / aspect_ratio)) |
| 280 |
else: |
| 281 |
window_height = max_window_height |
| 282 |
window_width = int(round(window_height * aspect_ratio)) |
| 283 |
|
| 284 |
|
| 285 |
if window_width > max_window_width: |
| 286 |
window_width = max_window_width |
| 287 |
window_height = int(round(window_width / aspect_ratio)) |
| 288 |
if window_height > max_window_height: |
| 289 |
window_height = max_window_height |
| 290 |
window_width = int(round(window_height * aspect_ratio)) |
| 291 |
|
| 292 |
window = pyglet.window.Window( |
| 293 |
width=window_width, |
| 294 |
height=window_height, |
| 295 |
caption=caption, |
| 296 |
) |
| 297 |
return window |
| 298 |
|
| 299 |
|
| 300 |
def _centerize_sprite_in_window(sprite, window): |
| 301 |
scale_x = 0.95 * window.width / sprite.image.width |
| 302 |
scale_y = 0.95 * window.height / sprite.image.height |
| 303 |
scale = min(scale_x, scale_y) |
| 304 |
|
| 305 |
width = sprite.image.width * scale |
| 306 |
height = sprite.image.height * scale |
| 307 |
x = (window.width - width) / 2.0 |
| 308 |
y = (window.height - height) / 2.0 |
| 309 |
|
| 310 |
sprite.update(x=x, y=y, scale=scale) |
| 311 |
|
| 312 |
|
| 313 |
def _convert_to_imagedata(image): |
| 314 |
pyglet = check_pyglet_available() |
| 315 |
|
| 316 |
if isinstance(image, np.ndarray): |
| 317 |
image = utils.numpy_to_pillow(image) |
| 318 |
elif isinstance(image, PIL.Image.Image): |
| 319 |
pass |
| 320 |
else: |
| 321 |
raise ValueError |
| 322 |
|
| 323 |
image = image.convert("RGB") |
| 324 |
|
| 325 |
kwargs = dict( |
| 326 |
width=image.width, |
| 327 |
height=image.height, |
| 328 |
data=image.tobytes(), |
| 329 |
pitch=-image.width * len(image.mode), |
| 330 |
) |
| 331 |
if hasattr(pyglet, "__version__") and pyglet.__version__[0] == "2": |
| 332 |
kwargs["fmt"] = image.mode |
| 333 |
else: |
| 334 |
kwargs["format"] = image.mode |
| 335 |
image = pyglet.image.ImageData(**kwargs) |
| 336 |
return image |