【问题标题】:How to save an image with the outline text using pygame?如何使用pygame保存带有大纲文本的图像?
【发布时间】:2021-02-11 12:24:39
【问题描述】:

下面的代码保存了这个image

import pygame

pygame.init()

font = pygame.font.SysFont('arialroundedbold.ttf', 60)
text = font.render("hello world", True, (0, 0, 0), (255, 255, 255))

pygame.image.save(text, "hello_world.png")

如何在文本 "hello world" 周围添加一个具有特定粗度(比如说 3 点)的红色轮廓并保存?

【问题讨论】:

标签: python pygame pygame-surface pygame2


【解决方案1】:

如果想在字母周围画轮廓,请参阅Have an outline of text in Pygame

import pygame

pygame.init()
window = pygame.display.set_mode((400, 400))

def render_text_outline(font, text, color, background, outline, outlinecolor):
    outlineSurf = font.render(text, True, outlinecolor)
    outlineSize = outlineSurf.get_size()
    textSurf = pygame.Surface((outlineSize[0] + outline*2, outlineSize[1] + 2*outline))
    textSurf.fill(background)
    textRect = textSurf.get_rect()
    offsets = [(ox, oy) 
        for ox in range(-outline, 2*outline, outline)
        for oy in range(-outline, 2*outline, outline)
        if ox != 0 or ox != 0]
    for ox, oy in offsets:   
        px, py = textRect.center
        textSurf.blit(outlineSurf, outlineSurf.get_rect(center = (px+ox, py+oy))) 
    innerText = font.render(text, True, color).convert_alpha()
    textSurf.blit(innerText, innerText.get_rect(center = textRect.center)) 
    return textSurf

font = pygame.font.SysFont('arialroundedbold.ttf', 60)
text_outline = render_text_outline(
    font, "hello world", (0, 0, 0), (255, 255, 255), 3, (255, 0, 0))

# save to file
pygame.image.save(text_outline, "hello_world.png")

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((127, 127, 127))
    window.blit(text_outline, text_outline.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

如果要在文本框周围绘制轮廓,请定义轮廓的宽度和颜色:

width = 3
outline_color = "red"

创建一个pygame.Surface,它比文本Surface宽和高两倍的外线粗细:

outline_w = text.get_width() + width*2
outline_h = text.get_height() + width*2
text_outline = pygame.Surface((outline_w, outline_h))

用轮廓颜色填充新的Surface

text_outline.fill(outline_color)

将文本Surface放在新Surface的中间:

text_outline.blit(text, text.get_rect(center = text_outline.get_rect().center))

小例子:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 400))

font = pygame.font.SysFont('arialroundedbold.ttf', 60)
text = font.render("hello world", True, (0, 0, 0), (255, 255, 255))

width = 3
outline_color = "red"
outline_w = text.get_width() + width*2
outline_h = text.get_height() + width*2
text_outline = pygame.Surface((outline_w, outline_h))
text_outline.fill(outline_color)
text_outline.blit(text, text.get_rect(center = text_outline.get_rect().center))

# save to file
pygame.image.save(text_outline, "hello_world.png")

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((127, 127, 127))
    window.blit(text_outline, text_outline.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

【讨论】:

  • 我需要第一个(我们在每个字母表周围都有轮廓),但问题是我不想打开 pygame 窗口,而是将图像保存为 .png... 所以该怎么做那个?
  • @Himanshu 只需删除应用程序循环,但要执行pygame.image.save(text_outline, "hello_world.png")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 2015-10-12
  • 2015-05-14
  • 1970-01-01
  • 2016-11-01
相关资源
最近更新 更多