import PIL
from PIL import Image, ImageDraw, ImageFilter
from backgroundsubtractuse import subtractbackground
import random
import os

#def collagemake(scape, counter):
def collagemake(scape):

    background = Image.open('C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/background.jpg')
    character1_search = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/character1.jpg'
    character2_search = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/character2.jpg'
    thing1_search = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/thing1.jpg'

    character1_png = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/character1.png'
    character2_png = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/character2.png'
    thing1_png = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/thing1.png'

    subtractbackground(character1_search, character1_png)
    subtractbackground(character2_search, character2_png)
    subtractbackground(thing1_search, thing1_png)

    character1 = Image.open(character1_png)
    character2 = Image.open(character2_png)
    thing1 = Image.open(thing1_png)

    w, h = background.size
    new_height  = int(h/3)

    w_im, h_im = character1.size
    new_width = int(new_height * w_im / h_im) 
    character1_resize = character1.resize((new_width, new_height))

    w_im, h_im = character2.size
    new_width = int(new_height * w_im / h_im) 
    character2_resize = character2.resize((new_width, new_height))

    w_im, h_im = thing1.size
    new_width = int(new_height * w_im / h_im) 
    thing1_resize = thing1.resize((new_width, new_height))

    collage = background.copy()
    collage.paste(thing1_resize,(random.randint(0,w), random.randint(0,h)), mask=thing1_resize)
    collage.paste(character1_resize, (random.randint(0,w), random.randint(0,h)), mask=character1_resize)
    collage.paste(character2_resize, (random.randint(0,w), random.randint(0,h)), mask=character2_resize)


    # pic_count = counter
    # while pic_count != 0:
    #     dreamthing_search = Image.open('C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/dreamthing' + str(pic_count) + '.jpg')
    #     dreamthing_png = 'C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images/dreamthing' + str(pic_count) + '.png'
    #     subtractbackground(dreamthing_search, dreamthing_png)
    #     dreamthing = Image.open(dreamthing_png)
    #     w_im, h_im = dreamthing.size
    #     new_width = int(new_height * w_im / h_im) 
    #     dreamthing_resize = thing1.resize((new_width, new_height))
    #     collage.paste(dreamthing_resize, (random.randint(0,w), random.randint(0,h)))
    #     pic_count= pic_count - 1

    os.chdir('C:/Users/Ainarikiaz/Documents/Year 2 BA DESIGN/2.2/collagedream/images')
    collage.save(scape + '-dreamscape.jpg', quality=95)