PIL: Image to array of values
Greg Landrum
glandrum at my-deja.com
Fri Aug 4 10:58:30 EDT 2000
In article <398bbe05.19577390 at news.online.no>,
thomas at cintra.no (Thomas Weholt) wrote:
>
> I was wondering if I could use something like PIL to take a image in
> say 3 tones of gray and analyze the picture so that the value of a
> pixel corresponded to a value in a python array or list of tuples?
>
I'm not completely sure that I understand the question, but I think
this may help.
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.
I hope this helps. If not, feel free to send me mail.
-greg
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list