Skip to content

Instantly share code, notes, and snippets.

@preddy5
Created September 13, 2021 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preddy5/810e2eb222f92f17db416686eeea616a to your computer and use it in GitHub Desktop.
Save preddy5/810e2eb222f92f17db416686eeea616a to your computer and use it in GitHub Desktop.
def create_img(xy, elements, background, element_id, thetas, width=256, height=256, scale=1.0, gen_img=None, color=None, background_img=None):
scale= int(scale)
canvas_size = [width*scale, height*scale]
xy = xy*scale
for i in range(len(elements)):
w_e = elements[i].size[0]
h_e = elements[i].size[1]
elements[i] = elements[i].resize((w_e*scale, h_e*scale), resample=PIL.Image.BICUBIC)
namespace_map = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H'}
alpha_mask = 0
if gen_img == None:
gen_img = Image.new('RGBA', tuple(canvas_size),
(background[0], background[1], background[2], alpha_mask)) # create new white image and paste source image into the center
empty = Image.new('RGBA', tuple(canvas_size),
(0, 0, 0, 0))
total_number = len(element_id)
for idx in range(len(element_id)):
(x, y), i, angel = xy[idx], element_id[idx], thetas[idx]
x = math.ceil(x*width)
y = math.ceil(y*height)
element = elements[i].rotate(-angel*3.14159 * 57.2958, resample=Image.BILINEAR)
if type(color)!=type(None):
image = to_numpy(element)
# image[:,:,:3]=1
image[:, :, :3] = image[:, :, :3]*color[None, idx, :]
element = to_pil(image)
w_2 = math.ceil(element.size[0]/2)
h_2 = math.ceil(element.size[1]/2)
empty.paste(element, box=(x- w_2, y- h_2, x+w_2 , y+h_2 ))
print('{:02d}-{}'.format(total_number-idx, namespace_map[i]), x/(2*width), y/(2*height))
empty.save(folder_final + '{:02d}-{}'.format(total_number-idx, namespace_map[i]) + '.png')
gen_img = Image.alpha_composite(gen_img, empty)
empty = Image.new('RGBA', tuple(canvas_size),
(0, 0, 0, 0))
if type(background_img)==type(None):
bg = Image.new('RGBA', tuple(canvas_size),
(background[0], background[1], background[2],
255)) # create new white image and paste source image into the center
else:
bg = background_img
img_w_bg = Image.alpha_composite(bg, gen_img)
return gen_img, img_w_bg
def calculate_color(c_variables, background):
z = torch.exp(c_variables[6] / 25)
z_hat = z / (z + 2000)
bg_hat = 2000 / (z + 2000)
# color_value_sig = torch.clamp(c_variables[8] / 20, min=0, max=1)#torch.nn.functional.sigmoid(c_variables[8])
color_scaled = c_variables[8] / 20
color_value_sig = torch.max(torch.min(color_scaled, 1 + (color_scaled - 1) * 0.001), 0.001 * color_scaled)
# color_values = (color_value_sig).to("cpu", torch.float).data.numpy()
color_values = ((color_value_sig * z_hat[:, None] + bg_hat[:, None] * background[:, :, 0, 0])).to("cpu",
torch.float).data.numpy()
return color_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment