def get_colours(path): colours = colorz(path, 16) # ####### HACK! !!!!!!!!!!!!!!!!!!!!!!!! # colours = ['#774d38', '#4a3a2b', '#f1ae39', '#d37837', '#182220', '#a15330', '#0e1211', '#212e2c', '#5e4534', '#f6f5b6', '#28271f', '#f4de5b', '#434338', '#34352d', '#030201', '#649386'] colours_sort = list(colours) colours_sort.sort(key=lambda x: darkness(x), reverse=True) colours_normalised = [] ret = [] indexes = array("i", [0] * 16) for idx, c in enumerate(colours_sort): indexes[colours.index(c)] = idx for i, c in enumerate(colours_sort): if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) c = normalize(c, minv=32, maxv=224) colours_normalised.append(c) # for c in colours_normalised: for idx, val in enumerate(indexes): ret.append(colours_normalised[val]) return ret
def colorspectrumimage(): body = request.get_json() img_url = body['img_url'] # check if the image url is local or network and open accordingly img_fd = open(img_url, 'rb') if os.path.isfile(img_url) else \ BytesIO(urlopen(img_url).read()) colors = colorz.colorz(img_fd) # TODO : create a jpeg image as svg supports only 16*16 format # note only a maximum of 16 colors can be shown in svg image dwg = svgwrite.Drawing('colour_spectrum.svg', profile='full') for i in range(0, len(colors) * 2, 2): j = int(i / 2) dwg.add( dwg.line((0, 0 + i), (16, 0 + i), stroke=svgwrite.rgb(colors[j][0][0], colors[j][0][1], colors[j][0][2], '%'))) dwg.add( dwg.line((0, 0 + i + 1), (16, 0 + i + 1), stroke=svgwrite.rgb(colors[j][1][0], colors[j][1][1], colors[j][1][2], '%'))) dwg.save() img_fd = open('colour_spectrum.svg') encoded = base64.b64encode(open("colour_spectrum.svg", "rb").read()) result = {} result['imageb64'] = encoded return jsonify(result)
def main( image_path, xresources_color_file, rofi_theme_file, kitty_config_file, orientation, no_scaling, debug, polybar_reload, **colorz_params, ): set_logging(debug) img = get_image(image_path, orientation, no_scaling) logger.info(f"Selected image {img}") with open(img, "rb") as img_file: colors = colorz.colorz( img_file, n=colorz_params["colorz_num_colors"], min_v=colorz_params["colorz_minv"], max_v=colorz_params["colorz_maxv"], bold_add=colorz_params["colorz_bold"], ) set_colors(colors, xresources_color_file) set_rofi_colors(colors, rofi_theme_file) set_kitty_colors(colors, kitty_config_file) load_new_xrdb_colors() set_new_background_image(img) if polybar_reload: reload_polybar() reload_i3()
def getColor(background): backgroundFile = background filePath = path + "/" + backgroundFile for c in colorz(filePath, n=3): cStrip = c.lstrip('#') rgb = tuple(int(cStrip[i:i + 2], 16) for i in (0, 2, 4)) return rgb
def write_album_art_colors(self, album_id, album_path, conn): if not DO_COLORZ: return print("DEBUG: Write colors {} {}".format(album_id, album_path)) try: colors = list(colorz(album_path)) except OSError: print("Error with album_path: {}".format(album_path)) return print("DEBUG: Colors: {}".format(colors)) if len(colors) > 0: self.delete_album_colors(album_id, conn) else: print("No colors found for {}".format(album_path)) return for c in colors: # Get sum of rgb c = c[1:] # get rid of leading "#" color_sum = sum([int(c[i:i+2],16) for i in range(0, len(c), 2)]) print("Color: {} ({})".format(c, color_sum)) cursor = conn.execute("""\ INSERT INTO album_colors (album_id, color, color_sum) VALUES (?, ?, ?) """, (album_id, c, color_sum) ) conn.commit()
def write_album_art_colors(self, album_id, album_path, conn): if not DO_COLORZ: return print("DEBUG: Write colors {} {}".format(album_id, album_path)) try: colors = list(colorz(album_path)) except OSError: print("Error with album_path: {}".format(album_path)) return print("DEBUG: Colors: {}".format(colors)) if len(colors) > 0: self.delete_album_colors(album_id, conn) else: print("No colors found for {}".format(album_path)) return for c in colors: # Get sum of rgb c = c[1:] # get rid of leading "#" color_sum = sum([int(c[i:i + 2], 16) for i in range(0, len(c), 2)]) print("Color: {} ({})".format(c, color_sum)) cursor = conn.execute( """\ INSERT INTO album_colors (album_id, color, color_sum) VALUES (?, ?, ?) """, (album_id, c, color_sum)) conn.commit()
def main(): path = os.getcwd() #get folder / images path n = 5 #number of color detected import csv #open csv result file with open('color_results.csv', mode='w') as result_file: result_writer = csv.writer(result_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) result_writer.writerow(["filename", "colorListHex", "colorName"]) for root, dirs, files in os.walk(path + "/"): # from your argv[1] for f in files: if ((os.path.isdir(f) is False) and (f != ".DS_Store") and (("png" in f) or ("jpg" in f) or ("PNG" in f) or ("JPG" in f))): #take only png images filename = join(root, f) colorName_str = "" print(filename) colors = colorz(filename, n=n) colorList = list(colors) #get list of colors detected print(colorList) for c in colorList: #for each hex color get the name c = c.lstrip('#') c_rgb = tuple(int(c[i:i + 2], 16) for i in (0, 2, 4)) #convertion hex to rgb c_name = getColorName(c_rgb[0], c_rgb[1], c_rgb[2]) colorName_str = colorName_str + "| " + c_name + "|" #print(colorName_str) result_writer.writerow([ filename, colorList, colorName_str ]) #write the result in the result csv file
def gen_colors(img): """Generate a colorscheme using Colorz.""" # pylint: disable=not-callable raw_colors = colorz.colorz(img, n=6, bold_add=50) ret_colors = [util.rgb_to_hex(color[0]) for color in raw_colors] ret_colors += [util.rgb_to_hex(color[1]) for color in raw_colors] return ret_colors
def get_colours(path): """ Given a file, extract the 8 most prominent colours, and generate the corresponding tints. Provides an interface between Caleb Everett (everett1992)'s code which extracts the colours from an image, and returns the hex values for the generated colours. """ _colours = colorz(path, 8) _colours.sort() colours = get_nearest_colours(_colours) ret = list(colours) for colour in colours: ret.append(tint(colour[0], colour[1], colour[2])) return [rtoh(colour) for colour in ret]
def colorspectrum(): body = request.get_json() img_url = body['img_url'] # check if the image url is local or network and open accordingly img_fd = open(img_url, 'rb') if os.path.isfile(img_url) else \ BytesIO(urlopen(img_url).read()) # Image._show(image) # uncomment to display the image colors = colorz.colorz(img_fd) # call colors library result = [] for c in colors: result.append(colorz.hexify(c[0])) result.append(colorz.hexify(c[1])) return jsonify(result)
# Convert colors from HSV to RGB for Red, Green and Blue r, g, b = colorsys.hsv_to_rgb(h, s, v) return '#{:02x}{:02x}{:02x}'.format(int(r * 256), int(g * 256), int(b * 256)) # If module is running as a program then if __name__ == '__main__': if len(sys.argv) == 1: n = 16 else: n = int(sys.argv[1]) i = 0 # Open colors.html and write your image as a thumbnail 200x200. with open('colors.html', 'w') as f: f.write("""<img src="file://{}" height=200/>""".format(wallpaper)) for c in colorz(wallpaper, n=n): if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) # Write the dominant colors in the image of height 50 pixels # and include their respective hex code. f.write(""" <div style="background-color: {0}; width: 100%; height: 50px">{1}: {0}</div> """.format(c, i) )
for j in range(width_sample*i, width_sample*i+width_sample): for k in range(0, 100): pix[j, k] = hex_color_to_rgb(c) im.save(f) if __name__ == '__main__': if len(sys.argv) == 2: n = 16 else: n = int(sys.argv[2]) i = 0 # sort by value, saturation, then hue colors = colorz(WALLPAPER, n=n) colors.sort(key=lambda x:darkness(x), reverse=True) for c in colors: if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) c = normalize(c, minv=32, maxv=224) xres += """*color{}: {}\n""".format(i, c) cols += """export COLOR{}="{}"\n""".format(i, c) i += 1
if v > maxv: v = maxv r, g, b = colorsys.hsv_to_rgb(h, s, v) return '#{:02x}{:02x}{:02x}'.format(int(r * 256), int(g * 256), int(b * 256)) if __name__ == '__main__': if len(sys.argv) == 2: n = 16 else: n = int(sys.argv[2]) i = 0 with open('colorz.html', 'w') as f: f.write("""<img src="file://{}" height=200/>""".format(WALLPAPER)) for c in colorz(WALLPAPER, n=n): # if i == 8: # i += 1 if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) f.write(""" <div style="background-color: {0}; width: 100%; height: 50px">{1}: {0}</div> """.format(c, i) ) xres += """urxvt*color{}: {}\n""".format(i, c)
def gen_colors(img): """Generate a colorscheme using colorz.""" raw_colors = colorz.colorz(img, n=6, bold_add=0) return [utils.rgb_to_hex([*color[0]]) for color in raw_colors]
def to_hsv(c): r, g, b = torgb(c) h, s, v = colorsys.rgb_to_hsv(r, g, b) return h, s, v if __name__ == '__main__': if len(sys.argv) == 2: n = 16 else: n = int(sys.argv[2]) i = 0 # sort by value, saturation, then hue colors = colorz(WALLPAPER, n=n) colors.sort(key=lambda x: darkness(x), reverse=True) for c in colors: if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) c = normalize(c, minv=32, maxv=224) xres += """*color{}: {}\n""".format(i, c) cols += """export COLOR{}="{}"\n""".format(i, c) i += 1
v = maxv r, g, b = colorsys.hsv_to_rgb(h, s, v) return '#{:02x}{:02x}{:02x}'.format(int(r * 256), int(g * 256), int(b * 256)) if __name__ == '__main__': if len(sys.argv) == 1: n = 16 else: n = int(sys.argv[1]) i = 0 with open('colorz.html', 'w') as f: f.write("""<img src="file://{}" height=200/>""".format(WALLPAPER)) for c in colorz(WALLPAPER, n=n): # if i == 8: # i += 1 if i == 0: c = normalize(c, minv=0, maxv=32) elif i == 8: c = normalize(c, minv=128, maxv=192) elif i < 8: c = normalize(c, minv=160, maxv=224) else: c = normalize(c, minv=200, maxv=256) f.write(""" <div style="background-color: {0}; width: 100%; height: 50px">{1}: {0}</div> """.format(c, i)) xres += """urxvt*color{}: {}\n""".format(i, c) cols += """export COLOR{}="{}"\n""".format(i, c)
def generate(options): image = os.path.abspath(options.image) json_before = JSON_BEFORE.format(options.parent, options.transparency, "" if options.no_background else image, to_json_bool(options.tiled), options.contrast, options.blend) json = "" colors = collections.deque(colorz(image, n=8)) colors.rotate(options.rotate) if options.reversed: colors = reversed(colors) for i, (normal, bright) in zip(range(8), colors): if options.inverted: normal = [256 - x for x in normal] bright = [256 - x for x in bright] normal = [x / 256.0 for x in normal] bright = [x / 256.0 for x in bright] if i == COLOR_WHITE: normal = clamp_hsv(normal, mins=0.0, maxs=0.12, minv=0.78, maxv=0.86) bright = clamp_hsv(bright, mins=0.0, maxs=0.19, minv=0.86, maxv=1.00) if i == COLOR_BLACK: normal = clamp_hsv(normal, mins=0.0, maxs=0.08, minv=0.08, maxv=0.12) bright = clamp_hsv(bright, mins=0.0, maxs=0.16, minv=0.08, maxv=0.23) normal = clamp_hsv(normal, mins=options.saturation_min, maxs=options.saturation_max, minv=options.brightness_min, maxv=options.brightness_max) bright = clamp_hsv(bright, mins=options.saturation_min, maxs=options.saturation_max, minv=options.brightness_min, maxv=options.brightness_max) if i == COLOR_WHITE: colors = clamp_hsv(normal, mins=0.12, maxs=0.16, minv=0.58, maxv=0.63) json += JSON_COLOR_NAMED.format("Foreground", *colors) if i == COLOR_BLACK: colors = clamp_hsv(normal, mins=0.0, maxs=0.1, minv=0.04, maxv=0.06) json += JSON_COLOR_NAMED.format("Background", *colors) colors = clamp_hsv(normal, mins=0.0, maxs=0.1, minv=0.06, maxv=0.10) json += JSON_COLOR_NAMED.format("Selected Text", *colors) if i == COLOR_BLUE: json += JSON_COLOR_NAMED.format("Selection", *normal) if i == COLOR_CYAN: json += JSON_COLOR_NAMED.format("Link", *normal) if i == COLOR_MAGENTA: colors = clamp_hsv(normal, mins=0.60, minv=0.80, maxv=0.90) json += JSON_COLOR_NAMED.format("Bold", *colors) json += JSON_COLOR_ANSI.format(i, *normal) json += JSON_COLOR_ANSI.format(i + 8, *bright) with open(options.out, 'w') as theme: theme.write(json_before) theme.write(json[:-2]) theme.write(JSON_AFTER)
import os from io import BytesIO from urllib.request import urlopen from PIL import Image import colorz image_filename = 'apple.jpeg' img_fd = open(image_filename, 'rb') if os.path.isfile(image_filename) else \ BytesIO(urlopen(image_filename).read()) image = Image.open(img_fd) if __name__ == '__main__': colors = colorz.colorz(img_fd) print(colors) result = [] for c in colors: result.append(colorz.hexify(c[0])) result.append(colorz.hexify(c[1])) print(result) html_fd = colorz.html_preview(colors, bg_img=None) html_url = 'file://' + html_fd.name print(html_url)
def get_colors(self, image, color_number=8) -> List[Color]: raw_colors = colorz(image, n=color_number, bold_add=0) return [Color(operations.color_conversions .rgb_to_hex(color[0])) for color in raw_colors]