Image.dice#

Image.dice(grid: int | tuple[int, int] | None = None, shape: int | tuple[int, int] | None = None, overlap: int | tuple[int, int] = 0) list[Self][source]#

Dice an image into a grid of subimages

Parameters:
  • grid (int or 2-tuple, optional) – grid (nw, nh) of output tiles, defaults to None

  • shape (int or 2-tuple, optional) – size (w, h) of output tiles in pixels, defaults to None

  • overlap (int or 2-tuple, optional) – overlap between adjacent tiles in pixels, defaults to 0

Returns:

a list of subimages in row-major order

Return type:

list of Image

The number and size of the subimages (tiles) can be specified in various ways:

  • grid=N create a grid of N x N subimages

  • grid=(N,M) create a grid of N x M subimages

  • shape=N create a grid of subimages of size N x N pixels

  • shape=(N,M) create a grid of subimages of size N x M pixels

If overlap is specified, the tiles will overlap. If shape is specified the number of tiles in each direction can increase. If grid is specified number of tiles remains as specified. If:

  • overlap=N overlap by N pixels in both directions

  • overlap=(N,M) overlap by N pixels in the horizontal direction and M pixels in the vertical direction

Note

If the image size is not an exact multiple of the grid size, the last rows and columns of the image will not be included in any of the tiles.

Example:

>>> from machinevisiontoolbox import Image
>>> mona = Image.Read("monalisa.png")
>>> subimages = mona.dice(grid=3)
>>> print(len(subimages))
9
>>> print(subimages[0])
Image: 225 x 233 (uint8), R:G:B
  R: span=[12, 254]; mean=141.176, 𝜎=47.0649; median=150
  G: span=[0, 224]; mean=114.96, 𝜎=39.0673; median=121
  B: span=[0, 112]; mean=42.7832, 𝜎=16.8952; median=42
>>> Image.Tile(subimages, bgcolor=(255,255,255)).disp()
<matplotlib.image.AxesImage object at 0x7f076e133ef0>

(Source code, png, hires.png, pdf)

../_images/machinevisiontoolbox-Image-dice-1.png
Seealso:

Tile