image create
Makes a photo image or a bitmap image and its command.
image create photo ?NAME? ?-option VALUE ...?
image create bitmap ?NAME? ?-option VALUE ...?
Description
Makes an image of the type with the options, and a command with the name NAME. With no NAME, the name is image1, image2, and so on. An image with the name of an image that exists replaces it.
A photo with no -data, -file, -width, or -height is empty, 0 by 0 pixels, until put or read fills it. A photo has the options -data, -file, -format, -width, -height, -density, -gamma, and -palette. A bitmap has -data, -file, -foreground, -background, -maskdata, and -maskfile.
Options
| Option | Type | Default | Description |
|---|---|---|---|
-data | string | none | The data of the image: for a photo, base64 text or the bytes of a file; for a bitmap, the text of an XBM file. |
-file | path | none | The file of the image data. |
-format | list | none | The format of the data, and the options of an SVG image. |
-width | integer | 0 | The width of a photo in pixels, or 0 for the width of its data. |
-height | integer | 0 | The height of a photo in pixels, or 0 for the height of its data. |
-density | integer | 1 | The count of pixels of the data for each logical pixel, from 1 to 4. |
-gamma | double | 1.0 | The gamma correction of a photo in Tk, stored with no effect. |
-palette | string | none | The palette of colors for a display with few colors, stored with no effect. |
-foreground | color | none | The color of the set bits of a bitmap. |
-background | color | none | The color of the bits that are not set, or an empty string for no color. |
-maskdata | string | none | The text of an XBM file whose set bits mark the bits of the bitmap to draw. |
-maskfile | path | none | The XBM file of the mask of a bitmap. |
-data DATA
The data of the image: for a photo, base64 text or the bytes of a file; for a bitmap, the text of an XBM file.
A photo decodes the base64 text, or the bytes as a binary string, as in Tk 8.6. The format comes from the first bytes. cget -data gives the text of the option.
A bitmap reads the text of an XBM file: the #define lines of the width and the height, and the bytes of the bits. A set bit has the foreground color.
% set png iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==
> image create photo one -data $png
> list [image width one] [one get 0 0]
1 {255 0 0}
% image create bitmap check -data "#define c_width 3\n#define c_height 2\nstatic char c_bits\[\] = {0x05, 0x02};"
> list [image width check] [image height check]
3 2% image create photo bad -data notanimage
couldn't recognize image data
-errorcode NONE
% image create bitmap bad2 -data junk
format error in bitmap data
-errorcode NONE-file FILENAME
The file of the image data.
The file opens through the file commands of the script, so it needs the file capability, and in the jail of a root folder the path is from the root. A photo reads the formats of the command. A bitmap reads an XBM file.
% set png iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==
> set f [open red.png wb]
> puts -nonewline $f [binary decode base64 $png]
> close $f
> image create photo fromfile -file red.png
> file delete red.png
> image width fromfile
1% image create photo nofile -file /nonexistent/a.png
couldn't open "/nonexistent/a.png": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}-format FORMAT
The format of the data, and the options of an SVG image.
The decoder finds the format from the data, so a name of a format has no effect. For an SVG image, the list svg with options sets the size: -scale S multiplies the size of the SVG, -scaletowidth W and -scaletoheight H set one side and keep the shape, and -dpi D is accepted.
% image create photo vec -format {svg -scale 2} -data {<svg xmlns="http://www.w3.org/2000/svg" width="10" height="5"/>}
> list [image width vec] [image height vec]
20 10
% image create photo vec2 -format {svg -scaletowidth 40} -data {<svg xmlns="http://www.w3.org/2000/svg" width="10" height="5"/>}
> list [image width vec2] [image height vec2]
40 20% image create photo vec3 -format {svg -scale 0}
bad value "0" for -scale: must be more than 0
-errorcode NONE
% image create photo vec4 -format {svg -size 2}
bad option "-size": must be -dpi, -scale, -scaletoheight, or -scaletowidth
-errorcode NONE-width PIXELS
The width of a photo in pixels, or 0 for the width of its data.
A width above 0 fixes the width: data that is wider is cut, and a put outside the width does not make the photo wider. The largest width is 8,192. While the other side of the size is 0, the photo has no pixels, as in Tk 8.6; a put then gives it the height of its data.
% image create photo wide -width 64
> set before [list [image width wide] [image height wide]]
> wide put red -to 0 0 1 1
> list $before [image width wide] [image height wide]
{0 0} 64 1% image create photo w2 -width x
expected integer but got "x"
-errorcode NONE-height PIXELS
The height of a photo in pixels, or 0 for the height of its data.
A height above 0 fixes the height, as -width does for the width. The largest height is 8,192.
% image create photo tall -width 10 -height 48
> image height tall
48% image create photo h2 -height x
expected integer but got "x"
-errorcode NONE-density N
The count of pixels of the data for each logical pixel, from 1 to 4.
An image for a screen with a high density has 2 or 3 pixels for each logical pixel. With -density 2, a photo of 64 by 64 pixels has the size 32 by 32 in the layout, and the screen shows each pixel of the data. This option is part of ztk and is not in Tk.
% image create photo sharp -density 2 -width 64 -height 64
> list [image width sharp] [image height sharp]
32 32% image create photo d2 -density 5
bad density "5": must be 1 to 4
-errorcode NONE-gamma VALUE
The gamma correction of a photo in Tk, stored with no effect.
ztk accepts the option, and cget -gamma gives 1.0.
% image create photo g -gamma 2.2
> g cget -gamma
1.0ztk accepts any value.
-palette PALETTE
The palette of colors for a display with few colors, stored with no effect.
ztk accepts the option, and cget -palette gives an empty string.
% image create photo pal -palette 8
> pal cget -paletteztk accepts any value.
-foreground COLOR
The color of the set bits of a bitmap.
The default is an empty string, and the set bits are black.
% image create bitmap fg -foreground red
> fg cget -foreground
red% image create bitmap fg2 -foreground
value for "-foreground" missing
-errorcode NONE-background COLOR
The color of the bits that are not set, or an empty string for no color.
With no background, the bits that are not set show what is under the image.
% image create bitmap bg -background yellow
> bg cget -background
yellow% image create bitmap bg2 -background
value for "-background" missing
-errorcode NONE-maskdata DATA
The text of an XBM file whose set bits mark the bits of the bitmap to draw.
A bit that is not set in the mask is not drawn, also with a background. The mask must have the size of the bitmap.
% set xbm "#define m_width 2\n#define m_height 1\nstatic char m_bits\[\] = {0x01};"
> image create bitmap masked -data $xbm -maskdata $xbm
> image width masked
2% image create bitmap mm -data "#define a_width 2\n#define a_height 1\nstatic char a_bits\[\] = {0x01};" -maskdata "#define b_width 1\n#define b_height 1\nstatic char b_bits\[\] = {0x01};"
bitmap and mask have different sizes
-errorcode NONE-maskfile FILENAME
The XBM file of the mask of a bitmap.
The same as -maskdata, with the text from a file.
% image create bitmap mf
> mf cget -maskfile% image create bitmap mf2 -maskfile /nonexistent/m.xbm
couldn't open "/nonexistent/m.xbm": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}Returns
The name of the image.
Examples
% image create photo logo
logo
% image create photo
image1
% image create photo pic -width 40 -height 30
> list [image width pic] [image height pic]
40 30
Errors
% image create vector
image type "vector" doesn't exist
-errorcode NONE
% image create
wrong # args: should be "image create type ?name? ?-option value ...?"
-errorcode TCL WRONGARGS
% image create photo p -size 3
bad option "-size": must be -data, -density, -file, -format, -gamma, -height, -palette, or -width
-errorcode NONE
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1