473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fastest way to tint an image with PIL

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
2 7657
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick | last post by:
Hello all! I am using a BufferedImage object to build an image from scratch. I want it to be a grayscale image with only 8bits of color. I have the color information as a byte, saved in variable...
1
by: Thomas | last post by:
Hi all, Is there a faster way to read image dimensions other than something like: ImageOrig = System.Drawing.Image.FromFile(Filepath); ImageOrigHeight = ImageOrig.Height; ImageOrigWidth =...
1
by: James dean | last post by:
The following code is the current method i have for converting an image to Base64.Is this the most efficient way to convert an image to base64 or are there others. The Image class's Save method is...
2
by: johnb41 | last post by:
My app has an image (black/white tiff) displayed in a picturebox (scanned text document). I need the user to be able to draw either white or black rectangles on it. (to hide/cover up sensitive...
4
by: jordi_ramis | last post by:
Hi, I'm currently using image.fromfile to load jpg images in my application. I've noted that this procedure is very slow compared to some aplications I have seen. Is there any way to load a jpeg...
30
by: Chaos | last post by:
As my first attempt to loop through every pixel of an image, I used for thisY in range(0, thisHeight): for thisX in range(0, thisWidth): #Actions here for Pixel thisX, thisY But it takes...
12
by: Speed | last post by:
Hi, Could you please tell me what is the simplest code to read a 8-bit grayscale JPEG using C++? Thanks a ton, Speed.
6
by: ShadowLocke | last post by:
How can I tint one color with another color? i.e. say i have a Color.LightSkyBlue (135,206,250) i want to tint it with Color.Green(0,128, 0) Whats the math behind it?
4
Airslash
by: Airslash | last post by:
Hello, been a while since I visited bytes.com, but I have an issue with GDIplus... I'm currently developing on/for a Windows Server 2003 system and I'm running a Benchmark to find the fastest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.