1

The following code is saving this 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")

How do I add a red outline around the text "hello world" with a specific boldness (let say 3 points) and save it?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Himanshu
  • 11
  • 1

1 Answers1

2

If want to draw an outline around the letters see 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()

If you want to draw an outline around the text box, define width and color of the outline:

width = 3
outline_color = "red"

Create a pygame.Surface that is twice the thickness of the outer line wider and higher than the text Surface:

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

Fill in the new Surface with the color of the outline:

text_outline.fill(outline_color)

Put the text Surface in the middle of the new Surface:

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

Minimal example:

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I need the first one (where we have outline around each alphabet), but the problem is instead of opening the pygame window, i want to save the image as .png... so how to do that? – Himanshu Feb 12 '21 at 07:12
  • @Himanshu Just delete the applicaition loop, but do `pygame.image.save(text_outline, "hello_world.png")` – Rabbid76 Feb 14 '21 at 08:30