-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
99 lines (67 loc) · 2.09 KB
/
index.py
File metadata and controls
99 lines (67 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import cv2
import numpy as np
import os
import math
import json
INPUT_IMAGE = "glyph_E4.png"
OUTPUT_DIR = "discord_emojis"
TARGET = 128
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Load image with transparency
img = cv2.imread(INPUT_IMAGE, cv2.IMREAD_UNCHANGED)
# Create grayscale for detection
if img.shape[2] == 4:
gray = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
else:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect glyph pixels
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = []
for c in contours:
x, y, w, h = cv2.boundingRect(c)
# ignore tiny noise
if w * h < 30:
continue
boxes.append((x, y, w, h))
# Sort boxes top-to-bottom then left-to-right
boxes.sort(key=lambda b: (b[1] // 40, b[0]))
emoji_index = 0
mapping = {}
for (x, y, w, h) in boxes:
glyph = img[y:y+h, x:x+w]
# integer pixel-art scaling
scale = TARGET // h
if scale < 1:
scale = 1
new_w = w * scale
new_h = h * scale
resized = cv2.resize(
glyph,
(new_w, new_h),
interpolation=cv2.INTER_NEAREST
)
tiles = math.ceil(new_w / TARGET)
base_name = f"glyph_{emoji_index}"
mapping[base_name] = []
for t in range(tiles):
# Transparent canvas
canvas = np.zeros((TARGET, TARGET, 4), dtype=np.uint8)
start = t * TARGET
end = min(start + TARGET, new_w)
part = resized[:, start:end]
# Ensure RGBA
if part.shape[2] == 3:
part = cv2.cvtColor(part, cv2.COLOR_BGR2BGRA)
# center vertically
y_offset = (TARGET - new_h) // 2
canvas[y_offset:y_offset+new_h, 0:part.shape[1]] = part
filename = f"{base_name}_{t}.png"
path = os.path.join(OUTPUT_DIR, filename)
cv2.imwrite(path, canvas)
mapping[base_name].append(filename)
emoji_index += 1
# Save glyph mapping
with open(os.path.join(OUTPUT_DIR, "emoji_map.json"), "w") as f:
json.dump(mapping, f, indent=4)
print("Discord emoji pack exported successfully.")