TIP #166: Reading and Writing the Photo Image Alpha Channel

Title:Reading and Writing the Photo Image Alpha Channel
Version:$Revision: 1.9 $
Author:Donal K. Fellows <donal dot k dot fellows at man dot ac dot uk>
Created:Wednesday, 19 November 2003
Keywords:Tk, image get, image put


This TIP describes how to update the image get and image put subcommands so as to allow script-level access to the full alpha channel information that has been present in the photo image data model since Tk 8.3.


Alpha channels. We've had them in Tk's photo image data model (which is currently 8-bits-per-channel RGBA) for quite some time now. We can copy the alpha data around inside the image. We now display them correctly on deep-enough displays (many thanks to the people who have worked on that!) But can we write alpha data into an image? No. Not unless you have an image format handler that produces alpha data installed (e.g. the PNG from tkimg.) I think we should fix this so that people can read and write the full alpha data from scripts.


I propose to update the photoImageInstance get subcommand so that it returns four values instead of three, with the fourth being the contents of the alpha channel for the pixel.

I also propose to update the photoImageInstance put subcommand so that alpha channel information may be specified in the following ways when using the list-of-lists-of-pixel-data format (the image-format format will be up to the particular image format code, as always.)

Additionally, the photoImageInstance transparency command's subcommands will be updated in the following way:


Create a small image with a black border and a partially-transparent red area in the center.

image create photo transExample
transExample put black -to 0 0 10 10
transExample put red@.5 -to 2 2 8 8

Retrieve the alpha value of a pixel near the middle of the image created previously.

set aVal [transExample transparency get 5 5 -alpha]

Create a green box with a simple shadow effect

image create photo foo
# Make a simple graduated fill varying in alpha for the shadow
for {set i 14} {$i>0} {incr i -1} {
   set i2 [expr {$i+30}]
   foo put [format black#%x [expr {15-$i}]] -to $i $i $i2 $i2
# Put a solid green rectangle on top
foo put #080 -to 0 0 30 30

Reference Implementation

Nothing yet.


This document is placed in the public domain.

Powered by Tcl[Index] [History] [Edit] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows