我試圖讓我的攝像頭通過pygame顯示視頻。下面是代碼:python pygame blit。獲取圖像以顯示
# 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()
當我嘗試這一點,我從screen.blit(圖像,(0,0))部分
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
得到一個錯誤,我想這是因爲我沒有將圖像轉換成pygame的任何作品,但我不知道。
任何幫助將不勝感激。謝謝。
-Alex
行,所以這裏是新代碼: 這一個工作,因爲它會將照片保存到當前文件夾。想出了爲什麼最後一個工作。屏幕仍然是黑色的,雖然= \
# 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()
'''cam.get_image()'''正在返回''''''''。 – FakeRainBrigand
錯誤意味着圖像是無,而不是它是錯誤格式的有效圖像。顯然cam.get_image()出於某種原因失敗。 – Dave