def _augment_data(self): """ Populates any missing fields via api(media_id) """ api = InstagramAPI(access_token=self._access_token) for i in self._instagrams_list: if not 'img_url' in i or not 'link_url' in i or not 'main_color' in i: # Only do actual api query if we are missing something (ex: new entries) media = api.media(i['media_id']) i['title'] = media.caption.text if media.caption else "" i['img_url'] = media.images['standard_resolution'].url i['link_url'] = media.link i['main_color'] = palette(url=i['img_url'], n=1)[0] # First in palette print "Loaded new media: '%s' (title: '%s')" % (i['media_id'], i['title'])
def extract_colors(self, replace=True, save=True): """ Extract dominant colors for this image's ``downloaded_image`` (if available) and store in ``_colors`` attribute. If the kwarg ``replace`` is ``True`` (the default) an image that already has extracted colors will have those colors re-extracted, else the existing colors will be left unchanged. If the kwarg ``save`` is ``True`` (the default) this item will be saved when colors are extracted, else it is up to the caller to ``save``. """ # Do nothing if image is still available and has extracted colors and # ``replace`` isn't set if self._colors and self.downloaded_image and not replace: return if not self.downloaded_image: # We have no downloaded image, clear extracted colors self._colors = '' else: kwargs = { 'format': None, 'mode': 'kmeans' } # Are the assets stored or served from another host? if urlparse(self.downloaded_image.url).netloc: kwargs['url'] = self.downloaded_image.url else: kwargs['path'] = self.downloaded_image.path try: palette = colorweave.palette(**kwargs) except Exception as e: print( 'Error extracting colors from ArtworkImage [pk: {}]' '\n\nError: {}\n\n'.format(self.pk, e) ) palette = None if palette: closest_colors = {} for hex_color in palette: closest_colors[hex_color] = get_closest_color(hex_color) self._colors = json.dumps(closest_colors) else: # We have empty/invalid palette data, clear extracted colors self._colors = '' if save: self.save()
def __init__(self, frame_number, filename, frame): self.filename = filename self.frame_number = frame_number b, g, r, a = cv2.mean(frame) self.frame_color_RGB = (r, g, b) self.frame_color_HSV = colorsys.rgb_to_hls(r, g, b) LOGGER.debug("Saving image to: " + self.filename) cv2.imwrite(filename, frame) palette = colorweave.palette(path=filename, n=6, format="css3") self.palette = [] for hex, name in palette.items(): self.palette.append({hex: name}) LOGGER.debug("Palette is: " + str(palette))
def extract_colors(self, replace=True, save=True): """ Extract dominant colors for this image's ``downloaded_image`` (if available) and store in ``_colors`` attribute. If the kwarg ``replace`` is ``True`` (the default) an image that already has extracted colors will have those colors re-extracted, else the existing colors will be left unchanged. If the kwarg ``save`` is ``True`` (the default) this item will be saved when colors are extracted, else it is up to the caller to ``save``. """ # Do nothing if image is still available and has extracted colors and # ``replace`` isn't set if self._colors and self.downloaded_image and not replace: return if not self.downloaded_image: # We have no downloaded image, clear extracted colors self._colors = '' else: kwargs = {'format': None, 'mode': 'kmeans'} # Are the assets stored or served from another host? if urlparse(self.downloaded_image.url).netloc: kwargs['url'] = self.downloaded_image.url else: kwargs['path'] = self.downloaded_image.path try: palette = colorweave.palette(**kwargs) except Exception as e: print('Error extracting colors from ArtworkImage [pk: {}]' '\n\nError: {}\n\n'.format(self.pk, e)) palette = None if palette: closest_colors = {} for hex_color in palette: closest_colors[hex_color] = get_closest_color(hex_color) self._colors = json.dumps(closest_colors) else: # We have empty/invalid palette data, clear extracted colors self._colors = '' if save: self.save()
def set_color_dominant_colors(self, url, count=0): try: dominant_colors_dict = (palette(url=url, format='css3', n=10)) except urllib2.URLError: if count == 5: pass else: count += 1 return self.set_color_dominant_colors(url, count) print dominant_colors_dict skip_list = [] for light in self.lights: color_str, name = self._filter_colors(dominant_colors_dict, skip_colors=skip_list) print('Changing light to color to %s' % name) skip_list.append(name) # the hex string has # in front but that messes up everything self.set_light_color(light, color_str[1:])
def get_dominant_frame_color(method, file): """Extract the dominant color for a given file.""" if method == "colortheif": from colorthief import ColorThief color_thief = ColorThief(file) return color_thief.get_color(quality=1) elif method == "colorcube": sys.path.append('ColorCube/Python') from ColorCube import ColorCube from PIL import Image cc = ColorCube(bright_threshold=0.0) img = Image.open(file) colors = cc.get_colors(img) return colors[0] elif method == "colorweave": from colorweave import palette p = palette(path=file, n=1) return hex_to_rgb(p[0]) else: return average_image_color(file)
from colorweave import palette print palette(url='http://data.hdwallpapers.im/alia_bhatt_orange_dress.jpg', n=6, format='css21', output='json')
from colorweave import palette urlpath = 'http://st.hzcdn.com/simgs/43119d07045c50e0_3-2972/modern-kitchen.jpg' print(palette(url=urlpath, n=6, format="css3"))