#!/usr/local/bin/python3

import json
import random
from PIL import Image
from pprint import pprint

file_loc = "../images/croppedPokeParts/"

def get_metadata():
  f = open("../metadata/body.json", 'r')
  bodies = json.loads(f.read())

  f = open("../metadata/head.json", 'r')
  heads = json.loads(f.read())

  return bodies, heads


# paste of the paste coords of the first image
# pinhole_a is the pinhold for the first image
# pinhole_b is the pinhold for the second image
# returns the coords to paste b to match pinholes
def get_pinhole_match(paste, pinhole_a, pinhole_b, imagea, imageb):
  x = paste[0] + pinhole_a[0] - pinhole_b[0]
  y = paste[1] + (imagea.size[1] - pinhole_a[1]) - pinhole_b[1]
  return (x, y)


def generate_image():

  bodies, heads = get_metadata()
  body = random.choice(bodies)
  head = random.choice(heads)

  pprint(body)
  pprint(head)


  body_image = Image.open(file_loc+body["filename"])
  head_image = Image.open(file_loc+head["filename"])

  master = Image.new(
      mode='RGBA',
      size=(1000, 1000),
      color=(0,0,0,0))

  body_pinhole = body["needs"][0]["pinhole"]
  head_pinhole = head["pinhole"]


  master.paste(body_image,(500, 500))
  coords = get_pinhole_match((500, 500),
                              body_pinhole, head_pinhole,
                              body_image, head_image)
  master.paste(head_image, coords, head_image)
  master.save("../site/static/imgs/horror.png")







@@ -136,16 +136,48 @@
 arts/%22%0A%0A
+def get_metadatalist():%0A  pass%0A%0A
 def get_
@@ -188,16 +188,16 @@
 data():%0A
-
   f = op
@@ -335,16 +335,89 @@
 ead())%0A%0A
+  f = open(%22../metadata/tail.json%22, 'r')%0A  tails = json.loads(f.read())%0A%0A
   return
@@ -430,16 +430,23 @@
 s, heads
+, tails
 %0A%0A%0A# pas
@@ -826,16 +826,40 @@
 (x, y)%0A%0A
+def paste_in():%0A  pass%0A%0A
 %0Adef gen
@@ -967,16 +967,46 @@
 e(heads)
+%0A  tail = random.choice(tails)
 %0A%0A  ppri
@@ -1136,16 +1136,69 @@
 ename%22%5D)
+%0A  tail_image = Image.open(file_loc+tail%5B%22filename%22%5D)
 %0A%0A  mast
@@ -1359,17 +1359,49 @@
 nhole%22%5D%0A
+  tail_pinhole = tail%5B%22pinhole%22%5D
 %0A
-
 %0A  maste
@@ -1623,32 +1623,349 @@
 ds, head_image)%0A
+  body_pinhole = body%5B%22needs%22%5D%5B1%5D%5B%22pinhole%22%5D%0A  coords = get_pinhole_match((500,500),%0A                             body_pinhole,tail_pinhole,%0A                             body_image, tail_image)%0A  master.paste(tail_image, coords, tail_image)%0A  %0A # for i in body_pinhole%5B%22needs%22%5D:%0A #   body_pinhole = i%5B%22pinhole%22%5D%0A    %0A
   master.save(%22.
@@ -1996,14 +1996,8 @@
 r.png%22)%0A
-%0A%0A%0A%0A%0A%0A

add tail