How to position images within a canvas while avoiding overlapping I have a canvas. On this canvas, I want to position 1, 2 or 3 images.  The images can be positioned anywhere on the canvas just as long as they're within the canvas. The images move by their top-left corner.  To make sure that the images do not leave the canvas, I am doing this:
image_topLeft_x_position = random.randint(0, (canvas_width - logo_width))

image_topLeft_y_position = random.randint(0, (canvas_height - logo_height))

I can save these locations in a list of tuples like this:
locations = [(image_topLeft_x_position, image_topLeft_y_position)]

After saving a location, I want to generate new locations within the canvas that don't overlap with the previous ones.  However, I've been stuck on this problem for well over a day. Any help is appreciated.
If there is a place for the new image to fit, there must be a place where the left edge of the new image is aligned with either the left edge of the canvas or of some previous image. The same true for the top edge of the new image.  So, if you've already placed 2 images, you only have to try 3 possibilities for the left edge and 3 possibilities for the top edge, or a total of 9 candidate positions in total.  Given a candidate position, you should be easily able to test whether a new image can be placed there without overlapping any prior image.