Fastest way to tint an image with PIL

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Nov 7 09:19:37 CET 2008


On Thu, 06 Nov 2008 13:08:59 -0800, Dan Moskowitz wrote:

> I'm using PIL to tint and composite images together.  Here's how I'm
> currently tinting images, it's really slow and I know there's got to be
> a better way:
> 
> def TintImage( im, tintColor ):
> 	tint = (tintColor[0]/255.0, tintColor[1]/255.0, tintColor
[2]/255.0,
> tintColor[3]/255.0)
> 	pix = im.load()
> 	for x in xrange( im.size[0] ):
> 		for y in xrange( im.size[1] ):
> 			c = pix[x,y]
> 			pix[x,y] = (int(c[0]*tint[0]), int(c[1]*tint[1]),
> int(c[2]*tint[2]), c[3])
> 
> I thought maybe there's a way to do it using the transform method, but I
> haven't figure it out yet.  Anyone?

from PIL import Image, ImageChops

def tint_image(image, tint_color):
    return ImageChops.multiply(image,
                               Image.new('RGB', image.size, tint_color))

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list