imgutils.data.layer
istack
- imgutils.data.layer.istack(*items: str | PathLike | bytes | bytearray | BinaryIO | Image | Tuple[str | PathLike | bytes | bytearray | BinaryIO | Image, float | ndarray] | Tuple[str, float | ndarray], size: Tuple[int, int] | None = None) Image [source]
- Overview:
Layer multiple images (which may contain transparent areas) and color blocks together into a new image, similar to a layering technique in PS.
- Parameters:
items – The layers that need to be stacked. If a PIL object or the file path of an image is given, the image will be used as a layer; if a color is given, the color will be used as a layer. Additionally, if a tuple is given, the second element represents the transparency, with a value range of \(\left[0, 1\right]\). It can be a float type or a two-dimensional numpy array in the format of
float32[H, W]
which represents the transparency of each position.size – The size of the target image. By default, the size of the first image object in the items list will be used. However, when all layers are solid colors, this parameter is required.
- Returns:
Stacked image.
- Examples::
>>> from imgutils.data import istack >>> >>> # pure color >>> istack('lime', 'nian.png').save('nian_lime.png') >>> >>> # transparency >>> istack(('yellow', 0.5), ('nian.png', 0.9)).save('nian_trans.png') >>> >>> # custom mask >>> import numpy as np >>> from PIL import Image >>> width, height = Image.open('nian.png').size >>> hs1 = (1 - np.abs(np.linspace(-1 / 3, 1, height))) ** 0.5 >>> ws1 = (1 - np.abs(np.linspace(-1, 1, width))) ** 0.5 >>> nian_mask = hs1[..., None] * ws1 # HxW >>> istack(('nian.png', nian_mask)).save('nian_mask.png')
The result should be