I am writing a program that will take a screenshot of the window in pygame and change the background to that image with ctypes.windll.user32.SystemParametersInfoW. However, saving the image then opening the image to use as the background every frame is slow and I imagine not very good for my computer. Is there a way to take a screen shot E.g. with pygame.image.tostring and use pillow to change the background?
The code so far is:
import pygame, ctypes, random, time
pygame.init()
size = (200,150)
screen = pygame.display.set_mode(size)
path = "C:/Users/theo/image.jpg"
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((0,0,0))
pygame.draw.circle(screen, [random.randint(0,255) for i in range(3)], (10,10), 10)
pygame.display.flip()
try:
pygame.image.save(screen, path)
time.sleep(0.05)
except:continue
try:
ctypes.windll.user32.SystemParametersInfoW(20,0,path,0)
time.sleep(0.05)
except:pass
pygame.quit()