forked from Batilan/ADS-B-funhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagecolor.py
132 lines (115 loc) · 4.44 KB
/
imagecolor.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
# Copyright (c) 2016 Johan Kanflo (github.com/kanflo)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import sys, os
import urllib2
import cStringIO
import json
import socket
import logging
import bing
log = logging.getLogger(__name__)
try:
from PIL import Image
except ImportError:
print "Pillow module not found, install using 'sudo pip install Pillow'"
exit(1)
def getImage(searchTerm):
searchTerm = "%s+logo" % (searchTerm)
log.debug("Searching for %s" % searchTerm)
try:
imageUrls = bing.imageSearch(searchTerm, {"minWidth":400, "minHeight":400})
except Exception, e:
log.error("Bing exception error: %s" % (e))
return None
for url in imageUrls:
log.debug(" Checking %s" % url)
fileName, fileExtension = os.path.splitext(url)
if fileExtension != ".svg" and fileExtension != ".gif":
log.debug("Fetching %s" % url)
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
try:
file = cStringIO.StringIO(opener.open(url, timeout = 10).read())
im = Image.open(file)
return (im, url)
except urllib2.HTTPError, e:
log.error("HTTP error: %s for %s" % (e, url))
log.error("searchTerm : '%s'" % (searchTerm))
pass
except UnicodeEncodeError, e:
log.error("UnicodeEncodeError error: %s" % (e))
log.error("searchTerm : '%s'" % (searchTerm))
pass
except urllib2.URLError, e:
log.error("URLError error: %s for %s" % (e, url))
log.error("searchTerm : '%s'" % (searchTerm))
pass
return (None, None)
def getProminentColor(searchTerm):
(im, url) = getImage(searchTerm)
if im == None:
# In case we cannot find a color, make sure we don't end up here in 10 milliseconds
return ((0,0,0), "error")
histogram = {}
limit = 10
px = im.getpixel((0, 0))
if isinstance(px, (int, long)):
im = im.convert() # Conver to multi-layer image
try:
for i in range(im.size[0]):
for j in range(im.size[1]):
px = im.getpixel((i,j))
if px != (0, 0, 0) and px != (0, 0, 0, 0) and px != (255, 255, 255):
if abs(px[0]-px[1]) > limit or abs(px[0]-px[2]) > limit or abs(px[1]-px[2]) > limit:
if not px in histogram:
histogram[px] = 1
else:
histogram[px] = histogram[px] + 1
except AttributeError, e:
pass # Grayscale image?
px_max = (0, 0, 0)
max_count = 0
for px in histogram:
if histogram[px] > max_count:
px_max = px
max_count = histogram[px]
return (((px_max[0], px_max[1], px_max[2])), url)
def loadColorData():
global colors
try:
colors = json.load(open("logocolors.json"))
except:
colors = {}
return colors
def getColor(key):
global colors
key = key.encode('utf-8', 'ignore')
if key in colors:
color = colors[key]["color"]
else:
(color, url) = getProminentColor(key)
if color and url:
colors[key] = {}
colors[key]["color"] = color
colors[key]["url"] = url
with open("logocolors.json", "w+") as f:
f.write(json.dumps(colors))
return color