吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1547|回复: 11
收起左侧

[Python 原创] Pygame实现GameMaker中文字体文件生成(包括png和csv)

  [复制链接]
Oblaanlein 发表于 2024-12-23 18:21
本帖最后由 Oblaanlein 于 2025-1-5 03:29 编辑

最近汉化了一个小GameMaker游戏,写了这么个小脚本方便生成要导入的字体文件,可以根据选择的文件夹中所有gml文件中的文本内容及字体文件生成包括所有使用到的汉字的GameMaker可用字符字体文件,包括png和csv
下载的文件里附带了ASCII码32-127的方正像素12字体png和对应的csv作为模版,中文字体会在模版上继续添加,有需要也可以更换模版。
目前只能添加汉字,添加符号需要在第56行手动添加,且需要在UTMT里手动调整到合适尺寸。
GenerateGMFont.rar (201.75 KB, 下载次数: 9)
通过网盘分享的文件:GenerateGMFont.rar
链接: https://pan.baidu.com/s/1PqdqFD9FoHRILsT3EiXQ6A?pwd=rbw6 提取码: rbw6
效果展示:

png文件

png文件

csv文件

csv文件

UTMT字体编辑器图

UTMT字体编辑器图

[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
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
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'):   #只识别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:
        # 如果像素是黑色(R=0, G=0, B=0),则改为透明(RGBA=(0,0,0,0))
        if item[0] == 0 and item[1] == 0 and item[2] == 0:
            new_data.append((0, 0, 0, 0))
        else:
            # 否则保持原像素值,并设置Alpha通道为不透明(255)
            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):
    """参数说明:
    从左到右依次为:文本文件夹路径,字体路径,字体尺寸,绘制起始点的XY坐标(按从左往右,从上到下的顺序绘制),绘制点的行列间隔,每行字符数,UTMT中字符长宽及Shift和Offset的值
    """
    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))
    # print(len(sorted_chinese_chars))  #打印共有几个字符
 
    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:  
        # f.write('"8bitoperator JVE (DO NOT EDIT IN GAMEMAKER, Japanese characters will disappear)";24;False;False;1;0;1;1\n')
        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")]))    #生成字体信息csv文件
 
    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)

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
SherlockProel + 1 + 1 好活儿当赏

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Oblaanlein 发表于 2025-1-4 19:31
本帖最后由 Oblaanlein 于 2025-1-5 03:28 编辑
family-123456 发表于 2025-1-4 16:11
大佬,汉字能单独使用一个txt文本吗,让程序读取txt然后再打印

可以,把19行的extract_chinese_chars_from_folder函数改成这个,然后传参的时候传txt的路径,绝对路径的话记得用原始字符串,代码里忘记了:
[Python] 纯文本查看 复制代码
1
2
3
4
def extract_chinese_chars_from_folder(file_path): #从文件中提取汉字
    all_chinese_chars = set()
    all_chinese_chars.update(extract_chinese_chars_from_file(file_path))
    return all_chinese_chars
 楼主| Oblaanlein 发表于 2025-1-3 19:19
family-123456 发表于 2025-1-2 14:16
大佬,输出没有汉字,是什么原因

EngCSV.txt里是根据模版图左上角那些字符预先写好的,在里面添加数据不会绘制出来,如果不是这个原因可以看看你对代码做了什么改动吗?
SagJoker 发表于 2024-12-23 20:47
losingstars 发表于 2024-12-24 06:21
收藏了,用到时再来看。
anning666 发表于 2024-12-24 08:37
以前玩游戏的时候,想想写游戏应该是一件很有激情的事情
接触编程以后,觉得写游戏真的很辛苦..........
百来行代码搞定汉化,大佬牛X~~~~
bbclinux 发表于 2024-12-24 09:25
收藏~感谢分享!
kodesu 发表于 2024-12-24 10:58
收藏~感谢分享!
yiluoen0502 发表于 2024-12-25 08:10
谢谢楼主分享
family-123456 发表于 2025-1-2 14:16
本帖最后由 family-123456 于 2025-1-2 15:27 编辑

大佬,输出没有汉字,是什么原因
输出.png
family-123456 发表于 2025-1-4 16:11
Oblaanlein 发表于 2025-1-3 19:19
EngCSV.txt里是根据模版图左上角那些字符预先写好的,在里面添加数据不会绘制出来,如果不是这个原因可以 ...

大佬,汉字能单独使用一个txt文本吗,让程序读取txt然后再打印
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-28 18:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表