[Image-SIG] Transparent colored overlay effect
Charles G Waldman
cgw@pgt.com
Tue, 29 Sep 1998 12:50:54 -0400 (EDT)
Roger Burnham writes:
> image. I would like to render the overlay image with the identified pixels
> "shaded" a selected color; i.e. you can still see the gray-scale intensities
> thru a "tinged" color (I've seen this in other commercial apps...).
>
There's more than one way to do this, I'm sure. One thing you could
do is create the overlay as an RGBA image with an alpha (opacity)
channel. Here's a (not very optimized) example. It puts up a
greyscale testpattern and with an overlaid transparent red square in
the middle. The slider allows you to adjust the opacity of the red
square.
from Tkinter import *
import Image
from ImageTk import PhotoImage
from string import atoi
class Demo(Frame):
def __init__(self,master=None):
Frame.__init__(self, master)
self.createWidgets()
def createWidgets(self):
self.image = Image.new('RGB',(256,256),0)
for y in range(0,256):
for x in range(0,256):
v = (x*4)%256
self.image.putpixel((x,y),(v,v,v))
self.mask = Image.new("RGBA",(256,256))
self.photo = PhotoImage(image=self.image)
self.slider = Scale(self, from_=0,to=255,
command=self.set_alpha,orient=HORIZONTAL)
self.slider.pack()
self.label = Label(self, image=self.photo)
self.label.pack()
self.pack()
def set_alpha(self,alpha):
alpha = atoi(alpha)
for y in range(128-32,128+32):
for x in range(128-32,128+32):
self.mask.putpixel((x,y),(255,0,0,alpha))
im2 = self.image.copy()
im2.paste(self.mask,None,self.mask)
self.photo = PhotoImage(image=im2)
self.label['image']=self.photo
self.update()
if __name__=="__main__":
Demo().mainloop()