Tk images
last modified November 24, 2026 Tcl 8.6
Tk has a built-in image type called the photo image. It stores a rectangular grid of pixels, reads and writes PNG and GIF files without any add-on library, and can be displayed in a label or drawn on a canvas. Photo images are the usual way to put icons, logos and sprite graphics into a Tk program.
This article creates images from scratch, loads them from disk, reads and
writes pixels, crops and scales them, and finally places them in widgets.
The examples use a few small files in the working directory; they were made
with ImageMagick's convert, but any PNG or GIF of the same size
works.
Photo images
image create photo makes an empty image with a name such as
image1 and returns that name. The -width and
-height options give it a size; without them the image starts at
zero by zero and grows when data is written into it.
package require Tk set img [image create photo -width 40 -height 30] puts "size: [image width $img] x [image height $img]" puts "type: [image type $img]" puts "inuse: [image inuse $img]" puts "blank: [$img get 0 0]" $img put #3a6ea5 -to 0 0 40 30 puts "painted corner: [$img get 39 29]" puts "inuse now: [image inuse $img]" destroy .
A new image is filled with black, so the first pixel reports
0 0 0. image inuse reports whether a widget
currently displays the image; it stays at zero because the image is not
shown yet. The image name is a command, so $img put and
$img get are subcommands of the image itself.
$ wish photo_create.tcl size: 40 x 30 type: photo inuse: 0 blank: 0 0 0 painted corner: 58 110 165 inuse now: 0
Loading PNG and GIF files
The -file option reads a file when the image is created. Tk 8.6
recognises PNG and GIF by their contents, so the extension is only a
convention. A format handler can also be named explicitly with
-format, which is useful for data that arrives without a file
name.
package require Tk
set png [image create photo -file swatch.png]
set gif [image create photo -file gradient.gif]
puts "swatch.png: [image width $png] x [image height $png]"
puts "gradient.gif: [image width $gif] x [image height $gif]"
puts "file option: [$png cget -file]"
puts "missing file: [catch {image create photo -file missing.png} err] $err"
destroy .
Both images arrive with the size stored in the file, and
cget -file remembers the path. A missing file raises an error
that names the path, and the script catches it so the program can report the
problem instead of aborting. The two files here are 277 and 309 bytes, small
enough to be generated by any drawing tool.
$ wish load.tcl swatch.png: 40 x 30 gradient.gif: 32 x 24 file option: swatch.png missing file: 1 couldn't open "missing.png": no such file or directory
Image metadata
The size of an image is read with image width and
image height. Together with image type and
image inuse they form the metadata a program needs before it
lays out a picture. The pixel colours at the corners show whether the file
was decoded as expected.
package require Tk set img [image create photo -file gradient.gif] puts "width: [image width $img]" puts "height: [image height $img]" puts "type: [image type $img]" puts "inuse: [image inuse $img]" puts "top left: [$img get 0 0]" puts "bottom right: [$img get 31 23]" set empty [image create photo] puts "empty image: [image width $empty] x [image height $empty]" destroy .
The gradient file runs from orange in the top left corner to dark blue in the
bottom right, which is exactly what the two pixel reads show. The last line
demonstrates the default size: an image created without options is
0 x 0 until something is drawn into it.
$ wish metadata.tcl width: 32 height: 24 type: photo inuse: 0 top left: 255 136 0 bottom right: 0 68 136 empty image: 0 x 0
Pixel values with put and get
put writes colours into a region and get reads one
pixel. Both take pixel coordinates with the origin in the top left corner.
The value passed to put is either a single colour or a list of
rows, where each row is a list of colours.
package require Tk
set img [image create photo -width 4 -height 3]
$img put red -to 0 0 1 1
$img put #00ff00 -to 3 2 4 3
$img put {{#0000ff white}} -to 1 1
puts "0,0 -> [$img get 0 0]"
puts "3,2 -> [$img get 3 2]"
puts "1,1 -> [$img get 1 1]"
puts "2,1 -> [$img get 2 1]"
puts "row 0: [$img data -from 0 0 4 1]"
puts "outside: [catch {$img get 5 5} err] $err"
destroy .
The region is given as two corners, so 0 0 1 1 covers exactly
one pixel and 3 2 4 3 the last pixel of the row. The nested list
{{#0000ff white}} is one row of two colours, which paints the
neighbouring pixel white. data -from returns the same pixels as
colour strings, which is convenient for saving or comparing a whole region.
$ wish pixels.tcl
0,0 -> 255 0 0
3,2 -> 0 255 0
1,1 -> 0 0 255
2,1 -> 255 255 255
row 0: {#ff0000 #000000 #000000 #000000}
outside: 1 image1 get: coordinates out of range
Copying and subsampling
The copy subcommand fills a destination image from a source
image. -subsample 2 2 takes every second pixel in both
directions, -zoom 3 3 repeats each pixel three times, and
-from selects a rectangle of the source. The destination size
follows from the operation.
package require Tk set src [image create photo -file checker.gif] set half [image create photo] $half copy $src -subsample 2 2 set zoom [image create photo] $zoom copy $src -zoom 3 3 set part [image create photo] $part copy $src -from 2 2 6 6 puts "source: [image width $src] x [image height $src]" puts "subsample 2: [image width $half] x [image height $half]" puts "zoom 3: [image width $zoom] x [image height $zoom]" puts "crop: [image width $part] x [image height $part]" puts "crop pixel: [$part get 0 0]" destroy .
Subsampling an 8 by 8 checkerboard gives a 4 by 4 image, zooming gives 24 by
24, and the -from region of four by four pixels produces a small
crop. The destination image was created empty and sized itself during the
copy, which is the normal way to derive a thumbnail or a scaled icon.
$ wish copy.tcl source: 8 x 8 subsample 2: 4 x 4 zoom 3: 24 x 24 crop: 4 x 4 crop pixel: 153 153 153
Writing an image back
write saves an image to a file. The format is chosen by the
handler name in -format; Tk 8.6 always provides
png, gif and ppm. Writing is the
easiest way to check that an operation produced the expected pixels, and it
also lets a program export what the user sees.
package require Tk set src [image create photo -file gradient.gif] set big [image create photo] $big copy $src -zoom 2 2 $big write out.png -format png $big write out.gif -format gif $big write out.ppm -format ppm puts "png: [file size out.png] bytes" puts "gif: [file size out.gif] bytes" puts "ppm: [file size out.ppm] bytes" set back [image create photo -file out.png] puts "reloaded: [image width $back] x [image height $back]" puts "pixel: [$back get 10 10]" destroy .
The 32 by 24 gradient is doubled to 64 by 48 and stored in three formats, which need 309, 523 and 9229 bytes for the same pixels. PNG compresses the smooth gradient best, GIF is limited to 256 colours, and PPM stores every pixel without compression, which makes the file thirty times larger. Reloading the PNG returns the same size and colour, so the round trip is lossless.
$ wish write.tcl png: 309 bytes gif: 523 bytes ppm: 9229 bytes reloaded: 64 x 48 pixel: 200 121 30
Transparency
A PNG file can carry an alpha channel, and Tk keeps that information in the
photo image. transparency get returns 1 for a fully transparent
pixel and 0 for an opaque one; transparency set changes a single
pixel. The 24 by 24 test file is a red circle on a transparent background.
package require Tk
set img [image create photo -file alpha.png]
puts "centre opaque: [$img transparency get 12 12]"
puts "corner transparent: [$img transparency get 0 0]"
$img write alpha_out.png -format png
set back [image create photo -file alpha_out.png]
puts "after round trip: [$back transparency get 0 0] [$back transparency get 12 12]"
set solid [image create photo -width 8 -height 8]
$solid put #b03030 -to 0 0 8 8
$solid transparency set 3 3 1
puts "hidden pixel: [$solid transparency get 3 3]"
puts "neighbour: [$solid transparency get 3 4]"
for {set x 0} {$x < 4} {incr x} {
for {set y 0} {$y < 4} {incr y} {
$solid transparency set $x $y 1
}
}
puts "top-left block: [$solid transparency get 1 1]"
destroy .
The centre of the circle reports 0 because it is painted, and the corner
reports 1 because it is empty. Writing the image with the png
handler keeps the alpha channel, so the reloaded copy still has a transparent
corner. The second image starts fully opaque and gets a transparent pixel and
then a 4 by 4 transparent block, which is how a program can punch holes or
build simple masks.
$ wish alpha.tcl centre opaque: 0 corner transparent: 1 after round trip: 1 0 hidden pixel: 1 neighbour: 0 top-left block: 1
Images in labels and canvases
A label displays an image through its -image option, and
-compound controls how image and text share the widget. A canvas
displays an image as an item created with create image, which
can be moved, raised and bound like any other item.
package require Tk
set icon [image create photo -file alpha.png]
font create caption -family "DejaVu Sans" -size 10
label .label -image $icon -text "alpha.png" -compound top -font caption
canvas .canvas -width 140 -height 70 -background white
.canvas create image 10 10 -image $icon -anchor nw
.canvas create text 60 30 -text "canvas item" -fill #3a6ea5 -anchor nw
pack .label -padx 8 -pady 6
pack .canvas -padx 8 -pady 6
puts "label image is [image width [.label cget -image]] px wide"
puts "canvas items: [.canvas find all]"
puts "image bbox: [.canvas bbox 1]"
puts "text coords: [.canvas coords 2]"
after 500 {destroy .}
The label shows the icon above its caption because of
-compound top. On the canvas the image is item 1, anchored at
its north-west corner so that its top left pixel lands on 10,10; the text
item follows at 60,30. The bounding box of the image item confirms the
24 pixel size from the file.
$ wish widgets.tcl label image is 24 px wide canvas items: 1 2 image bbox: 10 10 34 34 text coords: 60.0 30.0
Source
The photo image is documented on the image and
photo manual pages, and the canvas image item on the canvas
page.
This article created photo images, loaded PNG and GIF files, read and wrote pixels, copied and scaled images and placed them in widgets. Continue with Tk canvas animation, where a photo image becomes a moving sprite, or read about Tk fonts and colours for the colour syntax used in the pixel examples.
Author
List all Tcl Tutorials.