Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PIL映射具有随机坐标的图像,在不使用这些坐标的情况下,将一个坐标放置在另一个坐标之上_Python_Loops_Python Imaging Library - Fatal编程技术网

Python 使用PIL映射具有随机坐标的图像,在不使用这些坐标的情况下,将一个坐标放置在另一个坐标之上

Python 使用PIL映射具有随机坐标的图像,在不使用这些坐标的情况下,将一个坐标放置在另一个坐标之上,python,loops,python-imaging-library,Python,Loops,Python Imaging Library,我正在尝试使用Python图像库(PIL)将3个图像放在一个具有随机坐标的背景图像中。我已经附上所有必要的图片就在下面的代码 #background = 800x400 #red,blue,green = 120x48 background = Image.open('background.png') red = Image.open('red.png') blue = Image.open('blue.png') green = Image.open('green.png') positi

我正在尝试使用Python图像库(PIL)将3个图像放在一个具有随机坐标的背景图像中。我已经附上所有必要的图片就在下面的代码

#background = 800x400
#red,blue,green = 120x48

background = Image.open('background.png')
red = Image.open('red.png')
blue = Image.open('blue.png')
green = Image.open('green.png')

positionxred = random.randint(0, 800)
positionyred = random.randint(0, 400)

positionxblue = random.randint(0, 800)
positionyblue = random.randint(0, 400)

positionxgreen = random.randint(0, 800)
positionygreen = random.randint(0, 400)

background.paste(red, (positionxred, positionyred), red)
background.paste(blue, (positionxblue, positionyblue), blue)
background.paste(green, (positionxgreen, positionygreen), green)

background.save("test.png")
附件:

背景

红色的

蓝色的

绿色的

试验



我的目标是,红色、蓝色和绿色图像的区域坐标不相同,因为如果它们相同,图像将保持在彼此的顶部,如所附的测试图像所示。
如您所见,红色、蓝色和绿色图像的大小为120x48,即5760个单位的面积。
背景图像为400x800,总面积为320000单位。

我需要一种方法,使每个图像的5760个面积单位不停留在另一个图像的顶部,使用一些循环命令,我应该如何继续?

当所有图像不重叠时,核心部分是重新尝试粘贴点:

from PIL import Image
import random

"""
[ref](https://www.geeksforgeeks.org/find-two-rectangles-overlap/)
"""
def is_overlap(l1, r1, l2, r2):
    if l1[0] > r2[0] or l2[0] > r1[0]:
        return False

    if l1[1] > r2[1] or l2[1] > r1[1]:
        return False

    return True

background = Image.open('background.png')
paste_image_list = [Image.open('red.png'), Image.open('blue.png'), Image.open('green.png')]
alread_paste_point_list = []

for img in paste_image_list:
    # if all not overlap, find the none-overlap start point
    while True:
        # left-top point
        # x, y = random.randint(0, background.size[0]), random.randint(0, background.size[1])

        # if image need in the bg area, use this
        x, y = random.randint(0, max(0, background.size[0]-img.size[0])), random.randint(0, max(0, background.size[1]-img.size[1]))

        # right-bottom point
        l2, r2 = (x, y), (x+img.size[0], y+img.size[1])

        if all(not is_overlap(l1, r1, l2, r2) for l1, r1 in alread_paste_point_list):
            # save alreay pasted points for checking overlap
            alread_paste_point_list.append((l2, r2))
            background.paste(img, (x, y), img)
            break

background.save("test.png")

# check like this, all three rectangles all not overlapping each other
from itertools import combinations
assert(all(not is_overlap(l1, r1, l2, r2) for (l1, r1), (l2, r2) in combinations(alread_paste_point_list, 2)))

您是否还希望将完整图像保留在400x800内,并且不允许从背景边缘进行裁剪?当前
random.randint(0800)
random.randint(0400)
允许左边缘值大于图像的宽度和高度,因为蓝色和黄色图像在这里:对不起,不工作,图像仍然重叠:有时在测试图像时,背景中没有显示,缺少蓝色图像在我的例子中,所有图像都显示在背景中,但图像仍然相互重叠,您是否将所有图像都放在同一文件夹中@davedwards@LucasTesch是的,所有图像都与脚本位于同一目录中。您是对的,在最初几次运行脚本时,所有图像通常会重叠,但在运行脚本3或4次后,脚本会生成缺少的
blue
image。@Lucastsch在实现is_重叠函数时出现错误,您可以重试新代码。