Combining different snippets

To combine different snippets, we provide an ArraySnip class which can hold any number of snippets. The output of the array snippet is the concatenation of the outputs of the snippets in the array.

Creating an ArraySnip

Let’s assume we have two snippets text and image and we want to combine them. We can create an ArraySnip as follows:

from PIL import Image
from snip.snippets import ArraySnip, ImageSnip, TextSnip

text = TextSnip(
    "Hello, World!",
)
image = ImageSnip(
    Image.open("test.png"),
)

array = ArraySnip(
    [text, image],
)
array
../_images/e7546cdab23b4bc7c757d391b8ddb5ae9f8d136acac3f5027c02c72300d53da3.png

As you might have noticed, the image and text are nested above each other. This is because we have not set any coordinates for the inner snippets. As usual, we can set the coordinates for the inner snippets by using the x and/or y attributes.

image.y = 30
array
../_images/fd2e7ecc41ce1f0f1557c637113ce51a4950f2f699f6ed56b30a672fbbfb59df.png

Array snips contain their own coordinate system. The coordinates of the inner snippets are relative to the top-left corner of the array snippet. I.e. if we change the position of the array snippet, the position of the inner snippets will change accordingly.

array.y = 300
array.x = 300
array
../_images/de8c89ad128d53cc2bf3a014268985dd590371e27e9c2f70e78cc62270a9c93d.png