Mercurial > pylearn
annotate pylearn/io/image_tiling.py @ 1473:91a475ca9b6d
image_tiling with better scaling for integer inputs
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 18 May 2011 10:52:22 -0400 |
parents | 9d941cd77479 |
children |
rev | line source |
---|---|
652 | 1 """ |
2 Illustrate filters (or data) in a grid of small image-shaped tiles. | |
3 """ | |
4 | |
5 import numpy | |
6 from PIL import Image | |
7 | |
753
0eee6693f149
better numeric stability in tile_image
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
652
diff
changeset
|
8 def scale_to_unit_interval(ndar,eps=1e-8): |
652 | 9 ndar = ndar.copy() |
10 ndar -= ndar.min() | |
969
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
11 ndar *= 1.0 / max(ndar.max(),eps) |
652 | 12 return ndar |
13 | |
1448 | 14 def tile_raster_images(X, img_shape, |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
15 tile_shape=None, tile_spacing=(1,1), |
1448 | 16 scale_rows_to_unit_interval=True, |
969
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
17 output_pixel_vals=True, |
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
18 min_dynamic_range=1e-4, |
652 | 19 ): |
20 """ | |
21 Transform an array with one flattened image per row, into an array in which images are | |
22 reshaped and layed out like tiles on a floor. | |
23 | |
24 This function is useful for visualizing datasets whose rows are images, and also columns of | |
25 matrices for transforming those rows (such as the first layer of a neural net). | |
26 | |
27 :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can be 2-D ndarrays or None | |
28 :param X: a 2-D array in which every row is a flattened image. | |
29 :type img_shape: tuple; (height, width) | |
30 :param img_shape: the original shape of each image | |
31 :type tile_shape: tuple; (rows, cols) | |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
32 :param tile_shape: the number of images to tile (rows, cols) (Defaults to a square-ish |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
33 shape with the right area for the number of images) |
969
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
34 :type min_dynamic_range: positive float |
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
35 :param min_dynamic_range: the dynamic range of each image is used in scaling to the unit |
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
36 interval, but images with less dynamic range than this will be scaled as if this were |
bc22f739b54c
image_tiling - added dynamic_range parameter to avoid amplifying noise
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
964
diff
changeset
|
37 the dynamic range. |
652 | 38 |
39 :returns: array suitable for viewing as an image. (See:`PIL.Image.fromarray`.) | |
40 :rtype: a 2-d array with same dtype as X. | |
41 | |
42 """ | |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
43 # This is premature when tile_slices_to_image is not documented at all yet, |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
44 # but ultimately true: |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
45 #print >> sys.stderr, "WARN: tile_raster_images sucks, use tile_slices_to_image" |
1449
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
46 if len(img_shape)==3 and img_shape[2]==3: |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
47 # make this save an rgb image |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
48 if scale_rows_to_unit_interval: |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
49 print >> sys.stderr, "WARN: tile_raster_images' scaling routine messes up colour - try tile_slices_to_image" |
1449
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
50 return tile_raster_images( |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
51 (X[:,0::3], X[:,1::3], X[:,2::3], None), |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
52 img_shape=img_shape[:2], |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
53 tile_shape=tile_shape, |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
54 tile_spacing=tile_spacing, |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
55 scale_rows_to_unit_interval=scale_rows_to_unit_interval, |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
56 output_pixel_vals=output_pixel_vals, |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
57 min_dynamic_range=min_dynamic_range) |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
58 |
22f48914b3f8
Make tile_raster_images deal with a 3-tuple image shape as RGB spec.
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1448
diff
changeset
|
59 if isinstance(X, tuple): |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
60 n_images_in_x = X[0].shape[0] |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
61 else: |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
62 n_images_in_x = X.shape[0] |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
63 |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
64 if tile_shape is None: |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
65 tile_shape = most_square_shape(n_images_in_x) |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
66 |
652 | 67 assert len(img_shape) == 2 |
68 assert len(tile_shape) == 2 | |
69 assert len(tile_spacing) == 2 | |
70 | |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
71 #out_shape is the shape in pixels of the returned image array |
652 | 72 out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp |
73 in zip(img_shape, tile_shape, tile_spacing)] | |
74 | |
75 if isinstance(X, tuple): | |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
76 if scale_rows_to_unit_interval: |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
77 raise NotImplementedError() |
652 | 78 assert len(X) == 4 |
79 if output_pixel_vals: | |
80 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8') | |
81 else: | |
82 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype=X.dtype) | |
83 | |
84 #colors default to 0, alpha defaults to 1 (opaque) | |
85 if output_pixel_vals: | |
86 channel_defaults = [0,0,0,255] | |
87 else: | |
88 channel_defaults = [0.,0.,0.,1.] | |
89 | |
90 for i in xrange(4): | |
91 if X[i] is None: | |
92 out_array[:,:,i] = numpy.zeros(out_shape, | |
93 dtype='uint8' if output_pixel_vals else out_array.dtype | |
94 )+channel_defaults[i] | |
95 else: | |
96 out_array[:,:,i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals) | |
97 return out_array | |
98 | |
99 else: | |
100 H, W = img_shape | |
101 Hs, Ws = tile_spacing | |
102 | |
1473
91a475ca9b6d
image_tiling with better scaling for integer inputs
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1457
diff
changeset
|
103 out_scaling = 1 |
91a475ca9b6d
image_tiling with better scaling for integer inputs
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1457
diff
changeset
|
104 if output_pixel_vals and str(X.dtype).startswith('float'): |
91a475ca9b6d
image_tiling with better scaling for integer inputs
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1457
diff
changeset
|
105 out_scaling = 255 |
91a475ca9b6d
image_tiling with better scaling for integer inputs
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1457
diff
changeset
|
106 |
652 | 107 out_array = numpy.zeros(out_shape, dtype='uint8' if output_pixel_vals else X.dtype) |
108 for tile_row in xrange(tile_shape[0]): | |
109 for tile_col in xrange(tile_shape[1]): | |
110 if tile_row * tile_shape[1] + tile_col < X.shape[0]: | |
111 if scale_rows_to_unit_interval: | |
983
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
112 try: |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
113 this_img = scale_to_unit_interval( |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
114 X[tile_row * tile_shape[1] + tile_col].reshape(img_shape), |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
115 eps=min_dynamic_range) |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
116 except ValueError: |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
117 raise ValueError('Failed to reshape array of shape %s to shape %s' |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
118 % ( |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
119 X[tile_row*tile_shape[1] + tile_col].shape |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
120 , img_shape |
15371ff780a0
image-tiling - better error reporting
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
970
diff
changeset
|
121 )) |
652 | 122 else: |
123 this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape) | |
124 out_array[ | |
125 tile_row * (H+Hs):tile_row*(H+Hs)+H, | |
126 tile_col * (W+Ws):tile_col*(W+Ws)+W | |
127 ] \ | |
1473
91a475ca9b6d
image_tiling with better scaling for integer inputs
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1457
diff
changeset
|
128 = this_img * out_scaling |
652 | 129 return out_array |
130 | |
131 | |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
132 def most_square_shape(N): |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
133 """rectangle (height, width) with area N that is closest to sqaure |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
134 """ |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
135 for i in xrange(int(numpy.sqrt(N)),0, -1): |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
136 if 0 == N % i: |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
137 return (i, N/i) |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
138 |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
139 def save_tiled_raster_images(tiled_img, filename): |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
140 """Save a a return value from `tile_raster_images` to `filename`. |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
141 |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
142 Returns the PIL image that was saved |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
143 """ |
964
6a778bca0dec
fixed saving in image_tiling.py to work for greyscale and colour images
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
950
diff
changeset
|
144 if tiled_img.ndim==2: |
6a778bca0dec
fixed saving in image_tiling.py to work for greyscale and colour images
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
950
diff
changeset
|
145 img = Image.fromarray( tiled_img, 'L') |
970
930b92f88e61
typo in image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
969
diff
changeset
|
146 elif tiled_img.ndim==3: |
930b92f88e61
typo in image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
969
diff
changeset
|
147 img = Image.fromarray(tiled_img, 'RGBA') |
964
6a778bca0dec
fixed saving in image_tiling.py to work for greyscale and colour images
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
950
diff
changeset
|
148 else: |
6a778bca0dec
fixed saving in image_tiling.py to work for greyscale and colour images
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
950
diff
changeset
|
149 raise TypeError('bad ndim', tiled_img) |
6a778bca0dec
fixed saving in image_tiling.py to work for greyscale and colour images
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
950
diff
changeset
|
150 |
950
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
151 img.save(filename) |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
152 return img |
faa658da89c2
tweaks to image_tiling
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
753
diff
changeset
|
153 |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
154 def tile_slices_to_image_uint8(X, tile_shape=None): |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
155 if str(X.dtype) != 'uint8': |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
156 raise TypeError(X) |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
157 if tile_shape is None: |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
158 #how many tile rows and cols |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
159 (TR, TC) = most_square_shape(X.shape[0]) |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
160 H, W = X.shape[1], X.shape[2] |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
161 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
162 Hs = H+1 #spacing between tiles |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
163 Ws = W+1 #spacing between tiles |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
164 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
165 trows, tcols= most_square_shape(X.shape[0]) |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
166 outrows = trows * Hs - 1 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
167 outcols = tcols * Ws - 1 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
168 out = numpy.zeros((outrows, outcols,3), dtype='uint8') |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
169 tr_stride= 1+X.shape[1] |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
170 for tr in range(trows): |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
171 for tc in range(tcols): |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
172 Xrc = X[tr*tcols+tc] |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
173 if Xrc.ndim==2: # if no color channel make it broadcast |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
174 Xrc=Xrc[:,:,None] |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
175 #print Xrc.shape |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
176 #print out[tr*Hs:tr*Hs+H,tc*Ws:tc*Ws+W].shape |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
177 out[tr*Hs:tr*Hs+H,tc*Ws:tc*Ws+W] = Xrc |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
178 img = Image.fromarray(out, 'RGB') |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
179 return img |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
180 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
181 def tile_slices_to_image(X, |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
182 tile_shape=None, |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
183 scale_each=True, |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
184 min_dynamic_range=1e-4): |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
185 #always returns an RGB image |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
186 def scale_0_255(x): |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
187 xmin = x.min() |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
188 xmax = x.max() |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
189 return numpy.asarray( |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
190 255 * (x - xmin) / max(xmax - xmin, min_dynamic_range), |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
191 dtype='uint8') |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
192 |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
193 if scale_each: |
1457
9d941cd77479
fixed bug in tile_slice
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1456
diff
changeset
|
194 uintX = numpy.empty(X.shape, dtype='uint8') |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
195 for i, Xi in enumerate(X): |
1457
9d941cd77479
fixed bug in tile_slice
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1456
diff
changeset
|
196 uintX[i] = scale_0_255(Xi) |
9d941cd77479
fixed bug in tile_slice
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1456
diff
changeset
|
197 X = uintX |
1456
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
198 else: |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
199 X = scale_0_255(X) |
272879b84d30
added io/image_tiling:tile_slices_to_image which is a better version of tile_raster_*
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1449
diff
changeset
|
200 return tile_slices_to_image_uint8(X, tile_shape=tile_shape) |