Created
August 31, 2023 14:00
-
-
Save Jokymon/67a5781d4014279d03c55ce9e39c6fb0 to your computer and use it in GitHub Desktop.
Showing an image from command line using pygame
This file contains 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
from os import environ | |
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' | |
import threading | |
import pygame | |
class ImageShower(): | |
def __init__(self): | |
self.show_image = True | |
self.thread = None | |
def show(self, image_path): | |
self.thread = threading.Thread(target=self, args=(image_path,)) | |
self.show_image = True | |
self.thread.start() | |
def hide(self): | |
if self.show_image: | |
self.show_image = False | |
self.thread.join() | |
def __call__(self, image_path): | |
pygame.init() | |
# screen = pygame.display.set_mode((640, 480)) | |
img = pygame.image.load(image_path) | |
img_rect = img.get_rect() | |
screen = pygame.display.set_mode((img_rect.width, img_rect.height)) | |
screen.blit(img, img_rect) | |
pygame.display.flip() | |
while self.show_image: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
print("Quit requested") | |
pygame.display.update() | |
img = ImageShower() | |
img.show("C:/projects/VariableAnimation2d/assets/box.png") | |
input("> ") | |
img.hide() | |
input("> ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment