Skip to content

Instantly share code, notes, and snippets.

@JnyJny
Last active December 19, 2019 16:32
Show Gist options
  • Save JnyJny/4996d87cce5e9209b3d271a62b139c25 to your computer and use it in GitHub Desktop.
Save JnyJny/4996d87cce5e9209b3d271a62b139c25 to your computer and use it in GitHub Desktop.
Code for reproducing a suspected pyglet PNG saving bug.
#!/usr/bin/env python3
import pyglet
class RenderText(pyglet.window.Window):
def __init__(self, text, width=512, height=256):
super().__init__(width=width, height=height)
pyglet.gl.glClearColor(0, 0, 0, 1) # Fixes the problem.
self.document = pyglet.text.decode_text(text)
self.layout = pyglet.text.layout.TextLayout(
self.document, width, height, multiline=True
)
self.document.set_style(
start=0,
end=0,
attributes={
"font_size": 12,
"color": (255, 255, 255, 255),
"font_name": "Source Code Pro",
},
)
def on_draw(self):
self.clear()
self.layout.draw()
pyglet.image.get_buffer_manager().get_color_buffer().save("output.png")
if __name__ == "__main__":
text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt \
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nis\
i ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cil\
lum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui o\
fficia deserunt mollit anim id est laborum."""
window = RenderText(text)
pyglet.app.run()
@JnyJny
Copy link
Author

JnyJny commented Dec 19, 2019

solved by adding to the __init__ of my Window descended class:

pyglet.gl.glClearColor(0, 0, 0, 1) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment