def attach_colour_to_product(product_data, using_gallery_img): info_msg("Attaching colours to products") total_products = len(product_data) invalid_images = [] for index, data in enumerate(product_data): info_msg("[{0}/{1}]Extracting colours for product {2}".format( index, total_products, data['sku']) ) product_filename = get_product_filename(data, check_file_exists=True) if product_filename is None: continue if using_gallery_img: product_filename = transform_image_to_rgba(product_filename) if product_filename is None: invalid_images.append(data['sku']) continue colours_found = palette.extract_colors(product_filename) data['colours'] = [ (c.value, c.prominence) for c in colours_found.colors ] bg_colour = colours_found.bgcolor if bg_colour: data['colours'].insert(0, (bg_colour.value, bg_colour.prominence)) return invalid_images
def run(self): argv = sys.argv[1:] (options, args) = self.parser.parse_args(argv) if args: # image filenames were provided as arguments for filename in args: try: palette = extract_colors( filename, min_saturation=options.min_saturation, min_prominence=options.min_prominence, min_distance=options.min_distance, max_colors=options.max_colors, n_quantized=options.n_quantized) except Exception, e: # TODO: it's too broad exception. print >> sys.stderr, filename, e continue print_colors(filename, palette) if options.save_palette: save_palette_as_image(filename, palette) sys.exit(1)
def apply(self, mask, img, params): # throws exception if params are wrong self.check_params_keys(params.keys()) # base params palette_size = params["palette_size"] # optional params add_white_lines = params["add_white_lines"] if "add_white_lines" in params else self.optional_params_values["add_white_lines"] palette_colors = [] white = "#FFFFFF" if add_white_lines: params["palette_size"] += 1 palette_colors.append(white) for color in palette.extract_colors(img, max_colors=palette_size).colors: palette_colors.append(rgb_to_hex(color.value)) palette_colors.append(white) params["palette_size"] += 1 prev_img = img line_effect = LineEffect() lines_params = self.build_params_for_rainbow(params, palette_colors) for exact_line_params in lines_params: prev_img = line_effect.apply(mask, prev_img, exact_line_params) return prev_img
def main(meetup, tc=(255, 255, 255), bg=None, *tags): target_url = meetup soup = BeautifulSoup(requests.get(target_url).content, "html.parser") description = soup.find("div", id="groupDesc") description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4) description = "\n".join(map(lambda x: x.rstrip(), description.split("\n"))) target_meetup_name = target_url.split("/")[-2] target = target_url.split("/")[-2].lower().replace("-", "_") if re.match("^\d", target): target = "_" + target logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None if bg == None: if logo_url: palette = extract_colors(Image.open(BytesIO(requests.get(logo_url).content))) colors = palette.colors background_color = colors[0].value text_color = tc else: h = (random.randint(1, 100) * 0.618033988749895) % 1 background_color = hsv_to_rgb(h, .5, .95) text_color = "#000000" h, s, v = rgb_to_hsv(background_color) else: background_color = bg text_color = tc # background_color = map(lambda x: (x + 255)/2, background_color) red = RedBaron(open("agendas/be.py", "r").read()) for i in red("def", recursive=False): if target < i.name: break i.insert_before(template % { "background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color, "text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color, "url": target_url, "tags": ", ".join(map(repr, tags)), "function_name": target, "description": description, "meetup_name": target_meetup_name, }) red.dumps() open("agendas/be.py", "w").write(red.dumps()) os.system("python manage.py fetch_events %s" % target)
def test_extraction(self): expected = [(0, 101, 185), (187, 214, 236), (255, 0, 0), (45, 68, 86), (119, 173, 218)] p = palette.extract_colors(self.filename) found = [c.value for c in p.colors] self.assertEquals(found, expected)
def color_stream_st(istream=sys.stdin, save_palette=False, **kwargs): """ Read filenames from the input stream and detect their palette. """ for line in istream: filename = line.strip() try: palette = extract_colors(filename, **kwargs) except Exception, e: print >> sys.stderr, filename, e continue print_colors(filename, palette) if save_palette: save_palette_as_image(filename, palette)
def color_process(queue, lock): "Receive filenames and get the colors from their images." while True: block = queue.get() if block == config.SENTINEL: break for filename in block: try: palette = extract_colors(filename) except: # TODO: it's too broad exception. continue lock.acquire() try: print_colors(filename, palette) finally: lock.release()
def run(self): argv = sys.argv[1:] (options, args) = self.parser.parse_args(argv) if args: # image filenames were provided as arguments for filename in args: try: palette = extract_colors( filename, min_saturation=options.min_saturation, min_prominence=options.min_prominence, min_distance=options.min_distance, max_colors=options.max_colors, n_quantized=options.n_quantized, ) except Exception as e: # TODO: it's too broad exception. print >>sys.stderr, filename, e continue print_colors(filename, palette) if options.save_palette: save_palette_as_image(filename, palette) sys.exit(1) if options.n_processes > 1: # XXX add all the knobs we can tune color_stream_mt(n=options.n_processes) else: color_stream_st( min_saturation=options.min_saturation, min_prominence=options.min_prominence, min_distance=options.min_distance, max_colors=options.max_colors, n_quantized=options.n_quantized, save_palette=options.save_palette, )
''' Created on Sep 21, 2014 @author: ranjeetbhatia http://99designs.com/tech-blog/blog/2012/05/11/color-analysis/ ''' from colorific import palette from StringIO import StringIO from urllib2 import urlopen url = 'http://a9.zassets.com/images/z/2/2/4/3/5/4/2243543-p-MULTIVIEW.jpg' image_file = StringIO(urlopen(url).read()) p = palette.extract_colors(image_file) print p
def main(meetup, tc=(255, 255, 255), bg=None, *tags): target_url = meetup soup = BeautifulSoup(requests.get(target_url).content, "html.parser") description = soup.find("div", id="groupDesc") description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4) description = "\n".join(map(lambda x: x.rstrip(), description.split("\n"))) target_meetup_name = target_url.split("/")[-2] target = target_url.split("/")[-2].lower().replace("-", "_") if re.match("^\d", target): target = "_" + target logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None if bg == None: if logo_url: palette = extract_colors( Image.open(BytesIO(requests.get(logo_url).content))) colors = palette.colors background_color = colors[0].value text_color = tc else: h = (random.randint(1, 100) * 0.618033988749895) % 1 background_color = hsv_to_rgb(h, .5, .95) text_color = "#000000" h, s, v = rgb_to_hsv(background_color) else: background_color = bg text_color = tc # background_color = map(lambda x: (x + 255)/2, background_color) red = RedBaron(open("agendas/be.py", "r").read()) for i in red("def", recursive=False): if target < i.name: break i.insert_before( template % { "background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color, "text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color, "url": target_url, "tags": ", ".join(map(repr, tags)), "function_name": target, "description": description, "meetup_name": target_meetup_name, }) red.dumps() open("agendas/be.py", "w").write(red.dumps()) os.system("python manage.py fetch_events %s" % target)
def _extract_colors(self): return extract_colors(self.get_source_path)