mirror of
https://github.com/NicklasVraa/Color-manager.git
synced 2026-03-18 21:50:34 +01:00
added support for pngs
This commit is contained in:
@@ -6,6 +6,7 @@ from tqdm import tqdm
|
||||
from colormath.color_objects import sRGBColor, LabColor
|
||||
from colormath.color_conversions import convert_color
|
||||
from colormath.color_diff import delta_e_cie2000
|
||||
from PIL import Image
|
||||
import os, re, shutil, json
|
||||
|
||||
# Color conversion -------------------------------------------------------------
|
||||
@@ -44,6 +45,11 @@ def rgb_to_hsl(rgb:Tuple[int,int,int]) -> Tuple[float,float,float]:
|
||||
|
||||
return h, s, l
|
||||
|
||||
def rgb_to_gray(rgb:Tuple[int,int,int]) -> Tuple[int,int,int]:
|
||||
r, g, b = rgb
|
||||
weighed_avg = int(0.21*r + 0.72*g + 0.07*b)
|
||||
return weighed_avg, weighed_avg, weighed_avg
|
||||
|
||||
def hsl_to_rgb(hsl:Tuple[float,float,float]) -> Tuple[int,int,int]:
|
||||
h, s, l = hsl
|
||||
|
||||
@@ -211,7 +217,7 @@ def copy_pack(src_path:str, dest_path:str, name:str) -> str:
|
||||
|
||||
# Recolor ----------------------------------------------------------------------
|
||||
|
||||
def monochrome_svg(svg:str, colors:Set[str], hsl) -> str:
|
||||
def monochrome_svg(svg:str, colors:Set[str], hsl:Tuple[float,float,float]) -> str:
|
||||
"""Replace every instance of color within the given list with their
|
||||
monochrome equivalent in the given string representing an svg-file,
|
||||
determined by the given hue, saturation and lightness offset."""
|
||||
@@ -246,15 +252,33 @@ def multichrome_svg(svg:str, colors:Set[str], new_colors:List[str]) -> str:
|
||||
|
||||
return multichrome_svg
|
||||
|
||||
def monochrome_png(img:Image, hsl:Tuple[float,float,float]):
|
||||
h, s, l_offset = hsl
|
||||
|
||||
if s != 0:
|
||||
width, height = img.size
|
||||
l_offset = (l_offset - 0.5) * 2 # Remapping.
|
||||
|
||||
for x in range(width):
|
||||
for y in range(height):
|
||||
r, g, b, a = img.getpixel((x, y))
|
||||
l = (0.21*r + 0.72*g + 0.07*b)/255
|
||||
l = max(-1, min(l+l_offset, 1))
|
||||
new_pixel = hsl_to_rgb((h, s, l)) + (a,)
|
||||
img.putpixel((x,y), new_pixel)
|
||||
|
||||
return img
|
||||
|
||||
def recolor(src_path:str, dest_path:str, name:str, replacement) -> None:
|
||||
"""Recursively copies and converts a source folder into a destination, given a either a color or a palette."""
|
||||
|
||||
is_mono, new_colors = get_input_colors(replacement)
|
||||
dest_path = copy_pack(src_path, dest_path, name)
|
||||
vector_paths = get_paths(dest_path, [".svg"])
|
||||
raster_paths = get_paths(dest_path, [".png", ".jpeg"])
|
||||
svg_paths = get_paths(dest_path, [".svg"])
|
||||
png_paths = get_paths(dest_path, [".png"])
|
||||
#jpg_paths = get_paths(dest_path, [".jpg", ".jpeg"])
|
||||
|
||||
for path in tqdm(vector_paths, desc="Processing SVGs", unit=" svg"):
|
||||
for path in tqdm(svg_paths, desc="Processing svgs", unit=" file"):
|
||||
with open(path, 'r') as file:
|
||||
svg = file.read()
|
||||
|
||||
@@ -267,3 +291,13 @@ def recolor(src_path:str, dest_path:str, name:str, replacement) -> None:
|
||||
|
||||
with open(path, 'w') as file:
|
||||
file.write(svg)
|
||||
|
||||
for path in tqdm(png_paths, desc="Processing pngs", unit=" file"):
|
||||
png = Image.open(path)
|
||||
|
||||
if is_mono:
|
||||
png = monochrome_png(png, new_colors)
|
||||
else:
|
||||
pass
|
||||
|
||||
png.save(path)
|
||||
|
||||
84
test.ipynb
84
test.ipynb
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -12,7 +12,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -21,49 +21,41 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"src = \"packs/test_pack\"\n",
|
||||
"name = \"my_mono_pack\"\n",
|
||||
"dest = \"~/Downloads\"\n",
|
||||
"hsl = (0.5, 0.5, 0.5) # = rc.normalize_hsl(180, 50, 50)\n",
|
||||
"\n",
|
||||
"rc.monochrome_pack(src, dest, name, hsl)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"src = \"packs/papirus-mini_multi\"\n",
|
||||
"name = \"my_multi_pack\"\n",
|
||||
"dest = \"~/Downloads\"\n",
|
||||
"palette = \"palettes/dracula.json\"\n",
|
||||
"\n",
|
||||
"rc.multichrome_pack(src, dest, name, palette)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 74,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Processing SVGs: 0%| | 0/12 [00:00<?, ? svg/s]"
|
||||
"Processing svgs: 100%|██████████| 6/6 [00:00<00:00, 3082.91 file/s]\n",
|
||||
"Processing pngs: 0%| | 0/6 [00:00<?, ? file/s]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Processing SVGs: 100%|██████████| 12/12 [00:00<00:00, 62.58 svg/s]\n"
|
||||
"Processing pngs: 100%|██████████| 6/6 [00:00<00:00, 111.23 file/s]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"converting\n",
|
||||
"converting\n",
|
||||
"converting\n",
|
||||
"converting\n",
|
||||
"converting\n",
|
||||
"converting\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -71,10 +63,32 @@
|
||||
"src = \"packs/test_pack\"\n",
|
||||
"name = \"my_pack\"\n",
|
||||
"dest = \"~/Downloads\"\n",
|
||||
"hsl = (0.5, 0.5, 0.5)\n",
|
||||
"hsl = (0.5, 0.5, 0.5) # = rc.normalize_hsl(180, 50, 50)\n",
|
||||
"palette = \"palettes/dracula.json\"\n",
|
||||
"\n",
|
||||
"rc.recolor(src, dest, name, hsl)"
|
||||
"rc.recolor(src, dest, name, hsl) # or palette."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<PIL.Image.Image image mode=LA size=300x300 at 0x7F0C604A3880>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from PIL import Image\n",
|
||||
"import numpy as np\n",
|
||||
"path = \"packs/test_pack/pngs/firefox.png\"\n",
|
||||
"img = Image.open(path)\n",
|
||||
"img.getpixel((1,1))\n",
|
||||
"print(img)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user