-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py.bak
executable file
·147 lines (114 loc) · 4.61 KB
/
plotting.py.bak
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
__author__ = "Panos Achlioptas"
import numpy as np
import matplotlib.pylab as plt
import matplotlib.cm as cm
import matplotlib.colors as mcolors
from matplotlib import transforms
import cv2
from PIL import Image
from . arrays import is_true
def stack_images_horizontally(file_names, save_file=None):
''' Opens the images corresponding to file_names and
creates a new image stacking them horizontally.
'''
images = map(Image.open, file_names)
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGBA', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]
if save_file is not None:
new_im.save(save_file)
return new_im
def stack_images_in_square_grid(file_names, save_file=None):
''' Opens the images corresponding to file_names and
creates a new grid-square image that plots them in individual cells.
The behavior is as expected when the sizes of the images are the same.
'''
images = map(Image.open, file_names)
widths, heights = zip(*(i.size for i in images))
max_width = max(widths)
max_height = max(heights)
n_images = len(images)
im_per_row = int(np.floor(np.sqrt(n_images)))
total_width = im_per_row * max_width
total_height = im_per_row * max_height
new_im = Image.new('RGBA', (total_width, total_height))
x_offset = 0
y_offset = 0
in_row = 0
for im in images:
if in_row == im_per_row:
y_offset += im.size[1]
x_offset = 0
in_row = 0
new_im.paste(im, (x_offset, y_offset))
x_offset += im.size[0]
in_row += 1
y_offset += im.size[1]
if save_file is not None:
new_im.save(save_file)
return new_im
def read_transparent_png(filename):
''' TODO: add docstring
SEE https://stackoverflow.com/questions/3803888/opencv-how-to-load-png-images-with-4-channels'''
image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
alpha_channel = image_4channel[:, :, 3]
rgb_channels = image_4channel[:, :, :3]
# White Background Image
white_background_image = np.ones_like(rgb_channels, dtype=np.uint8) * 255
# Alpha factor
alpha_factor = alpha_channel[:, :, np.newaxis].astype(np.float32) / 255.0
alpha_factor = np.concatenate((alpha_factor, alpha_factor, alpha_factor), axis=2)
# Transparent Image Rendered on White Background
base = rgb_channels.astype(np.float32) * alpha_factor
white = white_background_image.astype(np.float32) * (1 - alpha_factor)
final_image = base + white
return final_image.astype(np.uint8)
def rgb_to_hex_string(r, g, b):
all_ints = is_true.is_integer(r) and is_true.is_integer(g) and is_true.is_integer(b)
in_range = np.all(np.array([r, g, b]) <= 255) and np.all(np.array([r, g, b]) >= 0)
if not all_ints or not in_range:
raise ValueError('Expects integers in [0, 255]')
return "#{0:02x}{1:02x}{2:02x}".format(int(r), int(g), int(b))
def scalars_to_colors(float_vals, colormap='jet'):
cmap = cm.get_cmap(colormap)
mappable = cm.ScalarMappable(cmap=cmap)
colors = mappable.to_rgba(float_vals)
return colors
def colored_text(in_text, scores=None, colors=None, colormap='jet', for_html=False, space_char=' '):
if colors is None:
colors = scalars_to_colors(scores, colormap)
codes = []
for c in colors:
codes.append(mcolors.to_hex(c)) # color as hex.
res = ''
if for_html:
for token, code in zip(in_text, codes):
if len(res) > 0:
res += space_char # add space
res += '<span style="color:{};"> {} </span>'.format(code, token)
else:
res = codes
return res
def colored_text_to_figure(in_text, scores=None, colors=None, figsize=(10, 0.5), colormap='jet', **kw):
"""
Input: in_text: (list) of strings
scores: same size list/array of floats, if None: colors arguement must be not None.
colors: if not None, it will be used instead of scores.
"""
fig = plt.figure(frameon=False, figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
t = plt.gca().transData
if colors is None:
colors = scalars_to_colors(scores, colormap)
for token, col in zip(in_text, colors):
text = plt.text(0, 0, ' ' + token + ' ', color=col, transform=t, **kw)
text.draw(fig.canvas.get_renderer())
ex = text.get_window_extent()
t = transforms.offset_copy(text._transform, x=ex.width, units='dots')
return fig