annotate pycaptcha/transformations.py @ 132:25b7c1f20949

Adapted pycaptcha to get fonts in /Tmp/allfonts local folder
author boulanni <nicolas_boulanger@hotmail.com>
date Sat, 20 Feb 2010 02:06:38 -0500
parents 4775b4195b4b
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())