-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplot_attributes.py
58 lines (46 loc) · 1.49 KB
/
plot_attributes.py
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
from PIL import Image
import sys, os
import numpy as np
import imageio
from scipy import ndimage
def cut_image(attribute, i):
path = attribute + '/'
filename = str(i) + '.png'
img = Image.open(path + filename)
# then merge all!
f = Image.new('RGBA', img.size, 'black')
f = Image.alpha_composite(f, img.convert('RGBA'))
classname = attribute + '_%d' % i
f.save('%s.png' % classname)
img = Image.open('%s.png' % classname)
# crop to 64 * 64
width = 64; height = 64
imgwidth, imgheight = img.size
N_width = imgwidth / width
N_height = imgheight / height
i = 0; j = 14
box = (i*width, j*height, (i+1)*width, (j+1)*height)
a = img.crop(box)
a.convert('RGB')
if attribute == 'body':
color = a.getpixel((32, 32))
print color
a = Image.new('RGBA', a.size, color)
os.remove('%s.png' % classname)
return a
if __name__ == '__main__':
n_class = 6
attributes = ['body', 'bottomwear', 'topwear', 'hair']
for attr in attributes:
images = []
for i in xrange(n_class):
images.append(cut_image(attr, i))
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save(attr + '.png')