mardi 23 mars 2021

(Python)How can I avoid overlapping of multiple images, while pasting on one background image?

I am pasting multiple images randomly on the custom white background image, but I cannot figure out how can I avoid images to overlap each other? So far, I wasn't able to find much on it. Any help pointing me in the right direction would be appreciated.

  for image_to_paste in os.listdir(path):

    image_to_paste = Image.open(os.path.join(path, image_to_paste))
    i_width, i_height = image_to_paste.size
    b_width, b_height = back_image.size
    img = random.randint(0, max(0, back_image.size[0]-image_to_paste.size[0])), \
        random.randint(0, max(0, back_image.size[1]-image_to_paste.size[1]))
    back_image.paste(image_to_paste, img)
    n += 1
    
    if n == 5:
        back_image.save(f'path to save output images\_{n}.jpg')
        back_image = Image.new('RGB', (1440, 900), (255, 255, 255, 255))
        n = 0



Aucun commentaire:

Enregistrer un commentaire