forked from ErikaKettleson/ShowMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
354 lines (270 loc) · 11.1 KB
/
seed.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""Utility file to seed database from pillow data/"""
# import datetime
from sqlalchemy import func
from model import Show, Show_Color, Brand, Color, connect_to_db, db
from server import app
from PIL import Image, ImageDraw, ImageColor, ImageFilter
import requests
from StringIO import StringIO
import os
import sys
from webcolors import *
import pdb
years = [2017]
seasons = ['fall', 'spring']
# brands = {"Carven": "/carven"}
brands = {
"Acne Studios": "/acne-studios",
"Alexander McQueen": "/alexander-mcqueen",
"Alexander Wang": "/alexander-wang",
"Altuzarra": "/altuzarra",
"Ann Demeulemeester": "/ann-demeulemeester",
"Antonio Berardi": "/antonio-berardi",
"Balenciaga": "/balenciaga",
"Balmain": "/balmain",
"Bottega Veneta": "/bottega-veneta",
"Calvin Klein": "/calvin-klein",
"Carven": "/carven",
"Celine": "/celine",
"Chanel": "/chanel",
"Christian Dior": "/christian-dior",
"Christopher Kane": "/christopher-kane",
"Comme Des Garcons": "/comme-des-garcons",
"Derek Lam": "/derek-lam",
"Isabel Marant": "/isabel-marant",
"Dolce Gabbana": "/dolce-gabbana",
"Dries Van Noten": "/dries-van-noten",
"Etro": "/etro",
"Fendi": "/fendi",
"Giambattista Valli": "/giambattista-valli",
"Givenchy": "/givenchy",
"Gucci": "/gucci",
"Hermes": "/hermes",
"J.W. Anderson": "/j-w-anderson",
"Junya Watanabe": "/junya-watanabe",
"Kenzo": "/kenzo",
"Lanvin": "/lanvin",
"Loewe": "/loewe",
"Louis Vuitton": "/louis-vuitton",
"Maison Margiela": "/maison-martin-margiela",
"Marc Jacobs": "/marc-jacobs",
"Marni": "/marni",
"Mary Katrantzou": "/mary-katrantzou",
"Michael Kors": "/michael-kors-collection",
"Miu Miu": "/miu-miu",
"Missoni": "/missoni",
"Oscar de la Renta": "/oscar-de-la-renta",
"Prada": "/prada",
"Proenza Schouler": "/proenza-schouler",
"Roksanda": "/roksanda",
"Stella McCartney": "/stella-mccartney",
"Saint Laurent": "/saint-laurent",
"Tory Burch": "/tory-burch",
"Valentino": "/valentino",
"Vetements": "/vetements",
}
def image_contents(img):
print "format: ", img.format
print "size: ", img.size
print "mode: ", img.mode
r, g, b = img.getpixel((1, 1))
print "r:", r, "g:", g, "b", b
def closest_color(color):
# buckets the colors to convert colors from rgb to css friendly names
min_colors = {}
# loops over css3 named hex/color items (138 pairs)
# next, pull r, g, b, values from image color tuple
# then convert hex to rgb with just key (hex values)
for key, name in css3_hex_to_names.items():
r_c, g_c, b_c = hex_to_rgb(key)
# an example might be 124 125 0
# so r = (124 - color[0]) ^ 2
# color is the inputted rgb value tuples
r = (r_c - color[0]) ** 2
g = (g_c - color[1]) ** 2
b = (b_c - color[2]) ** 2
min_colors[(r + g + b)] = name
# print "MIN COLORS", min_colors
# print "MIN KEYS ONLY", min_colors[min(min_colors.keys())]
return min_colors[min(min_colors.keys())]
# def show_colors(final_colors):
# # A FXN TO AGGREGATE TOP COLORS BY SHOW
# # final_colors is PER image, need to aggregate all final_colors to show colors
# show_colors = []
# for color in final_colors:
# show_colors.append(color)
# show_colors = final_colors[:6]
# return show_colors
def get_color_name(color):
# pillow errors out if it doesnt have the exact color name to rgb
# print "at get_color_name"
try:
closest_name = actual_name = rgb_to_name(color)
except ValueError:
closest_name = closest_color(color)
actual_name = None
return closest_name
# actual_name, closest_name = get_color_name(color)
# print "closest color name: ", closest_name
def get_colors(img, img2):
# this function takes in bg & foreground colors and returns top img colors
# 1. convert image to palette format
img = img.convert('P')
img2 = img2.convert('P')
# 2. convert imgs to RGB for getcolors fxn - returns rgb tuples
bg_colors = (img2.convert('RGB').getcolors())
foreground_colors = (img.convert('RGB').getcolors())
# 3. set up our lists to capture top colors for bg & foreground
final_bg_colors = []
final_colors = []
# 4. set up empty list to capture named final colors
final_named_colors = []
named_bg_colors = []
named_foreground_colors = []
# 6. these two loops return lists of named colors
for count, rgb in bg_colors:
named_bg_colors.append(get_color_name(rgb))
for count, rgb in foreground_colors:
named_foreground_colors.append(get_color_name(rgb))
# 7. these create color/count pairs
count_bg_colors = {color: named_bg_colors.count(color) for color in named_bg_colors}
count_foreground_colors = {color: named_foreground_colors.count(color) for color in named_foreground_colors}
# 8. these take the color/count pairs and sort them
sorted_bg_colors = sorted(count_foreground_colors.items(), key=lambda x: x[1], reverse=True)[:10]
sorted_foreground_colors = sorted(count_foreground_colors.items(), key=lambda x: x[1], reverse=True)[:10]
# 9. next i have to append top bg colors to final_bg_colors to compare foreground againsr
final_bg_colors.append(sorted_bg_colors)
# 10. next i need to compare the fg colors against bg colors & append to the fg final color list
for color, count in sorted_foreground_colors:
if color not in final_bg_colors:
final_colors.append(color)
else:
pass
final_named_colors = final_colors[:10]
return final_named_colors
def crop_image(img):
# crop the image and background sample
# print "hello at crop!"
width, height = img.size
left_box = ((.30) * width)
top_box = ((.15) * height)
width_box = ((.45) * width)
height_box = ((.66) * height)
box = (left_box, top_box, left_box+width_box, top_box+height_box)
# print "box", box
left_bg = ((0) * width)
top_bg = ((0) * height)
width_bg = ((.25) * width)
height_bg = ((.75) * height)
bg_box = (left_bg, top_bg, left_bg+width_bg, top_bg+height_bg)
# print "bg_box", bg_box
crop = img.crop(box)
bg_crop = img.crop(bg_box)
return get_colors(crop, bg_crop)
def pillow_loop(img_urls):
# for url in img_urls:
# print "URL:", img_urls
response = requests.get(img_urls)
img = Image.open(StringIO(response.content))
# print "heading to crop"
return crop_image(img)
def img_urls(show_url):
# this returns a list of images for a show
image_urls = set()
# print "getting: %s" % show_url
r = requests.get(show_url)
if r.status_code == 200:
print "got a hit!"
html_body = r.text.split(",")
for l in html_body:
match = re.match(r'.*(http:.*/_(?!ARC|AG|UMB).*.jpg).*', l)
# match = re.match(r'.*(http:\/\/assets.vogue.com\/photos\/.*\/master\/pass\/.*.[jpg|JPG]).*', l)
if match:
image_urls.add(match.group(1))
# print "image urls:", image_urls
# print len(image_urls)
# image_urls = list(image_urls)
return list(image_urls)
# pillow_loop(image_urls)
def feed_urls():
# main fxn to call url generator and aggregate top colors by show
generated_urls = {year: {season: {brand: 'http://www.vogue.com/fashion-shows/{}-{}-ready-to-wear{}'.format(season, year, brand_url) for brand, brand_url in brands.items()} for season in seasons} for year in years}
# print "generated urls:", generated_urls
for year in generated_urls.keys():
for season in generated_urls[year].keys():
for brand, brand_url in generated_urls[year][season].items():
print "generated urls", generated_urls
print "brand url:", brand_url
print "season", season
print "brand name:", brand
show = load_show(year, season, brand)
image_urls = img_urls(brand_url)
top_show_colors = []
for url in image_urls:
final_named_colors = pillow_loop(url)
for color in final_named_colors:
top_show_colors.append(color)
print "at feed urls, top show colors: ", top_show_colors
count_colors = {color: top_show_colors.count(color) for color in top_show_colors}
top_show_colors = sorted(set(top_show_colors), key=count_colors.get, reverse=True)
top_show_colors = top_show_colors[:10]
load_show_colors(top_show_colors, brand, year, season)
# jus have to pass top show color & show (or just show.show_id)
def load_brands(brands):
"""Load brands into database."""
print "Brands"
for key, value in brands.items():
brand_name = key
brand = Brand(brand_name=brand_name)
# We need to add to the session or it won't ever be stored
db.session.add(brand)
# Once we're done, we should commit our work
db.session.commit()
def load_colors():
"""Load colors/hex from css3 dict into database."""
print "Color"
for key, value in css3_hex_to_names.items():
color_hex, color_name = key, value
color = Color(color_hex=color_hex,
color_name=color_name)
db.session.add(color)
# Once we're done, we should commit our work
db.session.commit()
def load_show(year, season, brand):
"""Load shows into database."""
print "Show"
brand_id = db.session.query(Brand).filter_by(brand_name=brand).one().brand_id
year = 2017
show = Show(season=season,
year=year,
brand_id=brand_id)
# We need to add to the session or it won't ever be stored
db.session.add(show)
# Once we're done, we should commit our work
db.session.commit()
return show
def load_show_colors(top_show_colors, brand, year, season):
"""Set value for each shows top 6 colors & seed """
# show_id = db.session.query(Show.season == season,
# Show.year == year,
# (Show.brands.brand_name == brand)).one().show_id
show_id = db.session.query(Show).join(Brand).filter(Show.season == season).\
filter(Show.year == year).\
filter(Brand.brand_name == brand).one().show_id
# db.session.add(show_id)
# color_id = db.session.query(Color.color_name == (color for colors in top_show_colors)).one().color_id
for color in top_show_colors:
print "color: ", color
color_id = db.session.query(Color).filter(Color.color_name == color).one().color_id
print "color_id:", color_id
show_color = Show_Color(show_id=show_id, color_id=color_id)
db.session.add(show_color)
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
db.create_all()
load_brands(brands)
load_colors()
top_show_colors, year, brand, season = feed_urls()
load_show(year, season, brand)
load_show_colors(top_show_colors, brand, year, season)