-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmccag.py
254 lines (206 loc) · 10.1 KB
/
mccag.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
from flask import Flask, request, send_file, render_template, url_for, jsonify
import requests
import base64
import json
from PIL import Image, ImageFilter, ImageOps
from io import BytesIO
import os
import time
app = Flask(__name__)
CACHE_DIR = 'static/cache'
CACHE_DURATION = 1 * 60 # 1 分钟的秒数
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
def get_uuid_by_name(username):
"""根据玩家名称获取 UUID。"""
url = f"https://api.mojang.com/users/profiles/minecraft/{username}"
response = requests.get(url)
if response.status_code == 404:
return None
data = response.json()
return data['id']
def get_player_textures(uuid):
"""根据 UUID 获取玩家的皮肤纹理信息。"""
url = f"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}?unsigned=false"
response = requests.get(url)
if response.status_code != 200:
return None
data = response.json()
if 'properties' not in data:
return None
properties = data['properties']
for prop in properties:
if prop['name'] == 'textures':
textures_base64 = prop['value']
textures_json = base64.b64decode(textures_base64).decode('utf-8')
textures = json.loads(textures_json)
return textures
return None
def download_image(url):
"""下载图像并返回 PIL 图像对象。"""
response = requests.get(url)
if response.status_code == 200:
return Image.open(BytesIO(response.content))
else:
return None
def create_image_with_paste(source_image, target_image_path, canvas_size, operations):
"""创建图像并保存到目标路径。"""
canvas = Image.new('RGBA', canvas_size, (255, 255, 255, 0))
skin_size = source_image.size
# 根据皮肤尺寸选择缩放
if skin_size == (64, 32):
resized_source_image = source_image.resize((128, 64), Image.NEAREST)
else:
resized_source_image = source_image.resize((128, 128), Image.NEAREST)
for operation in operations:
crop_box, scale_factor, paste_position, *mirror = operation
cropped_image = resized_source_image.crop(crop_box)
if mirror:
cropped_image = ImageOps.mirror(cropped_image)
new_size = (int(cropped_image.size[0] * scale_factor), int(cropped_image.size[1] * scale_factor))
bordered_size = (new_size[0] + 30, new_size[1] + 30)
bordered_image = Image.new('RGBA', bordered_size, (0, 0, 0, 0))
bordered_image.paste(cropped_image.resize(new_size, Image.NEAREST), (15, 15))
# 创建阴影
mask = bordered_image.split()[3] # 仅获取透明度通道
solid_image = Image.new('RGBA', bordered_image.size, (75, 85, 142, 255)) # 固定颜色,透明背景
shadow_image = Image.new('RGBA', bordered_image.size, (0, 0, 0, 0)) # 完全透明的背景
shadow_image.paste(solid_image, (0, 0), mask) # 使用mask创建阴影
blurred_shadow = shadow_image.filter(ImageFilter.GaussianBlur(7))
alpha = blurred_shadow.split()[3].point(lambda p: p * 0.6)
blurred_shadow.putalpha(alpha)
shadow_position = (paste_position[0] - 15, paste_position[1] - 10)
# 创建新的全透明图像并合成阴影
base_image = Image.new('RGBA', canvas.size, (0, 0, 0, 0))
base_image.paste(blurred_shadow, shadow_position, blurred_shadow)
canvas = Image.alpha_composite(canvas, base_image)
# 粘贴实际图像
adjusted_paste_position = (paste_position[0] - 15, paste_position[1] - 15)
canvas.paste(bordered_image, adjusted_paste_position, bordered_image)
canvas.save(target_image_path)
def get_cache_filename(username):
"""根据用户名生成缓存文件名。"""
uuid = get_uuid_by_name(username)
return os.path.join(CACHE_DIR, f'{uuid}.png') if uuid else None
def is_cache_valid(filename):
"""检查缓存是否有效。"""
ignore_filename = '923ed5ce249a4cd3ac7d23e6797b939c.png'
if os.path.basename(filename) == ignore_filename:
return True
if os.path.exists(filename):
return time.time() - os.path.getmtime(filename) < CACHE_DURATION
return False
def clean_old_cache_files():
"""删除超过1分钟的缓存文件,但保留特定的忽略文件。"""
ignore_filename = '923ed5ce249a4cd3ac7d23e6797b939c.png'
now = time.time()
for filename in os.listdir(CACHE_DIR):
if filename == ignore_filename:
continue # 忽略指定的文件
filepath = os.path.join(CACHE_DIR, filename)
if os.path.isfile(filepath) and now - os.path.getmtime(filepath) > CACHE_DURATION:
os.remove(filepath)
@app.before_request
def before_request():
clean_old_cache_files()
@app.route('/', methods=['GET', 'POST'])
def index():
image_url = None
if request.method == 'POST':
username = request.form.get('username')
if not username:
return "玩家名称不能为空", 400
cache_filename = get_cache_filename(username)
if cache_filename and is_cache_valid(cache_filename):
image_url = url_for('static', filename=f'cache/{os.path.basename(cache_filename)}')
else:
uuid = get_uuid_by_name(username)
if uuid is None:
return "没有找到该玩家", 404
textures = get_player_textures(uuid)
if textures is None:
return "无法获取玩家纹理信息", 500
if 'SKIN' in textures['textures']:
skin_url = textures['textures']['SKIN']['url']
skin_image = download_image(skin_url)
if skin_image:
target_image_path = cache_filename
canvas_size = (1000, 1000) # 新画布的大小
skin_size = skin_image.size
if skin_size == (64, 32):
operations = [
((8, 40, 16, 64), 8.375, (434,751)), # 右腿
((8, 40, 16, 64), 8.375, (505,751), True), # 左腿,使用右腿镜像
((86, 40, 92, 64), 8.167, (388,561)), # 右臂
((86, 40, 92, 64), 8.167, (566,561), True), # 左臂,使用右臂镜像
((40, 40, 56, 64), 8.0625, (437,561)), # 躯干
((16, 16, 32, 32), 26.875, (287,131)), # 头部
((80, 16, 96, 32), 30.8125, (254,107)), # 头部外层
]
else:
operations = [
((8, 40, 16, 64), 8.375, (434,751)),
((8, 72, 16, 96), 9.375, (428,737)),
((40, 104, 48, 128), 8.375, (505,751)),
((8, 104, 16, 128), 9.375, (503,737)),
((86, 40, 92, 64), 8.167, (388,561)),
((88, 72, 94, 96), 9.5, (382,538)),
((74, 104, 80, 128), 8.167, (566,561)),
((104, 104, 110, 128), 9.5, (564,538)),
((40, 40, 56, 64), 8.0625, (437,561)),
((40, 72, 56, 96), 8.6575, (432,555)),
((16, 16, 32, 32), 26.875, (287,131)),
((80, 16, 96, 32), 30.8125, (254,107)),
]
create_image_with_paste(skin_image, target_image_path, canvas_size, operations)
image_url = url_for('static', filename=f'cache/{os.path.basename(target_image_path)}')
return render_template('index.html', image_url=image_url)
@app.route('/mc-avatar-api/<username>', methods=['GET'])
def mc_avatar_api(username):
cache_filename = get_cache_filename(username)
if cache_filename and is_cache_valid(cache_filename):
return send_file(cache_filename)
uuid = get_uuid_by_name(username)
if uuid is None:
return jsonify({"error": "没有找到该玩家"}), 404
textures = get_player_textures(uuid)
if textures is None:
return jsonify({"error": "无法获取玩家纹理信息"}), 500
if 'SKIN' in textures['textures']:
skin_url = textures['textures']['SKIN']['url']
skin_image = download_image(skin_url)
if skin_image:
target_image_path = cache_filename
canvas_size = (1000, 1000) # 新画布的大小
skin_size = skin_image.size
if skin_size == (64, 32):
operations = [
((8, 40, 16, 64), 8.375, (434,751)), # 右腿
((8, 40, 16, 64), 8.375, (505,751), True), # 左腿,使用右腿镜像
((86, 40, 92, 64), 8.167, (388,561)), # 右臂
((86, 40, 92, 64), 8.167, (566,561), True), # 左臂,使用右臂镜像
((40, 40, 56, 64), 8.0625, (437,561)), # 躯干
((16, 16, 32, 32), 26.875, (287,131)), # 头部
((80, 16, 96, 32), 30.8125, (254,107)), # 头部外层
]
else:
operations = [
((8, 40, 16, 64), 8.375, (434,751)),
((8, 72, 16, 96), 9.375, (428,737)),
((40, 104, 48, 128), 8.375, (505,751)),
((8, 104, 16, 128), 9.375, (503,737)),
((86, 40, 92, 64), 8.167, (388,561)),
((88, 72, 94, 96), 9.5, (382,538)),
((74, 104, 80, 128), 8.167, (566,561)),
((104, 104, 110, 128), 9.5, (564,538)),
((40, 40, 56, 64), 8.0625, (437,561)),
((40, 72, 56, 96), 8.6575, (432,555)),
((16, 16, 32, 32), 26.875, (287,131)),
((80, 16, 96, 32), 30.8125, (254,107)),
]
create_image_with_paste(skin_image, target_image_path, canvas_size, operations)
return send_file(target_image_path)
return jsonify({"error": "没有找到皮肤信息"}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)