import
os
import
re
import
pygame
import
shutil
from
pygame.
locals
import
*
from
PIL
import
Image
def
is_chinese_char(char):
return
'\u4e00'
<
=
char <
=
'\u9fff'
def
extract_chinese_chars_from_file(file_path):
chinese_chars
=
set
()
with
open
(file_path,
'r'
, encoding
=
'utf-8'
) as
file
:
for
line
in
file
:
chinese_chars.update(
filter
(is_chinese_char, line))
return
chinese_chars
def
extract_chinese_chars_from_folder(folder_path):
all_chinese_chars
=
set
()
for
root, dirs, files
in
os.walk(folder_path):
for
file
in
files:
if
file
.endswith(
'.gml'
):
file_path
=
os.path.join(root,
file
)
all_chinese_chars.update(extract_chinese_chars_from_file(file_path))
return
all_chinese_chars
def
setim(value,rect,big
=
20
):
global
myfont,screen
text
=
myfont.render(value,
False
,(
255
,
255
,
255
))
screen.blit(text,rect)
def
make_black_transparent(image_path, output_path):
image
=
Image.
open
(image_path).convert(
"RGBA"
)
data
=
image.getdata()
new_data
=
[]
for
item
in
data:
if
item[
0
]
=
=
0
and
item[
1
]
=
=
0
and
item[
2
]
=
=
0
:
new_data.append((
0
,
0
,
0
,
0
))
else
:
new_data.append(item[:
3
]
+
(
255
,))
if
len
(item)
=
=
3
else
new_data.append(item)
image.putdata(new_data)
image.save(output_path,
format
=
"PNG"
)
def
main(TextFolder:
str
, FontPath:
str
, FontSize:
int
, StartXY:
tuple
, StepXY:
tuple
, CountLine:
int
, UTMT_Size_Width:
int
, UTMT_Size_Height:
int
, UTMT_Shift:
int
, UTMT_Offset:
int
):
pygame.init()
global
myfont,screen
folder_path
=
TextFolder
chinese_chars
=
extract_chinese_chars_from_folder(folder_path)
chinese_chars.add(
"—"
)
sorted_chinese_chars
=
sorted
(chinese_chars, key
=
lambda
c:
ord
(c))
size
=
width,height
=
4096
,
1024
bg
=
pygame.image.load(r
"fnt_text_eng.png"
)
screen
=
pygame.display.set_mode(size)
pygame.display.set_caption(
"PNG"
)
screen.blit(bg,(
0
,
0
))
myfont
=
pygame.font.Font(FontPath,FontSize)
x
=
StartXY[
0
]
y
=
StartXY[
1
]
xStep
=
StepXY[
0
]
yStep
=
StepXY[
1
]
ty
=
str
(y
-
2
)
xcount
=
0
shutil.copy(
'EngCSV.txt'
,
'glyphs_fnt_text_eng.csv'
)
with
open
(r
"glyphs_fnt_text_eng.csv"
,
"a"
) as f:
for
char
in
sorted_chinese_chars:
setim(char,(x,y))
tx
=
str
(x
-
1
)
ty
=
str
(y
-
2
)
xcount
+
=
1
x
+
=
xStep
if
(xcount
=
=
CountLine):
xcount
=
0
x
=
StartXY[
0
]
y
+
=
yStep
f.write(
";"
.join([
str
(
ord
(char)), tx, ty,
str
(UTMT_Size_Width),
str
(UTMT_Size_Height),
str
(UTMT_Shift), (
str
(UTMT_Offset)
+
"\n"
)]))
pygame.display.flip()
pygame.image.save(screen,
"screenshot.png"
)
make_black_transparent(
"screenshot.png"
,
"output.png"
)
os.remove(
"screenshot.png"
)
if
__name__
=
=
"__main__"
:
main(r
"E:\PAK\AllText"
,
"方正像素12.ttf"
,
24
,(
100
,
250
),(
27
,
30
),
80
,
24
,
27
,
23
,
0
)