def get_palette(self, img_url): request = requests.get(img_url, stream=True) status = request.status_code if status == 200: img = Image.open(request.raw) palette = colorific.extract_colors(img) array_colors = [] for color in palette.colors: hex_value = colorific.rgb_to_hex(color.value) dictionary_colors = { 'r': color.value[0], 'g': color.value[1], 'b': color.value[2], 'hex': hex_value } array_colors.append(dictionary_colors) if palette.bgcolor is not None: hex_value = colorific.rgb_to_hex(palette.bgcolor.value) dictionary_colors = { 'r': palette.bgcolor.value[0], 'g': palette.bgcolor.value[1], 'b': palette.bgcolor.value[2], 'hex': hex_value } array_colors.append(dictionary_colors) else: array_colors = [] return array_colors
def upload_image(): data = request.files.get('data') if data is not None: s = request.environ.get('beaker.session') colors = [colorific.rgb_to_hex(color.value) for color in colorific.extract_colors(data.file).colors] s['colors'] = colors s.save() return redirect('/result')
def parse_colors(self): palette = colorific.extract_colors(self.image) palette_dict = {} for color in palette.colors: color_hex = colorific.rgb_to_hex(color.value) palette_dict[color_hex] = color.prominence colors = sorted(palette_dict.items(), key=operator.itemgetter(1), reverse=True) return ','.join([c[0] for c in colors])
def colors_as_image(colors): "Save palette as a PNG with labeled, colored blocks" colors = [hex_to_rgb("#%s" % c) for c in colors] size = (80 * len(colors), 80) im = Image.new('RGB', size) draw = ImageDraw.Draw(im) for i, c in enumerate(colors): v = colorsys.rgb_to_hsv(*norm_color(c))[2] (x1, y1) = (i * 80, 0) (x2, y2) = ((i + 1) * 80 - 1, 79) draw.rectangle([(x1, y1), (x2, y2)], fill=c) if v < 0.6: # white with shadow draw.text((x1 + 4, y1 + 4), rgb_to_hex(c), (90, 90, 90)) draw.text((x1 + 3, y1 + 3), rgb_to_hex(c)) else: # dark with bright "shadow" draw.text((x1 + 4, y1 + 4), rgb_to_hex(c), (230, 230, 230)) draw.text((x1 + 3, y1 + 3), rgb_to_hex(c), (0, 0, 0)) return im
def get_palette(self): image = Im.open(StringIO(self.image.body)) palette = colorific.extract_colors(image) palette_dict = {} for color in palette.colors: color_hex = colorific.rgb_to_hex(color.value) palette_dict[color_hex] = color.prominence colors = sorted( palette_dict.iteritems(), key=operator.itemgetter(1), reverse=True) return colors
def get_primary_color(image_url): """Get the primary color of the currently playing album""" try: urllib.request.urlretrieve(image_url, "album_art.jpg") except: return int("0xffffff", 0) palette = colorific.extract_colors("album_art.jpg") primary_color_rgb = palette.colors[0].value primary_color = "0x" + colorific.rgb_to_hex(primary_color_rgb)[1:] return int(primary_color, 0)
def process_file(input_file, filename): palette = colorific.extract_colors(filename, min_saturation=0.05, min_prominence=0.01, min_distance=10.0, max_colors=5, n_quantized=100) data = { 'filename': filename, 'colours': [rgb_to_hex(c.value) for c in palette.colors], 'background': palette.bgcolor and rgb_to_hex(palette.bgcolor.value) or '' } out_file = os.path.join(settings.OUTPUT_FILE_QUEUE, input_file + '_data.json') with open(out_file, 'wt') as out_file: out_file.write(json.dumps(data)) shutil.move(filename, os.path.join(settings.PROCESSED_FILE_QUEUE, input_file)) return data
def test_rgb_to_hex(self): for rgb, hexval in self.pairs: self.assertEqual(colorific.rgb_to_hex(rgb), hexval)
import colorific import glob html = open("index.html", "w") for filename in glob.glob('../src/wallpapers/*'): html.write("<div>") html.write("<img width=\"150px\" src=\"" + filename + "\">") print(filename) palette = colorific.extract_colors(filename) print(palette) for color in palette.colors: print(color) hex_value = colorific.rgb_to_hex(color.value) html.write(""" <div style="background: {color}; width: 500px; height: 50px; color: white;"> {prominence} {color} </div> """.format(color=hex_value, prominence=color.prominence)) html.write("</div>") if palette.bgcolor != None: hex_value = colorific.rgb_to_hex(palette.bgcolor.value) html.write(""" <div style="background: {color}; width: 500px; height: 50px; color: white;"> <span>BG:</span>{prominence} {color} </div> """.format(color=hex_value, prominence=palette.bgcolor.prominence)) html.write("</div>")
def palette(image_path, min_saturation=0.05, min_prominence=0.01, min_distance=10.0, max_colors=5, n_quantized=100): colorp = colorific.extract_colors(image_path, min_saturation=min_saturation, min_prominence=min_prominence, min_distance=min_distance, max_colors=max_colors, n_quantized=n_quantized) return [colorific.rgb_to_hex(c.value) for c in colorp.colors]