mirror of
https://github.com/NicklasVraa/Color-manager.git
synced 2026-01-18 17:27:20 +01:00
Added support for svg color extraction
This commit is contained in:
@@ -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
|
||||
|
||||
10
readme.md
10
readme.md
@@ -13,7 +13,7 @@ Color Manager is a program for recoloring and manipulating existing icon packs,
|
||||
| **Monochrome**:<br> `(0.6,0.54,0.5)` |  |
|
||||
| **Multichrome**:<br> `nord.json`<br> `smooth=false` |  |
|
||||
| **Multichrome**:<br> `nord.json`<br> `smooth=true` |  |
|
||||
| **Color Extraction**:<br> Original `num=10` |  |
|
||||
| **Extraction**:<br> Original `num=10` |  |
|
||||
|
||||
**GUI Demo**:
|
||||

|
||||
@@ -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)
|
||||
```
|
||||
|
||||
12
test.ipynb
12
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)"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user