beetlebug▾
/

image

Makes images, gives their size and type, and deletes them.

image SUBCOMMAND ?ARG ...?
IMAGENAME SUBCOMMAND ?ARG ...?

Description

An image is a picture that a widget can show, such as in the -image option of a button or a label. image create makes an image and a command with the name of the image. The subcommands of that command read and change the image.

ztk has two types of image:

A photo reads the formats PNG, GIF (the first image), PPM, JPEG, WebP, and SVG. The format comes from the first bytes of the data. The web build has no decoder of its own for JPEG and WebP: it gives the bytes to the decoder of the browser, and AVIF also. The script waits while the browser decodes, so image create gives the image at once, as in the native build. This needs the JavaScript Promise Integration (JSPI) of the browser and a script that runs in the frames or in evalAsync of the page. In a synchronous eval of the page, or in a browser with no JSPI, the result is the error of data that no decoder reads. The images of an app bundle (ztk bundle --target wasm) are the exception: the page decodes them before the script starts, up to 64 MiB of pixels in all, so they load in each call and in each browser. HEIF images need the decoder of the system on macOS, iOS, Android, and Windows, or a build with -Dheif.

A widget that shows an image draws again when the pixels change. The sizes of an image are in logical pixels.

Subcommands

SubcommandDescription
image createMakes a photo image or a bitmap image and its command.
image deleteDeletes images and their commands.
image widthGives the width of an image in logical pixels.
image heightGives the height of an image in logical pixels.
image typeGives the type of an image: photo or bitmap.
image typesGives the types of image.
image namesGives the names of the images.
image inuseTells if a widget shows the image: in ztk, always 0.
image putA command of a photo: puts colors or image data into the photo.
image getA command of a photo: gives the color of one pixel.
image dataA command of a photo: gives its pixels as colors or in a format.
image writeA command of a photo: writes its pixels to a file.
image blankA command of a photo: makes each pixel transparent.
image readA command of a photo: reads an image file into the photo.
image cgetA command of a photo or a bitmap: gives the value of an option.
image configureA command of a photo or a bitmap: gives or sets the options of the image.

Examples

% image create photo dot -width 2 -height 2
> dot put red -to 0 0 2 2
> dot get 1 1
255 0 0

% image create photo icon -width 16 -height 16
> button .b -image icon
> .b cget -image
icon

Errors

% image
wrong # args: should be "image option ?args?"
-errorcode TCL WRONGARGS

% image copy a b
bad option "copy": must be create, delete, height, inuse, names, type, types, or width
-errorcode NONE

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also