image
Guide: Canvas
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:
photo: a picture of RGBA pixels from image data, a file, or the colors of a script. Its command hasblank,cget,configure,get,put, andread.bitmap: a picture of two colors from the data of an XBM file. Its command hascgetandconfigure.
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
| Subcommand | Description |
|---|---|
image create | Makes a photo image or a bitmap image and its command. |
image delete | Deletes images and their commands. |
image width | Gives the width of an image in logical pixels. |
image height | Gives the height of an image in logical pixels. |
image type | Gives the type of an image: photo or bitmap. |
image types | Gives the types of image. |
image names | Gives the names of the images. |
image inuse | Tells if a widget shows the image: in ztk, always 0. |
image put | A command of a photo: puts colors or image data into the photo. |
image get | A command of a photo: gives the color of one pixel. |
image data | A command of a photo: gives its pixels as colors or in a format. |
image write | A command of a photo: writes its pixels to a file. |
image blank | A command of a photo: makes each pixel transparent. |
image read | A command of a photo: reads an image file into the photo. |
image cget | A command of a photo or a bitmap: gives the value of an option. |
image configure | A 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
- The photo commands
copy,redither, andtransparencydo not exist. The message of a bad subcommand of a photo lists them, but a call of them gives the same error. - The PNG that
dataandwritegive has nosBITortEXtchunk, so its bytes differ from the PNG of Tk with the same pixels. The PPM and the GIF have the bytes of Tk 8.6. image inusegives 0, also when a widget shows the image. Tk 8.6.16 gives 1 for an image that a widget shows.image namesdoes not give the icons::tk::icons::error,::tk::icons::information,::tk::icons::question, and::tk::icons::warningof Tk 8.6.16.- The
-foregroundof a bitmap is an empty string by default. Tk 8.6.16 gives#000000. A bitmap file that does not open gives the message ofopen,couldn't open "FILE". Tk 8.6.16 givescouldn't read bitmap file "FILE". -densityand the SVG options of-formatare part of ztk. Tk 8.6.16 has no-densityand no SVG format.- A subcommand needs its full name:
image cis an error. Tk 8.6.16 accepts a unique short form. - The errors have the
-errorcodeNONE, except some wrong counts of arguments. Tk 8.6.16 gives codes such asTK LOOKUP IMAGEandTK IMAGE PHOTO BAD_OPTION.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1