[Image-SIG] Undeliverable mail (fwd)

Les Schaffer godzilla@netmeg.net (Les Schaffer)
Wed, 17 Feb 1999 10:18:16 -0500 (EST)


Fred sed:

> Moving the multiarray stuff into the Python code would help a lot...

you can say that again.

hows bout "the tip" from the best pythoneer tipster around (and PIL
author): whats the best way (in the mean time) to jam an (NumPy) array
of pixel data into an image, and whats the best way to grab an (NumPy)
array of pixel data out of an image?

somewhere or other i got a copy of a file with this in it
(below). look good to you?

also, documentations on getdata() would be greatly appreciated; for
example:

getdata(). Returns the contents of a the image as a sequence object
containing pixel values. The sequence object is flattened, so that
values for line one follows directly after the values for line zero,
and so on.

---------
import Image
im = Image.open("c0368974139r.gif")
#im.show()

dat = im.getdata()

print dat
# <ImagingCore object at 8141798>

print dir(dat)

# ['chop_add', 'chop_add_modulo', 'chop_and', 'chop_darker',
# 'chop_difference', 'chop_invert', 'chop_lighter', 'chop_multiply',
# 'chop_or', 'chop_screen', 'chop_subtract', 'chop_subtract_modulo',
# 'chop_xor', 'convert', 'convert2', 'convert_matrix', 'copy',
# 'copy2', 'crop', 'draw_arc', 'draw_chord', 'draw_ellipse', 
....

------------

errrr wheres the DATA??????????


les schaffer





==================== found this somewhere on the net ===============

#!/usr/bin/python -O

import Image
def normalize(a):
    a = array(a, copy=1)
    a =1.0-(a - min(a.flat))/(max(a.flat) - min(a.flat)) 
    return a

def ArrayToImage(a, height=200, scale=1):
    if len(a.shape) == 2:
	a = (normalize(a)*254).astype('b')
	i = Image.new("L", (a.shape[1], a.shape[0]), color=255)
	i.fromstring(a.tostring())
    elif len(a.shape) == 1:
	a = 1-normalize(a)
	a = array(a*height).astype('i')
	i = Image.new("L", (a.shape[0], height), color=255)
	id = ImageDraw.ImageDraw(i)
	id.setink(0)
	def map_xandy_to_xy(x, y):
	    import _imaging
	    return _imaging.path(ravel(transpose(array((x, y)))).tolist())
	id.line(xy=map_xandy_to_xy(arange(a.shape[0]), a))
    if scale != 1:
	i = i.resize((i.size[0]*scale, i.size[1]*scale))
    return i

def ImageToArray(i):
    a = fromstring(i.tostring(), 'b')
    a.shape = i.im.size[1], i.im.size[0]
    return a


-- 
____        Les Schaffer              ___| --->> Engineering R&D <<---
Theoretical & Applied Mechanics          |  Designspring, Inc. 
Center for Radiophysics & Space Research |  http://www.designspring.com/
Cornell Univ.  schaffer@tam.cornell.edu  |  les@designspring.com