xpark.dataset.ImageCompute#

class xpark.dataset.ImageCompute(*args, **kwargs)[source]#

Note

Do not construct this class, use the staticmethod instead.

Methods

convert(images[, mode, matrix, dither, ...])

Returns a converted copy of this image.

crop(images[, box, source_mode])

Returns a rectangular region from this image.

entropy(images[, mask, extrema, source_mode])

Calculates and returns the entropy for the image.

filter(images, filter[, source_mode])

Filters this image using the given filter.

getchannel(images, channel[, source_mode])

Returns an image containing a single channel of the source image.

height(images[, source_mode])

Image height, in pixels.

mode(images[, source_mode])

Image mode.

point(images, lut[, mode, source_mode])

Maps this image through a lookup table or function.

quantize(images[, colors, method, kmeans, ...])

Convert the image to 'P' mode with the specified number of colors.

reduce(images, factor[, box, source_mode])

Returns a copy of the image reduced factor times.

resize(images, size[, resample, box, ...])

Returns a resized copy of this image.

rotate(images, angle[, resample, expand, ...])

Returns a rotated copy of this image.

size(images[, source_mode])

Image size, in pixels.

width(images[, source_mode])

Image width, in pixels.

static convert(images: ChunkedArray, mode: str | None = None, matrix: tuple[float, ...] | None = None, dither: Dither | None = None, palette: Palette = Palette.WEB, colors: int = 256, source_mode: str | None = None)#

Returns a converted copy of this image. For the “P” mode, this method translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.

This supports all possible conversions between “L”, “RGB” and “CMYK”. The matrix argument only supports “L” and “RGB”.

When translating a color image to grayscale (mode “L”), the library uses the ITU-R 601-2 luma transform:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

The default method of converting a grayscale (“L”) or “RGB” image into a bilevel (mode “1”) image uses Floyd-Steinberg dither to approximate the original image luminosity levels. If dither is None, all values larger than 127 are set to 255 (white), all other values to 0 (black). To use other thresholds, use the point() method.

When converting from “RGBA” to “P” without a matrix argument, this passes the operation to quantize(), and dither and palette are ignored.

When converting from “PA”, if an “RGBA” palette is present, the alpha channel from the image will be used instead of the values from the palette.

Parameters:
  • images – The images to be processed.

  • mode – The requested mode. See: concept-modes.

  • matrix – An optional conversion matrix. If given, this should be 4- or 12-tuple containing floating point values.

  • dither – 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). Note that this is not used when matrix is supplied.

  • palette – Palette to use when converting from mode “RGB” to “P”. Available palettes are Palette.WEB or Palette.ADAPTIVE.

  • colors – Number of colors to use for the Palette.ADAPTIVE palette. Defaults to 256.

  • source_mode – The mode of the images. Default is auto detect.

Return type:

Image

Returns:

An Image object.

static crop(images: ChunkedArray, box: tuple[float, float, float, float] | None = None, source_mode: str | None = None) Array#

Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate. See coordinate-system.

Note: Prior to Pillow 3.4.0, this was a lazy operation.

Parameters:
  • images – The images to be processed.

  • box – The crop rectangle, as a (left, upper, right, lower)-tuple.

  • source_mode – The mode of the images. Default is auto detect.

Return type:

Image

Returns:

An Image object.

static entropy(images: ChunkedArray, mask: Image | None = None, extrema: tuple[float, float] | None = None, source_mode: str | None = None)#

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:
  • images – The images to be processed.

  • mask – An optional mask.

  • extrema – An optional tuple of manually-specified extrema.

  • source_mode – The mode of the images. Default is auto detect.

Returns:

A float value representing the image entropy

static filter(images: ChunkedArray, filter: ImageFilter.Filter | type[ImageFilter.Filter], source_mode: str | None = None)#

Filters this image using the given filter. For a list of available filters, see the ImageFilter module.

Parameters:
  • images – The images to be processed.

  • filter – Filter kernel.

  • source_mode – The mode of the images. Default is auto detect.

Returns:

An Image object.

static getchannel(images: ChunkedArray, channel: int | str, source_mode: str | None = None) Array#

Returns an image containing a single channel of the source image.

Parameters:
  • images – The images to be processed.

  • channel – What channel to return. Could be index (0 for “R” channel of “RGB”) or channel name (“A” for alpha channel of “RGBA”).

  • source_mode – The mode of the images. Default is auto detect.

Returns:

An image in “L” mode.

Added in version 4.3.0.

static height(images: ChunkedArray, source_mode: str | None = None) Array#

Image height, in pixels.

Parameters:
  • images – The images to be processed.

  • source_mode – The mode of the images. Default is auto detect.

static mode(images: ChunkedArray, source_mode: str | None = None) Array#

Image mode. This is a string specifying the pixel format used by the image. Typical values are “1”, “L”, “RGB”, or “CMYK.”

Parameters:
  • images – The images to be processed.

  • source_mode – The mode of the images. Default is auto detect.

static point(images: ChunkedArray, lut: Sequence[float] | NumpyArray | Callable[[int], float] | Callable[[ImagePointTransform], ImagePointTransform | float] | ImagePointHandler, mode: str | None = None, source_mode: str | None = None)#

Maps this image through a lookup table or function.

Parameters:
  • images – The images to be processed.

  • lut

    A lookup table, containing 256 (or 65536 if self.mode==”I” and mode == “L”) values per band in the image. A function can be used instead, it should take a single argument. The function is called once for each possible pixel value, and the resulting table is applied to all bands of the image.

    It may also be an ImagePointHandler object:

    class Example(Image.ImagePointHandler):
      def point(self, im: Image) -> Image:
        # Return result
    

  • mode – Output mode (default is same as input). This can only be used if the source image has mode “L” or “P”, and the output has mode “1” or the source image mode is “I” and the output mode is “L”.

  • source_mode – The mode of the images. Default is auto detect.

Returns:

An Image object.

static quantize(images: ChunkedArray, colors: int = 256, method: int | None = None, kmeans: int = 0, palette: Image | None = None, dither: Dither = Dither.FLOYDSTEINBERG, source_mode: str | None = None)#

Convert the image to ‘P’ mode with the specified number of colors.

Parameters:
  • images – The images to be processed.

  • colors – The desired number of colors, <= 256

  • method

    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 – Integer greater than or equal to zero.

  • palette – Quantize to the palette of given PIL.Image.Image.

  • dither – 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).

  • source_mode – The mode of the images. Default is auto detect.

Returns:

A new image

static reduce(images: ChunkedArray, factor: int | tuple[int, int], box: tuple[int, int, int, int] | None = None, source_mode: str | None = None) Array#

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:
  • images – The images to be processed.

  • factor – A greater than 0 integer or tuple of two integers for width and height separately.

  • box – 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.

static resize(images: ChunkedArray, size: tuple[int, int] | list[int] | NumpyArray, resample: int | None = None, box: tuple[float, float, float, float] | None = None, reducing_gap: float | None = None, source_mode: str | None = None)#

Returns a resized copy of this image.

Parameters:
  • images – The images to be processed.

  • size – The requested size in pixels, as a tuple or array: (width, height).

  • resample – An optional resampling filter. This can be one of Resampling.NEAREST, Resampling.BOX, Resampling.BILINEAR, Resampling.HAMMING, Resampling.BICUBIC or Resampling.LANCZOS. If the image has mode “1” or “P”, it is always set to Resampling.NEAREST. Otherwise, the default filter is Resampling.BICUBIC. See: concept-filters.

  • box – An optional 4-tuple of floats providing the source image region to be scaled. The values must be within (0, 0, width, height) rectangle. If omitted or None, the entire source is used.

  • reducing_gap – Apply optimization by resizing the image in two steps. First, reducing the image by integer times using reduce(). Second, resizing using regular resampling. The last step changes size no less than by reducing_gap times. reducing_gap may be None (no first step is performed) or should be greater than 1.0. The bigger reducing_gap, the closer the result to the fair resampling. The smaller reducing_gap, the faster resizing. With reducing_gap greater or equal to 3.0, the result is indistinguishable from fair resampling in most cases. The default value is None (no optimization).

  • source_mode – The mode of the images. Default is auto detect.

Returns:

An Image object.

static rotate(images: ChunkedArray, angle: float, resample: Resampling = Resampling.NEAREST, expand: int | bool = False, center: tuple[float, float] | None = None, translate: tuple[int, int] | None = None, fillcolor: float | tuple[float, ...] | str | None = None, source_mode: str | None = None)#

Returns a rotated copy of this image. This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.

Parameters:
  • images – The images to be processed.

  • angle – In degrees counter clockwise.

  • resample – An optional resampling filter. This can be one of Resampling.NEAREST (use nearest neighbour), Resampling.BILINEAR (linear interpolation in a 2x2 environment), or Resampling.BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set to Resampling.NEAREST. See concept-filters.

  • expand – Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.

  • center – Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.

  • translate – An optional post-rotate translation (a 2-tuple).

  • fillcolor – An optional color for area outside the rotated image.

  • source_mode – The mode of the images. Default is auto detect.

Returns:

An Image object.

static size(images: ChunkedArray, source_mode: str | None = None) Array#

Image size, in pixels. The size is given as a 2-tuple (width, height).

Parameters:
  • images – The images to be processed.

  • source_mode – The mode of the images. Default is auto detect.

static width(images: ChunkedArray, source_mode: str | None = None) Array#

Image width, in pixels.

Parameters:
  • images – The images to be processed.

  • source_mode – The mode of the images. Default is auto detect.