Created
December 26, 2019 19:55
-
-
Save acbart/04cb8ef02e0b8d2d8f8b45d15bb198ea to your computer and use it in GitHub Desktop.
Demonstrates how you can now print Pygame images into Thonny's shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io | |
| import base64 | |
| import pygame | |
| from PIL import Image | |
| pygame.init() | |
| class PrintablePygameImage: | |
| BASE64_PNG_PREFIX = bytes("data:image/png;base64,", encoding='ascii') | |
| def __init__(self, size): | |
| self._s = pygame.Surface(size, pygame.SRCALPHA) | |
| def rotate(self, angle): | |
| self._s = pygame.transform.rotate(self._s, angle) | |
| def circle(self, color, position, radius): | |
| pygame.draw.circle(self._s, color, position, radius) | |
| def __repr__(self): | |
| surf_as_str = pygame.image.tostring(self._s, 'RGBA') | |
| surf_as_pil = Image.frombytes('RGBA', self._s.get_size(), surf_as_str) | |
| surf_as_bytes = io.BytesIO() | |
| surf_as_pil.save(surf_as_bytes, format="png") | |
| base64_png = base64.b64encode(surf_as_bytes.getvalue()) | |
| base64_png = self.BASE64_PNG_PREFIX + base64_png | |
| return base64_png.decode('utf-8') | |
| s = PrintablePygameImage((100, 100)) | |
| s.circle((255, 0, 0), (10, 10), 10) | |
| s.circle((0, 255, 0), (50, 50), 10) | |
| s.circle((0, 0, 255), (90, 90), 10) | |
| print(s) | |
| s.rotate(90) | |
| print(s) | |
| s.rotate(-45) | |
| print(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment