File size: 1,075 Bytes
8870024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt


_figures = {}


def show(im, waittime=0, id='plt', max_width=600):
    plt.ion()
    w = min(im.shape[1], max_width)
    h = max_width * (1.0 * im.shape[0]) / im.shape[1] if w == max_width else im.shape[0]
    plt.figure(id, figsize=(w / 80, h / 80), dpi=80)

    ax = plt.axes([0, 0, 1, 1], frameon=False)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    if np.issubdtype(im.dtype, np.floating):
        if np.max(im) > 1:
            factor = 255 / np.max(im)
        else:
            factor = 255
    else:
        factor = 1

    if np.atleast_3d(im).shape[2] == 3:
        data = np.uint8(im * factor)[:, :, ::-1]
    else:
        data = np.uint8(np.dstack((im, im, im)) * factor)

    if id in _figures and plt.fignum_exists(id):
        _figures[id].set_array(data)
    else:
        _figures[id] = plt.imshow(data)

    if waittime == 0:
        plt.waitforbuttonpress()
    else:
        plt.pause(waittime / 1000.)
    plt.ioff()