Created
June 25, 2017 02:07
-
-
Save nkt1546789/eb4bf9977dd6a32e706e17569bee4b87 to your computer and use it in GitHub Desktop.
Collage template generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
from scipy import stats | |
def generate_template(n, width, height, random_state=1, max_random_state=10000, offset=0): | |
L = [np.array([offset, offset, width-offset, height-offset])] | |
random_state_lists = stats.randint.rvs(0, max_random_state, size=(n-1, 4), random_state=random_state) | |
for random_state_list in random_state_lists: | |
n_areas = len(L) | |
if n_areas == 1: | |
i = 0 | |
else: | |
p = np.repeat(1 / (n_areas + i), n_areas) | |
x = stats.multinomial.rvs(1, p, size=1, random_state=random_state_list[0])[0] | |
i = x.argmax() | |
y = stats.bernoulli.rvs(0.5, size=1, random_state=random_state_list[1])[0] | |
if y == 0: | |
b = stats.uniform.rvs(L[i][0], L[i][2] - L[i][0], size=1, random_state=random_state_list[2])[0] | |
#b = stats.uniform.rvs(L[i][0], L[i][2] - L[i][0], size=1, random_state=random_state_list[2])[0] | |
else: | |
b = stats.uniform.rvs(L[i][1], L[i][3] - L[i][1], size=1, random_state=random_state_list[3])[0] | |
#b = stats.uniform.rvs(L[i][1], L[i][3] - L[i][1], size=1, random_state=random_state_list[3])[0] | |
if y == 0: | |
area1 = np.array([L[i][0], L[i][1], b-offset/2, L[i][3]]) | |
area2 = np.array([b+offset/2, L[i][1], L[i][2], L[i][3]]) | |
else: | |
area1 = np.array([L[i][0], L[i][1], L[i][2], b-offset/2]) | |
area2 = np.array([L[i][0], b+offset/2, L[i][2], L[i][3]]) | |
L.pop(i) | |
L.append(area1) | |
L.append(area2) | |
return L | |
L = generate_template(7, 1, 1, random_state=3, offset=0) | |
colors=["b", "g", "r", "c", "m", "y", "k"] | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
for l_i, c_i in zip(L, colors): | |
rect = plt.Rectangle(xy=[l_i[0], l_i[1]], width=l_i[2] - l_i[0], height=l_i[3] - l_i[1], facecolor=c_i, fill=True) | |
ax.add_patch(rect) | |
plt.tight_layout() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment