Create an image composed by different images

Question:

I have the a list of images (each of these image is a separate file), let’s say they are some jigsaw puzzle pieces and, for each of them, I know it’s position (x,y) and rotation in the complete puzzle.

How can I show the complete puzzle by stitching each of these pieces together in a single image (given that i know where to put each of them)?

I don’t know if this is important but the pieces are not of regular shape (e.g. they are not squares), and they are all of different sizes

EDIT:

For the moments it seems to be working without the rotation but there is another problem, the pieces seems to not have a transparent background but rather a black one.
I have loaded them with opencv2 in the following way:

import glob
folder = './img/2/frag_eroded/'
frags = []
files = glob.glob(folder+"/*.png")
for file in files:
    image = cv2.imread(file, cv2.IMREAD_UNCHANGED)
    image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
    frags.append(image)

Example of resulting image, you can kinda see the the squares around each piece and see how the pieces overlap with their "background" that should be transparent rather then black
enter image description here

Asked By: Mario Turco

||

Answers:

This depends on how you want to handle it when there’s an overlapping transparent area.

Suppose all pixels are either transparent or opaque, and

Suppose each image has RGBA (4-channels including alpha),
then you can set all RGB values to zero whenever the pixel is transparent.

Then proceed to add the smaller images to a bigger canvas (initialized to be all zeros RGB). The canvas can either have an alpha layer or not, depending on your preference.

Beware the canvas is big enough to contain all of them. So the first step here would be to make a large enough matrix / opencv image.

How to add images: https://stackoverflow.com/a/68878529/19042045

Answered By: Neo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.