Python實(shí)現(xiàn)修改字體字形和提取矢量數(shù)據(jù)
一、函數(shù)1:修改字體字形(change_glyph)
功能
- 將字體文件中指定字形(如"A")替換為新的矢量輪廓。
代碼實(shí)現(xiàn)
from fontTools.pens.transformPen import TransformPen
from fontTools.ttLib import TTFont
from fontTools.pens.recordingPen import RecordingPen
from fontTools.pens.ttGlyphPen import TTGlyphPen
def change_glyph(font_path, glyph_name, new_glyph_path):
# 加載字體文件
font = TTFont(font_path)
# 獲取 glyf 表
glyf_table = font['glyf']
# 確保字形存在
if glyph_name in glyf_table.glyphs:
# 記錄原始輪廓(可選,用于對(duì)比)
recording_pen = RecordingPen()
glyf_table[glyph_name].draw(recording_pen, font)
# 創(chuàng)建新輪廓
new_glyph_pen = TTGlyphPen(font.getGlyphSet())
# 定義新輪廓(此處為一個(gè)簡(jiǎn)單的矩形)
new_glyph_pen.moveTo((100, 100))
new_glyph_pen.lineTo((200, 100))
new_glyph_pen.lineTo((200, 200))
new_glyph_pen.lineTo((100, 200))
new_glyph_pen.closePath()
# 替換字形
glyf_table[glyph_name] = new_glyph_pen.glyph()
# 保存修改后的字體
font.save(new_glyph_path)
else:
print(f"字形 '{glyph_name}' 不存在!")
關(guān)鍵步驟解析
- 加載字體:通過(guò)
TTFont加載字體文件。 - 獲取 glyf 表:
glyf表存儲(chǔ)字形的矢量輪廓數(shù)據(jù)。 - 記錄原始輪廓:使用
RecordingPen記錄原始字形的路徑數(shù)據(jù)(可選)。 - 生成新輪廓:通過(guò)
TTGlyphPen定義新輪廓的坐標(biāo)點(diǎn)(如矩形)。 - 替換并保存:將新輪廓寫入
glyf表,并保存為新字體文件。
應(yīng)用場(chǎng)景
- 自定義字體設(shè)計(jì):修改特定字符的形狀(如Logo字體)。
- 修復(fù)字體缺陷:調(diào)整模糊或不規(guī)則的字形。
二、函數(shù)2:提取所有字形矢量數(shù)據(jù)(extract_all_glyph_vector_data)
功能
- 遍歷字體中所有字符,提取其矢量路徑數(shù)據(jù)并保存為文本文件。
代碼實(shí)現(xiàn)
def extract_all_glyph_vector_data(font_path, output_file):
font = TTFont(font_path)
cmap = font.getBestCmap() # 字符編碼到字形名的映射
glyph_set = font.getGlyphSet()
file_content = ""
for char_code, glyph_name in cmap.items():
glyph = glyph_set[glyph_name]
pen = RecordingPen()
transform_pen = TransformPen(pen, (1, 0, 0, 1, 0, 0)) # 無(wú)變換
glyph.draw(transform_pen)
# 格式化輸出
character = chr(char_code) if char_code <= 0x10FFFF else f"U+{char_code:04X}"
data = f"Character: {character} (U+{char_code:04X})\nVector Data: {pen.value}\n\n"
file_content += data
# 保存到文件
with open(output_file, "w", encoding="utf-8") as f:
f.write(file_content)
關(guān)鍵步驟解析
- 獲取字符映射:通過(guò)
cmap表將Unicode編碼映射到字形名稱。 - 遍歷所有字符:逐個(gè)提取字形的矢量數(shù)據(jù)。
- 記錄路徑數(shù)據(jù):使用
RecordingPen獲取字形的路徑指令(如moveTo,lineTo)。 - 保存為文本:將所有字符的矢量數(shù)據(jù)寫入文件,便于后續(xù)分析。
輸出示例
Character: A (U+0041) Vector Data: MoveTo((100, 200)) LineTo((300, 200)) LineTo((200, 400)) ClosePath()
應(yīng)用場(chǎng)景
- 字體逆向工程:分析字體設(shè)計(jì)邏輯或版權(quán)問(wèn)題。
- 自動(dòng)化處理:批量提取字形數(shù)據(jù)用于機(jī)器學(xué)習(xí)訓(xùn)練。
三、使用示例
# 修改字體中的"A"字形
change_glyph(
font_path="simsun.ttf", # 輸入字體路徑
glyph_name="A", # 目標(biāo)字形名稱
new_glyph_path="modified.ttf" # 輸出路徑
)
# 提取所有字形數(shù)據(jù)
extract_all_glyph_vector_data(
font_path="simsun.ttf",
output_file="simsun_vectors.txt"
)
四、注意事項(xiàng)
字體兼容性:
- 支持
.ttf和.otf格式,但 OpenType 字體需額外處理。 - 修改后字體需通過(guò)
fontTools驗(yàn)證:
- 支持
python -m fontTools.validate modified.ttf
性能優(yōu)化:
- 處理大字體時(shí),建議分批次處理或使用多線程。
版權(quán)問(wèn)題:
- 修改商業(yè)字體需遵守版權(quán)協(xié)議,開(kāi)源字體(如Google Fonts)更易操作。
五、擴(kuò)展功能
1. 轉(zhuǎn)換坐標(biāo)系
通過(guò) TransformPen 可以對(duì)字形進(jìn)行縮放、旋轉(zhuǎn)等變換:
# 縮放字形為原尺寸的50% scale = 0.5 transform = (scale, 0, 0, scale, 0, 0) transform_pen = TransformPen(pen, transform)
2. 可視化字形
使用 matplotlib 可視化字形輪廓:
import matplotlib.pyplot as plt
def plot_glyph(glyph):
pen = PathPen(glyph)
glyph.draw(pen)
path = pen.path
for element in path:
vertices = element.vertices
codes = element.codes
plt.plot(vertices[:,0], vertices[:,1], marker='o')
plt.show()
六、總結(jié)
通過(guò) fontTools,我們可以直接操作字體的底層矢量數(shù)據(jù),實(shí)現(xiàn)字形修改、分析和自動(dòng)化處理。無(wú)論是設(shè)計(jì)個(gè)性化字體,還是研究字體結(jié)構(gòu),這些工具都能提供強(qiáng)大的支持。
以上就是Python實(shí)現(xiàn)修改字體字形和提取矢量數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python修改字形和提取矢量數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決TypeError: Object of type xxx is&
這篇文章主要介紹了解決TypeError: Object of type xxx is not JSON serializable錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
淺析python實(shí)現(xiàn)布隆過(guò)濾器及Redis中的緩存穿透原理
本文帶你了解了位圖的實(shí)現(xiàn),布隆過(guò)濾器的原理及 Python 中的使用,以及布隆過(guò)濾器如何應(yīng)對(duì) Redis 中的緩存穿透,相信你對(duì)布隆過(guò)濾器已經(jīng)有了一定的認(rèn)識(shí)2021-09-09
使用TensorBoard進(jìn)行超參數(shù)優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了使用TensorBoard進(jìn)行超參數(shù)優(yōu)化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情
這篇文章主要介紹了Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
Python實(shí)戰(zhàn)使用XPath采集數(shù)據(jù)示例解析
這篇文章主要為大家介紹了Python實(shí)戰(zhàn)之使用XPath采集數(shù)據(jù)實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-04-04
Python基于機(jī)器學(xué)習(xí)方法實(shí)現(xiàn)的電影推薦系統(tǒng)實(shí)例詳解
這篇文章主要介紹了Python基于機(jī)器學(xué)習(xí)方法實(shí)現(xiàn)的電影推薦系統(tǒng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
python opencv圓、橢圓與任意多邊形的繪制實(shí)例詳解
在本篇文章里小編給大家整理的是關(guān)于python-opencv-圓、橢圓與任意多邊形的繪制內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02
如何用Pythony驗(yàn)證萬(wàn)物歸一(考拉咨猜想)
考拉咨猜想簡(jiǎn)單的來(lái)說(shuō),就是你隨便給我一個(gè)整數(shù),我最后都是會(huì)通過(guò)固定的規(guī)則演變成"1",萬(wàn)物歸一.今天我們就用那Python驗(yàn)證一下這個(gè)猜想2021-06-06

