Skip to main content
Pixeltable UDFs for ImageType. Example:
import pixeltable as pxt

t = pxt.get_table(...)
t.select(t.img_col.convert('L')).collect()
View source on GitHub

udf alpha_composite()

alpha_composite(im1: Image, im2: Image) -> Image
Alpha composite im2 over im1. Parameters:
  • im1 (Image): The first image. Must have mode RGBA.
  • im2 (Image): The second image. Must have mode RGBA, and the same size as the first image.
Returns:
  • Image: An ~PIL.Image.Image object.

udf b64_encode()

b64_encode(img: Image, image_format: String = 'png') -> String
Convert image to a base64-encoded string. Parameters:

udf blend()

blend(im1: Image, im2: Image, alpha: Float) -> Image
Creates a new image by interpolating between two input images, using a constant alpha:: out = image1 * (1.0 - alpha) + image2 * alpha Parameters:
  • im1 (Image): The first image.
  • im2 (Image): The second image. Must have the same mode and size as the first image.
  • alpha (Float): The interpolation alpha factor. If alpha is 0.0, a copy of the first image is returned. If alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range.
Returns:
  • Image: An ~PIL.Image.Image object.

udf composite()

composite(image1: Image, image2: Image, mask: Image) -> Image
Create composite image by blending images using a transparency mask. Parameters:
  • image1 (Image): The first image.
  • image2 (Image): The second image. Must have the same mode and size as the first image.
  • mask (Image): A mask image. This image can have mode “1”, “L”, or “RGBA”, and must have the same size as the other two images.

udf convert()

convert(self: Image, mode: String) -> Image
Convert the image to a different mode. Equivalent to PIL.Image.Image.convert(). Parameters:
  • mode (String): The mode to convert to. See the Pillow documentation for a list of supported modes.

udf crop()

crop(self: Image, box: Json = None) -> Image
Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate. See :ref:coordinate-system. Note: Prior to Pillow 3.4.0, this was a lazy operation. Parameters:
  • box (Json): The crop rectangle, as a (left, upper, right, lower)-tuple.
Returns:
  • Image: An ~PIL.Image.Image object.

udf effect_spread()

effect_spread(self: Image, distance: Int) -> Image
Randomly spread pixels in an image. Parameters:
  • distance (Int): Distance to spread pixels.

udf entropy()

entropy(
    self: Image,
    mask: Image | None = None,
    extrema: Json | None = None
) -> Float
Calculates and returns the entropy for the image. A bilevel image (mode “1”) is treated as a grayscale (“L”) image by this method. If a mask is provided, the method employs the histogram for those parts of the image where the mask image is non-zero. The mask image must have the same size as the image, and be either a bi-level image (mode “1”) or a grayscale image (“L”). Parameters:
  • mask (Image | None): An optional mask.
  • extrema (Json | None): An optional tuple of manually-specified extrema.
Returns:
  • Float: A float value representing the image entropy

udf get_metadata()

get_metadata(self: Image) -> Json
Return metadata for the image.

udf getbands()

getbands(self: Image) -> Json
Returns a tuple containing the name of each band in this image. For example, getbands on an RGB image returns (“R”, “G”, “B”). Returns:
  • Json: A tuple containing band names.

udf getbbox()

getbbox(self: Image, *, alpha_only: Bool = True) -> Json
Calculates the bounding box of the non-zero regions in the image. Parameters:
  • alpha_only (Bool): Optional flag, defaulting to True. If True and the image has an alpha channel, trim transparent pixels. Otherwise, trim pixels when all channels are zero. Keyword-only argument.
Returns:
  • Json: The bounding box is returned as a 4-tuple defining the left, upper, right, and lower pixel coordinate. See :ref:coordinate-system. If the image is completely empty, this method returns None.

udf getchannel()

getchannel(self: Image, channel: Int) -> Image
Returns an image containing a single channel of the source image. Parameters:
  • channel (Int): What channel to return. Could be index (0 for “R” channel of “RGB”) or channel name (“A” for alpha channel of “RGBA”).
Returns:
  • Image: An image in “L” mode. .. versionadded:: 4.3.0

udf getcolors()

getcolors(self: Image, maxcolors: Int = 256) -> Json
Returns a list of colors used in this image. The colors will be in the image’s mode. For example, an RGB image will return a tuple of (red, green, blue) color values, and a P image will return the index of the color in the palette. Parameters:
  • maxcolors (Int): Maximum number of colors. If this number is exceeded, this method returns None. The default limit is 256 colors.
Returns:
  • Json: An unsorted list of (count, pixel) values.

udf getextrema()

getextrema(self: Image) -> Json
Gets the minimum and maximum pixel values for each band in the image. Returns:
  • Json: For a single-band image, a 2-tuple containing the minimum and maximum pixel value. For a multi-band image, a tuple containing one 2-tuple for each band.

udf getpalette()

getpalette(self: Image) -> Json
Returns the image palette as a list. Parameters:
  • rawmode (Any): The mode in which to return the palette. None will return the palette in its current mode. .. versionadded:: 9.1.0
Returns:
  • Json: A list of color values [r, g, b, …], or None if the image has no palette.

udf getpixel()

getpixel(self: Image, xy: Json) -> Json
Return the pixel value at the given position. The position xy is a tuple containing the x and y coordinates. Equivalent to PIL.Image.Image.getpixel() Parameters:
  • xy (Json): The coordinates, given as (x, y).

udf getprojection()

getprojection(self: Image) -> Json
Get projection to x and y axes Returns:
  • Json: Two sequences, indicating where there are non-zero pixels along the X-axis and the Y-axis, respectively.

udf height()

height(self: Image) -> Int

udf histogram()

histogram(
    self: Image,
    mask: Image | None = None,
    extrema: Json | None = None
) -> Json
Returns a histogram for the image. The histogram is returned as a list of pixel counts, one for each pixel value in the source image. Counts are grouped into 256 bins for each band, even if the image has more than 8 bits per band. If the image has more than one band, the histograms for all bands are concatenated (for example, the histogram for an “RGB” image contains 768 values). A bilevel image (mode “1”) is treated as a grayscale (“L”) image by this method. If a mask is provided, the method returns a histogram for those parts of the image where the mask image is non-zero. The mask image must have the same size as the image, and be either a bi-level image (mode “1”) or a grayscale image (“L”). Parameters:
  • mask (Image | None): An optional mask.
  • extrema (Json | None): An optional tuple of manually-specified extrema.
Returns:
  • Json: A list containing pixel counts.

udf mode()

mode(self: Image) -> String

udf point()

point(
    self: Image,
    lut: Json,
    mode: String | None = None
) -> Image
Map image pixels through a lookup table. Equivalent to PIL.Image.Image.point() Parameters:
  • lut (Json): A lookup table.

udf quantize()

quantize(
    self: Image,
    colors: Int = 256,
    method: Int | None = None,
    kmeans: Int = 0,
    palette: Int | None = None,
    dither: Int = <Dither.FLOYDSTEINBERG: 3>
) -> Image
Convert the image to ‘P’ mode with the specified number of colors. Parameters:
  • colors (Int): The desired number of colors, <= 256
  • method (Int | None): Quantize.MEDIANCUT (median cut), Quantize.MAXCOVERAGE (maximum coverage), Quantize.FASTOCTREE (fast octree), Quantize.LIBIMAGEQUANT (libimagequant; check support using PIL.features.check_feature with feature="libimagequant"). By default, Quantize.MEDIANCUT will be used. The exception to this is RGBA images. Quantize.MEDIANCUT and Quantize.MAXCOVERAGE do not support RGBA images, so Quantize.FASTOCTREE is used by default instead.
  • kmeans (Int): Integer greater than or equal to zero.
  • palette (Int | None): Quantize to the palette of given PIL.Image.Image.
  • dither (Int): Dithering method, used when converting from mode “RGB” to “P” or from “RGB” or “L” to “1”. Available methods are Dither.NONE or Dither.FLOYDSTEINBERG (default).
Returns:
  • Image: A new image

udf reduce()

reduce(self: Image, factor: Int, box: Json | None = None) -> Image
Returns a copy of the image reduced factor times. If the size of the image is not dividable by factor, the resulting size will be rounded up. Parameters:
  • factor (Int): A greater than 0 integer or tuple of two integers for width and height separately.
  • box (Json | None): An optional 4-tuple of ints providing the source image region to be reduced. The values must be within (0, 0, width, height) rectangle. If omitted or None, the entire source is used.

udf resize()

resize(self: Image, size: Json) -> Image
Return a resized copy of the image. The size parameter is a tuple containing the width and height of the new image. Equivalent to PIL.Image.Image.resize()

udf rotate()

rotate(self: Image, angle: Int) -> Image
Return a copy of the image rotated by the given angle. Equivalent to PIL.Image.Image.rotate() Parameters:
  • angle (Int): The angle to rotate the image, in degrees. Positive angles are counter-clockwise.

udf transpose()

transpose(self: Image, method: Int) -> Image
Transpose image (flip or rotate in 90 degree steps) Parameters:
  • method (Int): One of :pyTranspose.FLIP_LEFT_RIGHT, :pyTranspose.FLIP_TOP_BOTTOM, :pyTranspose.ROTATE_90, :pyTranspose.ROTATE_180, :pyTranspose.ROTATE_270, :pyTranspose.TRANSPOSE or :pyTranspose.TRANSVERSE.
Returns:
  • Image: Returns a flipped or rotated copy of this image.

udf width()

width(self: Image) -> Int