Python 如何使用pygame保存带有大纲文本的图像?

Python 如何使用pygame保存带有大纲文本的图像?,python,pygame,pygame-surface,pygame2,Python,Pygame,Pygame Surface,Pygame2,下面的代码正在保存此文件 如何在文字“hello world”周围以特定的粗体(比如3点)添加红色轮廓并保存?如果要在字母周围绘制轮廓,请参见 导入pygame pygame.init() window=pygame.display.set_模式((400400)) def render_text_大纲(字体、文本、颜色、背景、大纲、大纲颜色): outlineSurf=font.render(文本、True、outlinecolor) outlineSize=outlineSurf.get_

下面的代码正在保存此文件


如何在文字“hello world”周围以特定的粗体(比如3点)添加红色轮廓并保存?

如果要在字母周围绘制轮廓,请参见

导入pygame
pygame.init()
window=pygame.display.set_模式((400400))
def render_text_大纲(字体、文本、颜色、背景、大纲、大纲颜色):
outlineSurf=font.render(文本、True、outlinecolor)
outlineSize=outlineSurf.get_size()
textSurf=pygame.Surface((大纲[0]+大纲*2,大纲[1]+2*大纲))
textSurf.fill(背景)
textRect=textSurf.get_rect()
偏移量=[(ox,oy)
对于范围内的公牛(-outline,2*outline,outline)
适用于范围内的oy(-outline,2*outline,outline)
如果ox!=0或ox!=0]
对于偏移量中的ox、oy:
px,py=textRect.center
text surf.blit(outlineSurf,outlineSurf.get_rect(center=(px+ox,py+oy)))
innerText=font.render(text,True,color).convert_alpha()
blit(innerText,innerText.get_rect(center=textRect.center))
返回文本冲浪
font=pygame.font.SysFont('arialroundedbold.ttf',60)
text\u outline=渲染text\u outline(
字体,“hello world”(0,0,0),(255,255,255),3,(255,0,0))
#保存到文件
pygame.image.save(文本“hello\u world.png”)
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((127127127))
blit(text_outline,text_outline.get_rect(center=window.get_rect().center))
pygame.display.flip()
pygame.quit()
退出()

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

width=3
轮廓颜色=“红色”
创建一个宽度为外线厚度两倍且高于文本表面的线:

outline\u w=text.get\u width()+width*2
轮廓线=文本。获取高度()+宽度*2
text_outline=pygame.Surface((outline_w,outline_h))
使用轮廓的颜色填充新曲面:

text\u outline.fill(轮廓颜色)

将文本表面置于新曲面的中间:

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

最简单的例子:

导入pygame
pygame.init()
window=pygame.display.set_模式((400400))
font=pygame.font.SysFont('arialroundedbold.ttf',60)
text=font.render(“hello world”,True,(0,0,0),(255,255,255))
宽度=3
轮廓颜色=“红色”
轮廓宽度=文本。获取宽度()+宽度*2
轮廓线=文本。获取高度()+宽度*2
text_outline=pygame.Surface((outline_w,outline_h))
文本\轮廓。填充(轮廓\颜色)
blit(text,text.get_rect(center=text_outline.get_rect().center))
#保存到文件
pygame.image.save(文本“hello\u world.png”)
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((127127127))
blit(text_outline,text_outline.get_rect(center=window.get_rect().center))
pygame.display.flip()
pygame.quit()
退出()

你是指用文本或每个字符在矩形上的轮廓吗?问题是否解决了?请阅读并考虑到你发现最有帮助的地方。我需要第一个(在每个字母表上都有大纲),但是问题不是打开PyGaby窗口,而是把图像保存为PNG…那么怎么做呢?@Himanshu只需删除应用程序循环,但要执行
pygame.image.save(text\u outline,“hello\u world.png”)
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")