49
|
1 #!/usr/bin/python
|
|
2
|
|
3 import numpy
|
|
4 import Image
|
|
5 from image_tiling import tile_raster_images
|
|
6 import pylab
|
|
7 import time
|
|
8
|
|
9 class Visualizer():
|
|
10 def __init__(self, num_columns=10, image_size=(32,32), to_dir=None, on_screen=False):
|
|
11 self.list = []
|
|
12 self.image_size = image_size
|
|
13 self.num_columns = num_columns
|
|
14
|
|
15 self.on_screen = on_screen
|
|
16 self.to_dir = to_dir
|
|
17
|
|
18 self.cur_grid_image = None
|
|
19
|
|
20 self.cur_index = 0
|
|
21
|
|
22 def visualize_stop_and_flush(self):
|
|
23 self.make_grid_image()
|
|
24
|
|
25 if self.on_screen:
|
|
26 self.visualize()
|
|
27 if self.to_dir:
|
|
28 self.dump_to_disk()
|
|
29
|
|
30 self.stop_and_wait()
|
|
31 self.flush()
|
|
32
|
|
33 self.cur_index += 1
|
|
34
|
|
35 def make_grid_image(self):
|
|
36 num_rows = len(self.list) / self.num_columns
|
|
37 if len(self.list) % self.num_columns != 0:
|
|
38 num_rows += 1
|
|
39 grid_shape = (num_rows, self.num_columns)
|
|
40 self.cur_grid_image = tile_raster_images(numpy.array(self.list), self.image_size, grid_shape, tile_spacing=(5,5), output_pixel_vals=False)
|
|
41
|
|
42 def visualize(self):
|
|
43 pylab.imshow(self.cur_grid_image)
|
|
44 pylab.draw()
|
|
45
|
|
46 def dump_to_disk(self):
|
|
47 gi = Image.fromarray((self.cur_grid_image * 255).astype('uint8'), "L")
|
|
48 gi.save(self.to_dir + "/grid_" + str(self.cur_index) + ".png")
|
|
49
|
|
50 def stop_and_wait(self):
|
|
51 # can't raw_input under gimp, so sleep)
|
|
52 print "New image generated, sleeping 5 secs"
|
|
53 time.sleep(5)
|
|
54
|
|
55 def flush(self):
|
|
56 self.list = []
|
|
57
|
|
58 def get_parameters_names(self):
|
|
59 return []
|
|
60
|
|
61 def regenerate_parameters(self):
|
|
62 return []
|
|
63
|
|
64 def after_transform_callback(self, image):
|
|
65 self.transform_image(image)
|
|
66
|
|
67 def end_transform_callback(self, final_image):
|
|
68 self.visualize_stop_and_flush()
|
|
69
|
|
70 def transform_image(self, image):
|
|
71 sz = self.image_size
|
|
72 self.list.append(image.copy().reshape((sz[0] * sz[1])))
|
|
73
|