-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgwentCardGetter.py
More file actions
351 lines (296 loc) · 10.7 KB
/
Copy pathgwentCardGetter.py
File metadata and controls
351 lines (296 loc) · 10.7 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import os
import time
import requests
from bs4 import BeautifulSoup
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import io
# 设置WebDriver路径
webdriver_path = r'C:\Users\ADMIN\AppData\Local\Programs\Python\Python311\chromedriver.exe'
# 配置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless") # 无头模式,不显示浏览器窗口
chrome_options.add_argument("--window-size=992,1424") # 设置窗口大小为992x1424
# 初始化WebDriver
service = Service(webdriver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
temp_html_path = r'./assets\temp\temp.html'
header = '''
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./eimg.css">
<link rel="stylesheet" type="text/css" href="./cardart-v9.css">
<link rel="stylesheet" type="text/css" href="./asset-v9.css">
<link rel="stylesheet" type="text/css" href="./rebuild_sy.css">
<style>
body {
display: flex;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f8f9fa;
}
.h2c-txt-wrap {
width: 992px;
height: 1424px;
border: 1px solid #ccc;
border-radius: 20px;
background-color: #a18c6c;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
padding: 60px;
box-sizing: border-box;
background-image: url("cardpreview_paper_bg.png");
}
.h2c-txt-wrap {
font-size: 36px;
margin: 0;
text-align: left;
}
.h2c-card--header {
margin-bottom: 20px;
padding: 10px;
border-radius: 10px;
}
.h2c-card__title {
font-size: 48px;
font-weight: bold;
}
.h2c-card__category {
font-size: 24px;
/*color: #555;*/
}
.h2c-card--body {
font-size: 20px;
line-height: 1.5;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
background-color: rgba(255, 255, 255, 0.2);
}
.h2c-card__ability {
margin-bottom: 20px;
text-shadow: 2px 2px 4px #ffffff ;
}
.h2c-keyword {
display: none;
}
.h2c-flavor {
display: incline;
font-style: italic;
padding: 10px;
border-radius: 10px;
}
.h2c-version {
display: none;
}
.card {
font-size: clamp(10px, 1vw, 48px);
}
.h2c-card--header, .h2c-card--body, .h2c-card__ability, .h2c-card__footer {
font-size: inherit;
}
.h2c-card__title {
font-size: clamp(16px, 8vw, 128px);
}
.h2c-card__category {
font-size: clamp(12px, 6vw, 48px);
}
.h2c-card--body {
font-size: clamp(10px, 4vw, 40px);
line-height: 1.5;
}
.h2c-card__ability {
font-size: 36px;
margin: 0;
text-align: left;
line-height: 1.5;
letter-spacing: 1px;
margin-bottom: 20px;
}
.h2c-card__header {
font-size: 36px;
margin: 0;
text-align: left;
line-height: 1.5;
letter-spacing: 1px;
margin-bottom: 20px;
}
.h2c-card--body {
margin-bottom: 15px;
}
.h2c-card__keyword {
line-height: 1.5;
letter-spacing: 0.5px;
}
.h2c-card__flavor {
margin-top: 20px;
font-size: 26px;
color: #555;
font-style: italic;
}
.left-footer {
position: absolute; /* 绝对定位 */
left: 30px; /* 距离左边界10px,确保不超出左边界 */
bottom: 30px; /* 距离底部10px */
font-size: 18px; /* 小号字体 */
color: black; /* 黑色文字 */
text-align: left; /* 文字左对齐 */
padding: 5px; /* 内边距 */
z-index: 10; /* 确保在卡牌内容上方 */
}
</style>
<title>Card Display</title>
</head>
'''
# 卡牌ID,你需要替换成你想要查询的卡牌的ID
def getCardInfo(card_id):
# 构建POST请求的URL和参数
url = 'https://api.gwent.one/'
params = {
'key': 'data',
'id': card_id,
'response': 'html', # 选择返回数据的格式
'language': 'cn'
}
# 发送POST请求
response = requests.post(url, data=params)
# 检查请求是否成功
if response.status_code == 200:
# 获取JSON格式的数据
card_data = response.content
# 保存数据到html文件
with open(fr'.\cards\{card_id}.html', 'wb') as f:
f.write(card_data)
print(f'Card data for ID {card_id} has been saved successfully.')
return card_data
else:
print(f'Failed to retrieve card data. Status code: {response.status_code}')
return None
def getCardPic(card_id):
html = getCardInfo(card_id)
# 解析HTML
soup = BeautifulSoup(html, 'html.parser')
# 找到所有的img标签
images = soup.find_all('img')
info = soup.find("div", class_="G1-info")
info_html = getnewSoup(str(info))
# 保存HTML片段为文件
with open(temp_html_path, 'w', encoding='utf-8') as file:
file.write(header)
file.write(info_html)
# 使用Selenium打开HTML文件并截图
driver.get('file://' + os.path.realpath(temp_html_path))
time.sleep(1) # 等待页面加载完成
# 截图并保存
screenshot_path = rf'./assets\cards_pic_full\card_description_{card_id}.png'
driver.save_screenshot(screenshot_path)
print(f"Screenshot saved as: {screenshot_path}")
flag = False
# 创建一个空白图像作为画布,大小根据第一张图片设置
if not images:
flag = True
print("No images found, leader detected.")
images = [fr"./assets\temp\{id_faction_dict[card_id]}.png", f"https://gwent.one/image/gwent/assets/ability/{card_id}.png"]
if flag:
first_img_url = images[0]
first_img = Image.open(first_img_url)
else:
first_img_url = images[0].get('src').replace("low", "max")
response = requests.get(first_img_url)
first_img = Image.open(io.BytesIO(response.content))
canvas = Image.new('RGBA', first_img.size, color=(0, 0, 0, 0))
# 将所有图片叠加到画布上
for img in images:
if flag:
img_url = img
else:
img_url = img.get('src').replace("low", "max")
if "number" not in img_url:
print(img_url)
if "http" in img_url:
response = requests.get(img_url)
img_data = Image.open(io.BytesIO(response.content))
else:
img_data = Image.open(img_url)
if flag:
transparent_img = Image.new('RGBA', first_img.size, (0, 0, 0, 0))
# 计算小图片在透明图像上的位置(这里以居中为例)
x = (first_img.width - img_data.width) // 2
y = (first_img.height - img_data.height) // 2
# 确保遮罩图像的尺寸与img_data相同,且模式为'L'或'1'
mask = img_data.split()[-1].convert('L') # 使用img_data的alpha通道作为遮罩
if mask.mode != 'L':
mask = mask.convert('L')
# 将小图片粘贴到透明图像上,使用mask作为遮罩
transparent_img.paste(img_data, (x, y))
img_data = transparent_img
canvas = Image.alpha_composite(canvas.convert('RGBA'), img_data.convert('RGBA'))
# 保存结果
# canvas.save(rf'.\cardImages\image_{card_id}.png')
return canvas
def getnewSoup(html):
# 解析原始HTML
original_soup = BeautifulSoup(html, 'html.parser')
# 提取数据
name = original_soup.find('div', {'class': 'G1-name'}).text
ability = original_soup.find('div', {'class': 'G1-ability'}).get_text(strip=True)
keywords = original_soup.find('div', {'class': 'G1-keywords'})
if keywords:
keywords = keywords.get_text(strip=True)
flavor_text = original_soup.find('div', {'class': 'G1-flavor'}).get_text(strip=True)
category = original_soup.find('div', {'class': 'G1-category'}).get_text(strip=True)
# 构建新HTML
new_html = f'''
<body>
<div class="h2c-wrap h2c-card h2c-card--out Neutral Gold Unit">
<div class="h2c-txt-wrap">
<div class="h2c-card--header">
<div class="h2c-card__title">{name}</div>
<div class="h2c-card__category">{category if category else ''}</div>
</div>
<!-- header -->
<div class="h2c-card--body">
<div class="h2c-card__ability">{ability}<br></div>
<div class="h2c-keyword">
<div class="h2c--split-40"></div>
<div class="h2c-card__keyword">{keywords if keywords else ''}</div>
</div>
<div class="h2c-flavor">
<div class="h2c--split-80"></div>
<div class="h2c-card--footer">
<div class="h2c-card__flavor">{flavor_text}</div>
</div>
</div>
</div>
</div>
<div class="h2c-version">
<div class="text-right">游戏版本 12.7.0</div>
</div>
</div>
</body>
'''
return new_html
import pandas as pd
# 读取CSV文件
path_to_csv = 'cards_full.csv' # 替换为你的CSV文件路径
df = pd.read_csv(path_to_csv)
# 创建一个字典,键为'data-id'的值,值为'data-faction'的值
id_faction_dict = df.set_index('data-id')['data-faction'].to_dict()
card_data = df['data-id']
allNum = len(card_data)
# 修正:将迭代操作应用于整个DataFrame而非单列
for index, row in df.iterrows():
card_id = row['data-id']
print(card_id, index, allNum)
save_path = rf'./assets\cards_pic_full\card_image_{card_id}.png'
# 检查文件是否已经存在
if not os.path.exists(save_path):
card_image = getCardPic(card_id)
card_image.save(save_path)
# time.sleep(1)
else:
print(f"File {save_path} already exists, skipping.")