Skip to content

Instantly share code, notes, and snippets.

@Jokymon
Created August 31, 2023 14:00
Show Gist options
  • Save Jokymon/67a5781d4014279d03c55ce9e39c6fb0 to your computer and use it in GitHub Desktop.
Save Jokymon/67a5781d4014279d03c55ce9e39c6fb0 to your computer and use it in GitHub Desktop.
Showing an image from command line using pygame
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