annotate data_generation/transformations/pycaptcha/transformations.py @ 578:61aae4fd2da5

typo fixed, uploaded to CMT
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Sun, 08 Aug 2010 08:16:21 -0400
parents 1f5937e9e530
children
rev   line source
87
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
1
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
2 import Numeric, Image
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
3 #""" Transforme une image PIL en objet numpy.array et vice versa"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
4
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
5
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
6 def image2array(im):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
7 #""" image vers array numpy"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
8 if im.mode not in ("L", "F"):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
9 raise ValueError, "can only convert single-layer images"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
10 if im.mode == "L":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
11 a = Numeric.fromstring(im.tostring(), Numeric.UnsignedInt8)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
12 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
13 a = Numeric.fromstring(im.tostring(), Numeric.Float32)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
14 a.shape = im.size[1], im.size[0]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
15 return a
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
16
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
17 def array2image(a):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
18 #""" array numpy vers image"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
19 if a.typecode() == Numeric.UnsignedInt8:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
20 mode = "L"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
21 elif a.typecode() == Numeric.Float32:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
22 mode = "F"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
23 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
24 raise ValueError, "unsupported image mode"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
25 return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring())