Image.paste#

Image.paste(pattern: Any, pt: Any, method: str = 'set', position: str = 'topleft', copy: bool = False, zero: bool = True) Image[source]#

Paste an image into an image

Parameters:
  • pattern (Image, ndarray(H,W)) – image to be pasted

  • pt (array_like(2)) – coordinates (u,v) where pattern is pasted

  • method (str) – options for image merging, one of: 'set' [default], 'mean', 'add'

  • position (str, optional) – pt is one of: 'topleft' [default] or 'centre'

  • copy (bool, optional) – retained for backward compatibility, ignored

  • zero (bool, optional) – zero-based coordinates (True, default) or 1-based coordinates (False)

Raises:

ValueError – pattern is positioned outside the bounds of the image

Returns:

new image with pasted pattern

Return type:

Image

Pastes pattern into the image and returns a new image. The pattern can be incorporated into the specified image by:

method

description

'set'

overwrites the pixels in image

'add'

adds to the pixels in image

'mean'

sets pixels to the mean of the pixel values in pattern and image

The position of the pasted pattern in the image can be specified by its top left corner (umin, vmin) or its centre in the image.

Example:

>>> from machinevisiontoolbox import Image
>>> img1 = Image.Constant(10, size=5)
>>> pattern = Image([[11, 12], [13, 14]])
>>> img1.copy().paste(pattern, (1,2)).print()
   10 10 10 10 10
   10 10 10 10 10
   10 11 12 10 10
   10 13 14 10 10
   10 10 10 10 10
>>> img1.copy().paste(pattern, (1,2), method='add').print()
   10 10 10 10 10
   10 10 10 10 10
   10 21 22 10 10
   10 23 24 10 10
   10 10 10 10 10
>>> img1.copy().paste(pattern, (1,2), method='mean').print()
   10 10 10 10 10
   10 10 10 10 10
   10 10 11 10 10
   10 11 12 10 10
   10 10 10 10 10

Note

  • Pixels outside the pasted region are unaffected.

  • The input image is not modified.

  • For position='centre' an odd sized pattern is assumed. For an even dimension the centre pixel is the one at dimension / 2.

  • Multi-plane images are supported.

  • If the pattern is multiplane and the image is singleplane, the image planes are replicated and colororder is taken from the pattern.

  • If the image is multiplane and the pattern is singleplane, the pattern planes are replicated.