-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht.py
More file actions
78 lines (66 loc) · 2.34 KB
/
t.py
File metadata and controls
78 lines (66 loc) · 2.34 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
#
# 在网易强大的刷屏拦截之下,效果很不理想
#
import math
from PIL import Image
import tqdm
image_path = r"C:\Users\sgpl\Downloads\109004781_p0.jpg"
colors = {
"§0": (0, 0, 0), # black
"§1": (0, 0, 170), # dark_blue
"§2": (0, 170, 0), # dark_green
"§3": (0, 170, 170), # dark_aqua
"§4": (170, 0, 0), # dark_red
"§5": (170, 0, 170), # dark_purple
"§6": (255, 170, 0), # gold
"§7": (170, 170, 170), # gray
"§8": (85, 85, 85), # dark_gray
"§9": (85, 85, 255), # blue
"§a": (85, 255, 85), # green
"§b": (85, 255, 255), # aqua
"§c": (255, 85, 85), # red
"§d": (255, 85, 255), # light_purple
"§e": (255, 255, 85), # yellow
"§f": (255, 255, 255), # white
"§g": (221, 214, 5), # minecoin_gold (BE)
"§h": (227, 212, 209), # material_quartz (BE)
"§i": (206, 202, 202), # material_iron (BE)
"§j": (68, 58, 59), # material_netherite (BE)
"§m": (151, 22, 7), # material_redstone (BE)
"§n": (180, 104, 77), # material_copper (BE)
"§p": (222, 177, 45), # material_gold (BE)
"§q": (17, 160, 54), # material_emerald (BE)
"§s": (44, 186, 168), # material_diamond (BE)
"§t": (33, 73, 123), # material_lapis (BE)
"§u": (154, 92, 198), # material_amethyst (BE)
"§v": (235, 114, 20), # material_resin (BE)
}
def rgb_distance(rgb1, rgb2):
return math.sqrt(
(rgb1[0] - rgb2[0]) ** 2 + (rgb1[1] - rgb2[1]) ** 2 + (rgb1[2] - rgb2[2]) ** 2)
def find_closest_color(target_rgb):
closest_color = None
min_distance = float("inf") # 初始化最小距离为无穷大
for block, color in colors.items():
distance = rgb_distance(target_rgb, color)
if distance < min_distance:
min_distance = distance
closest_color = block
return closest_color, min_distance
def png2json(image_path:str,wmax:int=61):
r=""
img = Image.open(image_path).convert("RGB")
w, h = img.size
img = img.resize((wmax, int(wmax / w * h)), Image.Resampling.LANCZOS)
w, h = img.size
# print("[debug] 图像尺寸:", img.size)
for y in range(h):
for x in range(w):
rgb = img.getpixel((x, y))
color_name = r"\u00a7"+find_closest_color(rgb)[0].replace("§","")
r += f"{color_name}▓"
r += r"\n"
#print('/tellraw @a {"rawtext":[{"text":"%s"}]}' % r)
# r=""
return r
print(png2json(image_path,20))