Example #1
0
def makeImg(images):
    gripper = Rectangle(295, 125, 120, 125)
    obstacles = [gripper]
    # grip = Image.new('RGB', (125,125), color=400)
    # white background
    clusterBack = Image.open("scripts/objects/back.png")
    # background of result
    background = Image.open("scripts/objects/" + backName + ".png")
    # radius for checking if objects are inside circle and positioning center
    radius = 180 # 180
    xCenter = 160
    yRadius = 210
    yCenter = 210
    # x from 0 to 310; y from 155 - sqrt(-(x-310)x) to 155 + sqrt(-(x-310)x)
    x = np.random.normal(xCenter, (radius*1.2))
    # print (radius/2), yRadius/2
    # field = Image.new('RGB', (int(radius*1.2),int(yRadius*1.2)), color=400)
    # yRadius = math.sqrt(-1 * x * (x - radius * 2))
    y = np.random.normal(yCenter, (yRadius*1.2))
    center = (x,y)
    order = []
    for i in range(1, 5):
        # selects objects in random order
        j = random.randrange(0, len(images))
        name = "scripts/objects/" + "obj" + str(images[j]) + ".png"
        order.append(images[j])
        images.pop(j)
        obj = Image.open(name)
        objX = x
        objY = y
        # splits objects into four quadrants around point
        if (i % 2 == 0):
            objX -= w(obj)
        if (i > 2):
            objY -= h(obj)
        z = Rectangle(objX, w(obj), objY, h(obj))
        # foundLoc = (not z.hasCollision(obstacles)) and z.insideCircle(radius, radius, radius)
        foundLoc = (not z.hasCollision(obstacles) and z.insideRectangle((0, 2*yRadius), (2*radius,0)))
        if not foundLoc:
            # print "Failed: ", (x,y)
            return None, None, order
        else:
            pasteOn(clusterBack, obj, objX, objY)
        obstacles.append(z)
    # Random angle from -30 to 30; noise
    theta = (random.random() * 60) - 30
    clusterBack = clusterBack.rotate(theta)
    clusterBack = makeTransparent(clusterBack)
    # pasteOn(background, field, xCenter - int(radius/2*1.2), yCenter- int(yRadius/2*1.2))
    # pasteOn(background, field, 0, 0)
    pasteOn(background, clusterBack, 0, 0)
    return background, center, order