-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbSortToJSON.py
190 lines (144 loc) · 5.44 KB
/
thumbSortToJSON.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
__author__ = 'Gabriel'
from skimage import data
import skimage.transform
from numpy import *
import pickle
from matplotlib.pyplot import *
import os
"""
Viusalize the sorted thumbnails, tools to set cutoff by hand, and
finally generate a JSON File.
"""
"""
This section populates clickedList using a GUI
usage example 1:
thumbs = pickle.load(open('D:\\memeproject\\ThumbsnailSummaryTitles','r'))
(thumbList, a) = compareAll("test.jpg", thumbs):
clickedList = previewThumbs(thumbList, (20,20))
usage example 2:
memes = json.load(open(r'D:\memeproject\code\html\memes\3rd_World_Success_Kid.js','r'))
n = int(ceil(sqrt(len(memes))))
clickedList = previewThumbs([meme['id'] + '.jpg' for meme in memes], (n,n))
"""
def previewThumbs(thumbList, dims, plottitle=""):
tsize = 50
clickedList = []
thumbdir = 'D:\\memeproject\\thumbnails\\'
pview = uint8(np.zeros((tsize*dims[0],tsize*dims[1],3)));
for i in range(0,dims[0]*dims[1]):
if i >= len(thumbList) :
break
# Sometimes images have 4 channels. Crop it to 3
pthumb = skimage.transform.resize(data.load(thumbdir + thumbList[i])[:,:,0:3], (tsize,tsize))
pthumb = uint8(255*pthumb);
x = i/dims[1]
y = i - dims[1]*(i/dims[1])
pview[tsize*x:tsize*(x+1), tsize*y:tsize*(y+1),:] = pthumb
pviewOriginal = copy(pview)
# Generate Plot and assign callback on click
thumbPlot = figure()
ax = thumbPlot.add_subplot(111)
imageWindow = ax.imshow(pview)
title(plottitle)
def coord2ij(coord):
# Coordinate to (i,j) position on screen
return (coord/dims[1], coord%dims[1])
def highlight(coord):
ij = coord2ij(coord)
x = (ij[0]*tsize,(ij[0]+1)*tsize)
y = (ij[1]*tsize,(ij[1]+1)*tsize)
pview[ x[0]:x[1] , y[0]:y[1] ] = pview[ x[0]:x[1] , y[0]:y[1] ] + 10
return pview
def setborder(coord):
ij = coord2ij(coord)
x = (ij[0]*tsize,(ij[0]+1)*tsize)
y = (ij[1]*tsize,(ij[1]+1)*tsize)
pview[ x[0]:x[1] , y[0]:y[1] ] = 0
pview[ (x[0] + 3):(x[1] - 3) , (y[0] + 3):(y[1] - 3) ] = 255
pview[ (x[0] + 6):(x[1] - 6) , (y[0] + 6):(y[1] - 6) ] = pviewOriginal[ (x[0] + 6):(x[1] - 6) , (y[0] + 6):(y[1] - 6) ]
return pview
def unhighlight(coord):
ij = coord2ij(coord)
x = (ij[0]*tsize,(ij[0]+1)*tsize)
y = (ij[1]*tsize,(ij[1]+1)*tsize)
pview[ x[0]:x[1] , y[0]:y[1] ] = pviewOriginal[ x[0]:x[1] , y[0]:y[1] ]
return pview
def onclick(event):
if event.ydata == None or event.xdata == None:
# User clicked outside the box
return
ij = (math.floor(event.ydata/tsize), math.floor(event.xdata/tsize))
coord = int((ij[0] * dims[1]) + ij[1])
if event.button == 2:
# Left Click
if coord in clickedList:
unhighlight(coord)
clickedList.remove(coord)
else:
setborder(coord)
clickedList.append(coord)
if event.button == 3:
# Right Click to choose cutoff
del clickedList[:]
pview[:] = pviewOriginal[:]
for i in range(0,coord+1):
clickedList.append(i)
setborder(i)
clickedList.sort()
imageWindow.set_data(pview)
thumbPlot.canvas.draw()
thumbPlot.canvas.mpl_connect('button_press_event', onclick)
return clickedList
"""
Removes duplicates from json list
"""
def unique(memelist):
from operator import itemgetter
from itertools import groupby
memeListSorted = sorted(list)
return list(map(itemgetter(0), groupby(memeListSorted, key = lambda d : d['id'])))
"""
sMemes is a list of (distance, something.jpg) pairs.
allThumbs is a dict with all the thumbnails in it.
returns JSON file
"""
def toJSON(allThumbs, sMemes):
allMemesCuratedData = []
for meme in sMemes:
m = allThumbs[meme]
memeCurated = { 'id' : meme[0:-4],
'cr': m['created'],
'pt': m['ups'] - m['downs'],
'ti': m['title'] }
allMemesCuratedData.append(memeCurated)
return allMemesCuratedData
if __name__ == "__main__":
# Load all summaries
fp = open(r'D:\memeproject\thumbsnailSummaryTitlesAll','r')
allThumbs = pickle.load(fp)
fp.close()
print "Summaries Loaded"
templateList = os.listdir(r'D:\memeproject\templates')[3:]
templateList = ["College_Freshman.jpg"]
for currentMemeTemplateName in templateList:
"""
Read and visualize the sorted thumbnails generated by sortThumbs.py
"""
# Open the pre-cached meme distance data
fp = open('D:\\memeproject\\thumbssort\\' + currentMemeTemplateName,'r')
(s, cutoff) = pickle.load(fp)
fp.close()
thumbList = [i[1] for i in s]
clickedList = previewThumbs(thumbList, (30,80), plottitle = currentMemeTemplateName)
show()
if len(clickedList) != 0:
"""
Turn ClickedList into a JSON File, ready for use in the browser.
Ignore if no icons selected.
"""
from operator import itemgetter
import json
selectedMemes = itemgetter(*clickedList)(s)
fp = open('D:\\memeproject\\code\html\\memes\\' + currentMemeTemplateName[0:-4] + ".js", 'w')
json.dump(toJSON(allThumbs, [meme[1] for meme in selectedMemes]), fp)
fp.close()