File size: 2,611 Bytes
ea2f7c7
 
 
 
 
 
 
91c0967
ea2f7c7
2578b1e
ea2f7c7
 
 
 
91c0967
 
 
 
ea2f7c7
 
91c0967
 
 
 
 
ea2f7c7
2578b1e
ea2f7c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab9bc0
ea2f7c7
 
 
 
 
 
 
 
 
 
2578b1e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from PIL import Image
import numpy as np
import os
import cv2

def resize_sofa(img):
    img = Image.fromarray(img)
    width, height = img.size 
    idx = np.argmin([width,height])
    newsize = (640, 640) # parameters from test script

    if idx==0:
        img1 = Image.new(img.mode, (height, height), (255, 255, 255))
        img1.paste(img, ((height-width)//2, 0))
        box = ( newsize[0]*(1-width/height)//2,
                0, 
                newsize[0]-newsize[0]*(1-width/height)//2,
                newsize[1])
    else:
        img1 = Image.new(img.mode, (width, width), (255, 255, 255))
        img1.paste(img, (0, (width-height)//2)) 
        box = (0, 
                newsize[1]*(1-height/width)//2, 
                newsize[0], 
                newsize[1]-newsize[1]*(1-height/width)//2)
    im1 = img1.resize(newsize)
    return im1,box

def resize_style(img):
    #img = Image.open(path)#"../style5.jpg")
    img = Image.fromarray(img)
    width, height = img.size
    idx = np.argmin([width,height])
    #print(width,height)

    if idx==0:
        top= (height-width)//2
        bottom= height-(height-width)//2
        left = 0
        right= width
    else:
        left = (width-height)//2
        right = width - (width-height)//2
        top = 0
        bottom = height

    newsize = (640, 640) # parameters from test script
    im1 = img.crop((left, top, right, bottom))

    copies = 8
    resize = (newsize[0]//copies,newsize[1]//copies)
    dst = Image.new('RGB', (resize[0]*copies,resize[1]*copies))
    im2 = im1.resize((resize))
    for row in range(copies):
        im2 = im2.transpose(Image.FLIP_LEFT_RIGHT)
        for column in range(copies):
            im2 = im2.transpose(Image.FLIP_TOP_BOTTOM)
            dst.paste(im2, (resize[0]*row, resize[1]*column))
    dst = dst.resize((newsize))
    return dst

def create_styledSofa(sofa,style):
    path_sofa,path_style = 'sofa.jpg','style.jpg'
    sofa.save(path_sofa)
    style.save(path_style)
    os.system("python3 test.py  --content "+path_sofa+" \
                    --style "+path_style+" \
                    --output . \
                    --vgg vgg_normalised.pth \
                    --decoder_path decoder_iter_160000.pth \
                    --Trans_path transformer_iter_160000.pth \
                    --embedding_path embedding_iter_160000.pth")
    styled_sofa = cv2.imread('sofa_stylized_style.jpg')

    return styled_sofa

# image = Image.open('sofa_office.jpg') 
# image.show() 
# image = np.array(image)
# image,box = resize_sofa(image)  
# image = image.crop(box)
# print(box)
# image.show()