These are code and comments from the code above:
It is straightforward to convert Numeric arrays into PIL images:
#--------------------
from Numeric import *
from Pil import Image
# construct an arbitrary Numeric matrix
dims = (256,256)
arr = zeros(dims,Int8)
for i in xrange(dims[0]):
arr[i,i] = 255
# and convert it to an Image
img = Image.fromstring('L',dims,arr.tostring())
img.save('foo.gif')
#--------------------
or to convert from a PIL image back to a Numeric array:
#--------------------
from Numeric import *
from Pil import Image
newImg = Image.open('foo.gif')
newArr = fromstring(newImg.tostring(),Int8)
newArr = reshape(newArr,newImg.size)
#-------------------
If you are constructing images from Numeric arrays, don't forget that
the Image values should run from 0-255, so you'll need to scale your
data in the Numeric array before creating the image.
Nessun commento:
Posta un commento