Mercurial > ift6266
view data_generation/transformations/pycaptcha/transformations.py @ 236:7be1f086a89e
added __init__.py to allow module loading of baseline
author | Myriam Cote <cotemyri@iro.umontreal.ca> |
---|---|
date | Mon, 15 Mar 2010 09:22:52 -0400 |
parents | 1f5937e9e530 |
children |
line wrap: on
line source
import Numeric, Image #""" Transforme une image PIL en objet numpy.array et vice versa""" def image2array(im): #""" image vers array numpy""" if im.mode not in ("L", "F"): raise ValueError, "can only convert single-layer images" if im.mode == "L": a = Numeric.fromstring(im.tostring(), Numeric.UnsignedInt8) else: a = Numeric.fromstring(im.tostring(), Numeric.Float32) a.shape = im.size[1], im.size[0] return a def array2image(a): #""" array numpy vers image""" if a.typecode() == Numeric.UnsignedInt8: mode = "L" elif a.typecode() == Numeric.Float32: mode = "F" else: raise ValueError, "unsupported image mode" return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring())