Search code examples
pythonpython-3.xpygamecamera

How to re-scale camera footage using pygame.camera


Hey does anyone know how to re-scale pygame camera footage I have looked everywhere but there doesn't seem to be any way of doing this. I have tried using pygame.transform.scale but the pygame screen doesn't even pop up Code:

import pygame
from pygame.locals import *
import time
import pygame.camera
import pygame.image

pygame.init()
pygame.camera.init()
pygame.display.set_caption("Game")

cameras = pygame.camera.list_cameras()

print("Using camera %s ..." % cameras[0])

cam1 = pygame.camera.Camera(cameras[0])
cam1.start()

img1 = cam1.get_image()

flags = DOUBLEBUF
screen = pygame.display.set_mode((100,100), flags, 4)

while 1:
    for event in pygame.event.get():
        pygame.event.set_allowed(KEYDOWN)
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            pygame.camera.quit()

    img1 = cam1.get_image().convert()


    scaled_img1 = pygame.transform.scale(img1, (500, 500))
    screen.blit(scaled_img1, (0, 0))
    pygame.display.update()



Solution

  • pygame.transform.scale does not scale the Surface itself, but returns a new scaled Surface:

    scaled_img1 = pygame.transform.scale(img1, (100, 100))
    

    See also How to change an image size in Pygame? and Pygame cannot make image bigger over time.

    Additionally you have to blit the image after scaling the image:

    while 1:
        # [...]
    
        scaled_img1 = pygame.transform.scale(img1, (100, 100))
        screen.blit(scaled_img1 , (0, 0))
        pygame.display.update()