Mercurial > ift6266
comparison data_generation/transformations/pycaptcha/transformations.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | pycaptcha/transformations.py@4775b4195b4b |
children |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 | |
2 import Numeric, Image | |
3 #""" Transforme une image PIL en objet numpy.array et vice versa""" | |
4 | |
5 | |
6 def image2array(im): | |
7 #""" image vers array numpy""" | |
8 if im.mode not in ("L", "F"): | |
9 raise ValueError, "can only convert single-layer images" | |
10 if im.mode == "L": | |
11 a = Numeric.fromstring(im.tostring(), Numeric.UnsignedInt8) | |
12 else: | |
13 a = Numeric.fromstring(im.tostring(), Numeric.Float32) | |
14 a.shape = im.size[1], im.size[0] | |
15 return a | |
16 | |
17 def array2image(a): | |
18 #""" array numpy vers image""" | |
19 if a.typecode() == Numeric.UnsignedInt8: | |
20 mode = "L" | |
21 elif a.typecode() == Numeric.Float32: | |
22 mode = "F" | |
23 else: | |
24 raise ValueError, "unsupported image mode" | |
25 return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring()) |