Photo(3pm)

πŸ’– Photo(3pm) β€” Tk::Photo πŸ’–

okay bestie, welcome to the User Contributed Perl Documentation section, this is the manual page for Tk::Photo, and it's literally all about full-color images. grab a snack, we're doing this. πŸ“


✨ NAME ✨

Tk::Photo β€” full-color images. that's the whole name. iconic, brief, no notes. πŸ‘‘


✨ SYNOPSIS ✨

$widget->Photo(?name??, options?)

✨ DESCRIPTION ✨

so a photo is an image where the pixels can display any color or be transparent. yes, transparent. we love a flexible queen. πŸ’…

a photo image is stored internally in full color β€” that's 32 bits per pixel β€” and it gets displayed using dithering if necessary. no cap.

image data for a photo image can come from:

  • a file πŸ“
  • a string 🧡
  • or it can be supplied from C code through a procedural interface πŸ‘‘

at present, only GIF, XBM, XPM, BMP, JPEG, PNG and PPM/PGM formats are supported, but there's an interface that exists to allow additional image file formats to be added easily. so if your fave format isn't here, the door is open, bestie. πŸšͺ✨

a photo image is transparent in regions where no image data has been supplied, or where it has been set transparent by the transparencySet subcommand. lowkey the vibe.


🌸 CREATING PHOTOS 🌸

photos are created using the Photo method. Photo supports the following options:

  • -data => string β€” specifies the contents of the image as a string. the string can contain base64 encoded data or binary data. the format of the string must be one of those for which there's an image file format handler that will accept string data. and heads up: if both the -data and -file options are specified, the -file option takes precedence. file wins, periodt. πŸ†
  • -format => format-name β€” specifies the name of the file format for the data specified with the -data or -file option.
  • -file => name β€” name gives the name of a file that is to be read to supply data for the photo image. the file format must be one of those for which there's an image file format handler that can read data.
  • -gamma => value β€” specifies that the colors allocated for displaying this image in a window should be corrected for a non-linear display with the specified gamma exponent value. (the intensity produced by most CRT displays is a power function of the input value, to a good approximation; gamma is the exponent and is typically around 2.) the value specified must be greater than zero. the default value is one (no correction). in general, values greater than one will make the image lighter, and values less than one will make it darker. glow up or glow down, your call ✨
  • -height => number β€” specifies the height of the image, in pixels. this option is useful primarily in situations where the user wishes to build up the contents of the image piece by piece. a value of zero (the default) allows the image to expand or shrink vertically to fit the data stored in it. flexible king πŸ’œ
  • -palette => palette-spec β€” specifies the resolution of the color cube to be allocated for displaying this image, and thus the number of colors used from the colormaps of the windows where it's displayed. the palette-spec string may be either a single decimal number, specifying the number of shades of gray to use, or three decimal numbers separated by slashes (/), specifying the number of shades of red, green and blue to use, respectively. if the first form (a single number) is used, the image will be displayed in monochrome (i.e., grayscale). sad beige image, but make it intentional 🎨
  • -width => number β€” specifies the width of the image, in pixels. this option is useful primarily in situations where the user wishes to build up the contents of the image piece by piece. a value of zero (the default) allows the image to expand or shrink horizontally to fit the data stored in it.

πŸ¦‹ IMAGE METHODS πŸ¦‹

when a photo image is created, Tk also creates a new object. this object supports the configure and cget methods described in Tk::options, which can be used to enquire and modify the options described above. go ask questions, it's healthy 🫢

those options that write data to the image generally expand the size of the image, if necessary, to accommodate the data written to the image β€” unless the user has specified non-zero values for the -width and/or -height configuration options, in which case the width and/or height, respectively, of the image will not be changed. she said what she said, the size stays. πŸ’€

the following addition methods are available for photo images:

  • $image->blank β€” blank the image; that is, set the entire image to have no data, so it will be displayed as transparent, and the background of whatever window it's displayed in will show through. ghost mode activated πŸ‘»
  • $image->copy(sourceImage ?,option value(s) ...?) β€” copies a region from the image called $sourceImage (which must be a photo image) to the image called $image, possibly with pixel zooming and/or subsampling. if no options are specified, this method copies the whole of $sourceImage into $image, starting at coordinates (0,0) in $image. the following options may be specified:
    • -from => x1, y1, ? ,x2, y2? β€” specifies a rectangular sub-region of the source image to be copied. (x1,y1) and (x2,y2) specify diagonally opposite corners of the rectangle. if x2 and y2 are not specified, the default value is the bottom-right corner of the source image. the pixels copied will include the left and top edges of the specified rectangle but not the bottom or right edges. if the -from option is not given, the default is the whole source image. boundaries matter bestie πŸŽ€
    • -to => x1, y1, ?, x2, y2? β€” specifies a rectangular sub-region of the destination image to be affected. (x1,y1) and (x2,y2) specify diagonally opposite corners of the rectangle. if x2 and y2 are not specified, the default value is (x1,y1) plus the size of the source region (after subsampling and zooming, if specified). if x2 and y2 are specified, the source region will be replicated if necessary to fill the destination region in a tiled fashion. tile it like a bathroom floor, slay 🧚
    • -shrink β€” specifies that the size of the destination image should be reduced, if necessary, so that the region being copied into is at the bottom-right corner of the image. this option will not affect the width or height of the image if the user has specified a non-zero value for the -width or -height configuration option, respectively.
    • -zoom => x, y β€” specifies that the source region should be magnified by a factor of x in the X direction and y in the Y direction. if y is not given, the default value is the same as x. with this option, each pixel in the source image will be expanded into a block of x x y pixels in the destination image, all the same color. x and y must be greater than 0. BIGGER. BOLDER. BETTER. πŸ”₯
    • -subsample => x, y β€” specifies that the source image should be reduced in size by using only every xth pixel in the X direction and yth pixel in the Y direction. negative values will cause the image to be flipped about the Y or X axes, respectively. if y is not given, the default value is the same as x. mirror mirror on the wall πŸͺž
    • -compositingrule => rule β€” specifies how transparent pixels in the source image are combined with the destination image. when a compositing rule of overlay is set, the old contents of the destination image are visible, as if the source image were printed on a piece of transparent film and placed over the top of the destination. when a compositing rule of set is set, the old contents of the destination image are discarded and the source image is used as-is. the default compositing rule is overlay. choose your fighter πŸ’…
  • $image->data(?option value(s), ...?) β€” returns image data in the form of a string. the following options may be specified:
    • -background => color β€” if the color is specified, the data will not contain any transparency information. in all transparent pixels the color will be replaced by the specified color.
    • -format => format-name β€” specifies the name of the image file format handler to be used. specifically, this method searches for the first handler whose name matches a initial substring of format-name and which has the capability to read this image data. if this option is not given, this method uses the first handler that has the capability to read the image data. first come, first served πŸƒβ€β™€οΈ
    • -from => x1, y1, ?, x2, y2? β€” specifies a rectangular region of $image to be returned. if only x1 and y1 are specified, the region extends from (x1,y1) to the bottom-right corner of $image. if all four coordinates are given, they specify diagonally opposite corners of the rectangular region, including x1,y1 and excluding x2,y2. the default, if this option is not given, is the whole image.
    • -grayscale β€” if this options is specified, the data will not contain color information. all pixel data will be transformed into grayscale. (yes, the original says "options", we're keeping it, it's canon πŸ’€)
  • $image->get(x, y) β€” returns the color of the pixel at coordinates (x,y) in the image as a list of three integers between 0 and 255, representing the red, green and blue components respectively. rgb girlies rise 🎨
  • $image->put(data ?,-format=>format-name? ?,-to=> x1 y1 ?x2 y2??) β€” sets pixels in $image to the data specified in data. this command first searches the list of image file format handlers for a handler that can interpret the data in data, and then reads the image encoded within into $image (the destination image). if data does not match any known format, an attempt to interpret it as a (top-to-bottom) list of scan-lines is made, with each scan-line being a (left-to-right) list of pixel colors (see Tk_GetColor for a description of valid colors.) every scan-line must be of the same length. note that when data is a single color name, you are instructing Tk to fill a rectangular region with that color. the following options may be specified:
    • -format => format-name β€” specifies the format of the image data in data. specifically, only image file format handlers whose names begin with format-name will be used while searching for an image data format handler to read the data.
  • -to =>x, y ?, x2, y2? β€” this one tells it where in $image to drop the goods. Specifically, it's the coordinates of the top-left corner (x1,y1) of the region of $image that data from filename gets read into, and the default is (0,0). Now if you also hand it x2,y2 and your data isn't big enough to cover that rectangle, the extracted image data gets tiled so it covers the whole destination rectangle. Plot twist tho: if the data is just a single color value, then a region stretching to the bottom-right corner represented by (x2,y2) gets filled with that color. πŸ’…

$image->read(filename ?,option value(s), ...?)

Okay bestie, this is the "go get me that picture" method. πŸ›οΈ It reads image data from the file named filename into the image. Here's the lil sequence it runs: first it searches the list of image file format handlers for a handler that can actually interpret the data in filename, and then it reads the image in filename into $image β€” that's the destination image, the one getting the glow-up. ✨

Options you can pass, all iconic:

  • -format => format-name β€” tells it what format the image data in filename is. Concretely, only image file format handlers whose names begin with format-name will be used while it's searching for an image data format handler to read the data. Narrowing the search, we love to see it.
  • -from => x1, y1, x2, y2 β€” specifies a rectangular sub-region of the image file data to be copied to the destination image. If you only give x1 and y1, the region stretches from (x1,y1) to the bottom-right corner of the image in the image file. If you give all four coordinates, they specify diagonally opposite corners of the region. And if you skip this option entirely, the default is the whole entire image in the image file. 🫢
  • -shrink β€” if this option is specified, the size of $image will be reduced, if necessary, so that the region into which the image file data are read is at the bottom-right corner of $image. Important caveat, no cap: this option will not affect the width or height of the image if the user has specified a non-zero value for the -width or -height configuration option, respectively. πŸ™ˆ
  • -to => x, y β€” coordinates of the top-left corner of the region of $image into which data from filename are to be read. Default is (0,0). Simple, clean, slays.

$image->redither

So the dithering algorithm used in displaying photo images propagates quantization errors from one pixel to its neighbors. Cute in theory! But if the image data for $image is supplied in pieces, the dithered image may not be exactly correct. 😭 Normally the difference is not noticeable, so don't panic, but if it is a problem, this method can be used to recalculate the dithered image in each window where the image is displayed. Fix-it button energy.

$image->transparency(subcommand, ?arg, arg ...?);

This one lets you examine and manipulate the transparency information in the photo image. Several subcommands are available:

  • $image->transparencyGet(x, y); β€” returns a boolean indicating if the pixel at (x,y) is transparent. πŸ‘»
  • $image->transparencySet(x, y, boolean); β€” makes the pixel at (x,y) transparent if boolean is true, and makes that pixel opaque otherwise. See-through or not see-through, your call, main character.

$image->write(filename ?,option value(s), ...?)

The reverse girlie: this writes image data from $image out to a file named filename. πŸ’Ύ Options available:

  • -background => color β€” if the color is specified, the data will not contain any transparency information. In all transparent pixels the color will be replaced by the specified color. Transparency? Gone. Replaced. Slay.
  • -format => format-name β€” specifies the name of the image file format handler to be used to write the data to the file. Specifically, this subcommand searches for the first handler whose name matches a initial substring of format-name and which has the capability to write an image file. If this option is not given, this subcommand uses the first handler that has the capability to write an image file. πŸ€·β€β™€οΈ
  • -from => x1, y1, ?, x2, y2? β€” specifies a rectangular region of $image to be written to the image file. If only x1 and y1 are specified, the region extends from (x1,y1) to the bottom-right corner of $image. If all four coordinates are given, they specify diagonally opposite corners of the rectangular region. The default, if this option is not given, is the whole image. πŸ–ΌοΈ
  • -grayscale β€” if this option is specified, the data will not contain color information. All pixel data will be transformed into grayscale. Color? Never met her.

πŸ–ΌοΈ IMAGE FORMATS πŸ–ΌοΈ

The photo image code is structured to allow handlers for additional image file formats to be added easily β€” very modular of it, honestly. The photo image code maintains a list of these handlers. Handlers are added to the list by registering them with a call to Tk_CreatePhotoImageFormat. The standard Tk distribution comes with handlers for XBM, XPM, BMP, JPEG, PNG and PPM/PGM formats, which are automatically registered on initialization. πŸ”₯

When reading an image file or processing string data specified with the -data configuration option, the photo image code invokes each handler in turn until one is found that claims to be able to read the data in the file or string. Usually this will find the correct handler, but if it doesn't, the user may give a format name with the -format option to specify which handler to use. In fact the photo image code will try those handlers whose names begin with the string specified for the -format option (the comparison is case-insensitive). For example, if the user specifies -format => gif, then a handler named GIF87 or GIF89 may be invoked, but a handler named JPEG may not (assuming that such handlers had been registered). Case-insensitive matching is lowkey the real MVP here. πŸ‘‘

When writing image data to a file, the processing of the -format option is slightly different: the string value given for the -format option must begin with the complete name of the requested handler, and may contain additional information following that, which the handler can use, for example, to specify which variant to use of the formats supported by the handler. Note that not all image handlers may support writing transparency data to a file, even where the target image format does. πŸ’€


🎨 COLOR ALLOCATION 🎨

When a photo image is displayed in a window, the photo image code allocates colors to use to display the image and dithers the image, if necessary, to display a reasonable approximation to the image using the colors that are available. The colors are allocated as a color cube, that is, the number of colors allocated is the product of the number of shades of red, green and blue. Color cube!! Math but make it fashion. 🌈

Normally, the number of colors allocated is chosen based on the depth of the window. For example, in an 8-bit PseudoColor window, the photo image code will attempt to allocate seven shades of red, seven shades of green and four shades of blue, for a total of 198 colors. In a 1-bit StaticGray (monochrome) window, it will allocate two colors, black and white. In a 24-bit DirectColor or TrueColor window, it will allocate 256 shades each of red, green and blue. Fortunately, because of the way that pixel values can be combined in DirectColor and TrueColor windows, this only requires 256 colors to be allocated. If not all of the colors can be allocated, the photo image code reduces the number of shades of each primary color and tries again. 🫢

The user can exercise some control over the number of colors that a photo image uses with the -palette configuration option. If this option is used, it specifies the maximum number of shades of each primary color to try to allocate. It can also be used to force the image to be displayed in shades of gray, even on a color display, by giving a single number rather than three numbers separated by slashes. Three numbers separated by slashes vs. one lonely number β€” choose your fighter.


πŸ’– CREDITS πŸ’–

The photo image type was designed and implemented by Paul Mackerras, based on his earlier photo widget and some suggestions from John Ousterhout. Absolute icons, we stan. πŸ‘‘


πŸ‘€ SEE ALSO πŸ‘€

  • Tk::Bitmap
  • Tk::Image
  • Tk::Pixmap

πŸ”‘ KEYWORDS πŸ”‘

  • photo
  • image
  • color

And that's the whole page, bestie!! πŸŽ€ You just read the perl v5.40.1 version of Photo(3pm) from 2025-04-13 β€” go forth and render pixels, you gorgeous genius. πŸ’…βœ¨

πŸ“¦ where this page comes from