[IMAGE-SIG] Image processing and editing

Andrew Kuchling amk@magnet.com
Thu, 31 Jul 1997 11:57:13 -0400 (EDT)


A faster way to change colour values in an image is to use the point()
method of an Image object, which takes a function (or a table) that's
applied to every pixel and return a new image that's been modified.
Let's say you want to change all pixels of value v1 to have value v2;
other pixels should be left alone.  Untested code to do that is:

def func(value, old_value=v1, new_value=v2):
	if value == old_value: return new_value
	else: return value

new_image = old_image.point(func)

This function won't be called for every single pixel; rather, PIL will
call it for all 256 possible values and construct a table, which will
then be quickly applied to the image.  

For your application, you should probably keep the original image
around unchanged and maintain a table, which gets modified by the
user's requests; the resulting new image would be displayed.
Presumably, if you change colour 1 to colour 2, and then change colour
2 to colour 3, you only want the second change to affect pixels that
were colour 2 in the original image, right?


	Andrew Kuchling
	amk@magnet.com
	http://people.magnet.com/%7Eamk/

_______________
IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________