By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,510 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,510 IT Pros & Developers. It's quick & easy.

Fastest way to tint an image with PIL

P: n/a
Hi,

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?

Thanks
-Dan
Nov 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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?
Too lazy to read the manual? How about

def tint_image(im, color):
color_map = []
for component in color:
color_map.extend(int(component/255.0*i) for i in range(256))
return im.point(color_map)

Peter
Nov 7 '08 #2

P: n/a
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
Nov 7 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.