NOTE: this is very rough and values are hard coded. publishing just for education. feel free to improve it.
first extract colors with colors.py
next encode with encoder.py
use decoder.py
to view output
from matplotlib import image | |
from colormap import rgb2hex | |
import numpy as np | |
import glob | |
import json | |
from collections import OrderedDict | |
f = open("./partcolors.json", "r") | |
meta = json.load(f) | |
colors = OrderedDict() | |
for c in meta["partcolors"]: | |
colors[c] = True | |
for file in glob.glob("./imgs/*.png"): | |
img = image.imread(file) | |
a = np.where(img[:,:,3] != 0) | |
bbox = np.min(a[0]), np.max(a[0]), np.min(a[1]), np.max(a[1]) | |
cropped = img[bbox[0]:bbox[1]+1, bbox[2]:bbox[3]+1] | |
for y in cropped: | |
for x in y: | |
if x[3] != 0: | |
c = rgb2hex(int(x[0]*255), int(x[1]*255), int(x[2]*255)) | |
colors[c[1:]] = True | |
meta["partcolors"] = list(colors.keys()) | |
f = open("./partcolors.json", "w") | |
json.dump(meta, f, indent=4) |
from eth_utils import to_bytes | |
import json | |
from matplotlib import pyplot as plt | |
import numpy as np | |
from colormap import hex2rgb | |
f = open("./encoded-layers.json", "r") | |
meta = json.load(f) | |
granularity = 64 | |
resolution = 320 | |
lookup = range(0, resolution, int(resolution/granularity)) | |
img = np.zeros((350, 350, 4)) | |
for parts in meta["parts"]: | |
part = parts[0] | |
subimg = to_bytes(hexstr=part["data"]) | |
top = subimg[1] | |
right = subimg[2] | |
bottom = subimg[3] | |
left = subimg[4] | |
print(top, right, bottom, left) | |
x = left | |
y = top | |
for i in range(5, len(subimg), 2): | |
length = subimg[i] | |
c = meta["partcolors"][subimg[i+1]] | |
if c != "": | |
rgb = hex2rgb("#" + c) | |
img[lookup[y] : lookup[y+1], lookup[x]: lookup[x] + lookup[length], :3] = np.array(rgb) / 255 | |
img[lookup[y] : lookup[y+1], lookup[x]: lookup[x] + lookup[length], 3] = 1.0 | |
print(x, length, right) | |
x += length | |
if x == right: | |
x = left | |
y += 1 | |
plt.imshow(img) | |
plt.show() |
from matplotlib import image | |
import numpy as np | |
from colormap import rgb2hex | |
import json | |
from eth_utils import to_hex | |
f = open("./partcolors.json", "r") | |
partcolors = json.load(f) | |
colors = {0:0} | |
for i, c in enumerate(partcolors["partcolors"]): | |
colors[c] = i | |
img = image.imread("./imgs/police64x64_0009_face.png") | |
a = np.where(img[:,:,3] != 0) | |
bbox = np.min(a[0]), np.max(a[0]), np.min(a[1]), np.max(a[1]) | |
cropped = img[bbox[0]:bbox[1]+1, bbox[2]:bbox[3]+1] | |
encoded = [0, bbox[0], bbox[3]+1, bbox[1]+1, bbox[2]] | |
for y in cropped: | |
n = 0 | |
prev, cur = -1, -1 | |
out = "" | |
for x in y: | |
prev = cur | |
if x[3] == 0: | |
cur = 0 | |
else: | |
# matplotlib reads in bgr | |
cur = rgb2hex(int(x[0]*255), int(x[1]*255), int(x[2]*255))[1:] | |
out += str(colors[cur]) | |
if cur != prev and prev != -1: | |
encoded.append(n) | |
encoded.append(colors[prev]) | |
n = 0 | |
n += 1 | |
encoded.append(n) | |
encoded.append(colors[cur]) | |
print(out) | |
out = "" | |
f = open("./output.json", "r") | |
print(len(encoded)) | |
meta = json.load(f) | |
meta["parts"].append([{ | |
"name": "black-hoodie", | |
"data": to_hex(bytes(encoded)) | |
}]) | |
f = open("./output.json", "w") | |
json.dump(meta, f, indent=4) |