简体   繁体   中英

python pygame blit. Getting an image to display

I'm trying to get my webcam to show video through pygame. Here is the code:

# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

# set up a camera object
cam = pygame.camera.Camera(0)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    image = cam.get_image()
    # blank out the screen
    screen.fill((0,0,2))
    # copy the camera image to the screen
    screen.blit( image, ( 0, 0 ) )
    # update the screen to show the latest screen image
    pygame.display.update()

When i try this I get an error from the screen.blit( image, ( 0, 0 ) ) part

Traceback (most recent call last):
  File "C:\Python32\src\webcam.py", line 28, in <module>
    screen.blit( image, ( 0, 0 ) )
TypeError: argument 1 must be pygame.Surface, not None

I assume It's because I didn't convert the image into whatever works with pygame, but i don't know.

Any help would be appreciated.Thanks.

-Alex

OK so here is the new code: This one works because it saves the picture to the current folder. figured out why the last one work. the screen is still black although=\\

# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()
# set up a camera object
size = (640,480)
screen = pygame.display.set_mode(size,0)


surface = pygame.surface.Surface(size,0,screen)

cam = pygame.camera.Camera(0,size)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    pic = cam.get_image(surface)
    # blank out the screen
    #screen.fill((0,0,0))
    # copy the camera image to the screen
    screen.blit(pic,(0,0))
    # update the screen to show the latest screen image
    p=("outimage.jpg")

    pygame.image.save(surface,p)
    pygame.display.update()

Try creating a Camera like this.

cam = pygame.camera.Camera(camlist[0],(640,480))

That's how it's done on this page of the pygame docs.


Looking at the API page for pygame.camera , I found two things that might help. First,

Pygame currently supports only Linux and v4l2 cameras.

EXPERIMENTAL!: This api may change or disappear in later pygame releases. If you use this, your code will very likely break with the next pygame release.

Keep that in mind when wondering why this surprisingly fails.

On a more up-beat note... you can try calling camera.get_raw() and printing the result. It should be a string with raw image data. If you get an empty string, None , or some non-sense text: please share that with us here. It'll tell if you're getting anything from the camera.

Gets an image from a camera as a string in the native pixelformat of the camera. Useful for integration with other libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM