From c06f2adb5a4e0c53840d58a8c8db4516cfc48687 Mon Sep 17 00:00:00 2001 From: NicklasVraa Date: Fri, 11 Aug 2023 18:07:00 +0200 Subject: [PATCH] Added support for svg color extraction --- color_manager/utils.py | 22 ++++++++++++++++------ readme.md | 10 +++++----- test.ipynb | 12 ++++++------ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/color_manager/utils.py b/color_manager/utils.py index 4d5b5a30..0da53d2e 100644 --- a/color_manager/utils.py +++ b/color_manager/utils.py @@ -351,16 +351,26 @@ def recolor(src_path:str, dest_path:str, name:str, replacement) -> None: img.save(path) def extract_colors(img_path, num_colors=8, save_path=None, pixels=50, cols=10): - img = Image.open(img_path) - colors = img.convert('P', palette=Image.ADAPTIVE, colors=num_colors) - colors = colors.getpalette()[0:num_colors*3] + _, ext = os.path.splitext(img_path) - colors = ['#{:02X}{:02X}{:02X}'.format(colors[i], colors[i+1], colors[i+2]) for i in range(0, len(colors), 3)] + if ext == ".svg": + with open(img_path, 'r') as file: + svg = file.read() + + colors = list(get_svg_colors(svg)) + num_colors = len(colors) + + else: + img = Image.open(img_path) + + colors = img.convert('P', palette=Image.ADAPTIVE, colors=num_colors) + colors = colors.getpalette()[0:num_colors*3] + + colors = ['#{:02X}{:02X}{:02X}'.format(colors[i], colors[i+1], colors[i+2]) for i in range(0, len(colors), 3)] if save_path != None: - n = len(colors) - if n < cols: cols = n + if num_colors < cols: cols = num_colors rows = -(-len(colors) // cols) width = cols * pixels; height = rows * pixels diff --git a/readme.md b/readme.md index 0726be24..fccc1419 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ Color Manager is a program for recoloring and manipulating existing icon packs, | **Monochrome**:
`(0.6,0.54,0.5)` | ![2](resources/mono.png) | | **Multichrome**:
`nord.json`
`smooth=false` | ![3](resources/multi_accurate.png) | | **Multichrome**:
`nord.json`
`smooth=true` | ![3](resources/multi_smooth.png) | -| **Color Extraction**:
Original `num=10` | ![4](resources/palette.png) | +| **Extraction**:
Original `num=10` | ![4](resources/palette.png) | **GUI Demo**: ![demo](resources/demo.gif) @@ -29,7 +29,7 @@ Color Manager is a program for recoloring and manipulating existing icon packs, - [x] Graphical user interface based on the GTK framework. - [x] Python pip package. - [x] Full support for pngs and jpgs. -- [ ] Generate palette from source image or svg. +- [x] Generate palette from source image or svg. - [ ] Adding basic geometry to the backgrounds of icons. - [ ] Optional automatic palette extending. - [ ] Basic framework for manipulating GTK, Cinnamon and Metacity themes. @@ -63,16 +63,16 @@ Recoloring: src = "test_pack" name = "my_pack" dest = "~/Downloads" -hsl = (0.5, 0.5, 0.5) # = rc.norm_hsl(180, 50, 50) +hsl = (0.5, 0.5, 0.5) # = rc.norm_hsl(180, 50, 50) palette = "palettes/dracula.json" utils.recolor(src, dest, name, hsl) # Either hsl or palette. ``` Extracting: ```python -image = "test_pack/imgs/lake_cabin.png" +image = "test_pack/imgs/lake_cabin.png" # Also try an svg. num_colors = 10 -output = "resources/palette.png" # Optional - if given, saves colors as image. +output = "resources/palette.png" # Optional - saves colors as image. utils.extract_colors(image, num_colors, output) ``` diff --git a/test.ipynb b/test.ipynb index 7e9db80f..ef545734 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 63, "metadata": {}, "outputs": [], "source": [ @@ -40,12 +40,12 @@ "hsl = (0.6, 0.54, 0.5) # = rc.normalize_hsl(180, 50, 50)\n", "palette = \"palettes/nord.json\"\n", "\n", - "utils.recolor(src, dest, name, palette) # hsl or palette" + "utils.recolor(src, dest, name, palette) # Either hsl or palette" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 75, "metadata": {}, "outputs": [ { @@ -63,7 +63,7 @@ " '#012941']" ] }, - "execution_count": 57, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -71,9 +71,9 @@ "source": [ "from color_manager import utils\n", "\n", - "image = \"test_pack/imgs/lake_cabin.png\"\n", + "image = \"test_pack/imgs/lake_cabin.png\" # Also try an svg.\n", "num_colors = 10\n", - "output = \"./palette.png\" # if given, saves colors as image.\n", + "output = \"resources/palette.png\" # Optional - saves colors as image.\n", "\n", "utils.extract_colors(image, num_colors, output)" ]