pygame將文字轉爲圖片

安裝python

pip install pygame

下載微軟雅黑字體
http://font.chinaz.com/940144340.htmide

代碼示例字體

# -*- coding: utf-8 -*-

import pygame

# pygame初始化
pygame.init()

# 待轉換文字
text = "文字轉圖片"

# 設置字體和字號系統字號
# font = pygame.font.SysFont('Microsoft YaHei', 64)

# 設置字體和字號,自定義
font = pygame.font.Font('msyh.ttf', 64)
# 渲染圖片,設置背景顏色和字體樣式, 前面的顏色是字體顏色
ftext = font.render(text, True, (104, 51, 207), (255, 255, 255))
# 保存圖片
pygame.image.save(ftext, "image.jpg")  # 圖片保存地址

在這裏插入圖片描述

相關文章
相關標籤/搜索