2011-12-11 13 views
2

파이 게임을 통해 비디오를 보여줄 웹캠을 얻으려고합니다. 여기에 코드입니다 :이 때 나는 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 

에서 오류가 발생

# 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() 

나는하지 않았다 때문입니다 가정 파이 게임으로 작동하는 이미지로 이미지를 변환하지만, 나는 모른다.

도움이 될 것입니다. 감사합니다.

알렉스

확인 그래서 여기에 새로운 코드 : 그것은 현재 폴더에 사진을 저장하기 때문에 이 하나가 작동합니다. 마지막 한 가지 이유를 알아 냈습니다. = \

# 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() 
+0

'''cam.get_image()'''는''''''''를 돌려줍니다. – FakeRainBrigand

+0

이 오류는 이미지가 없음이라는 것을 의미하며 잘못된 형식의 유효한 이미지가 아닙니다. cam.get_image()가 어떤 이유로 실패했습니다. – Dave

답변

1

이렇게 카메라를 만들어보십시오.

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

그건이 파이 게임 문서의 this page에 어떻게하는지입니다. pygame.camera에 대한 API page 보면


, 내가 도움이 될 두 가지를 발견했다. 먼저,

현재 Pygame은 Linux 및 v4l2 카메라 만 지원합니다.

실험 :이 API는 나중에 파이 게임 에서 변경되거나 사라질 수 있습니다. 이것을 사용하면 코드가 다음 파이 게임 릴리스로 중단 될 가능성이 큽니다.

놀랍게도 왜 이것이 실패하는지 궁금 할 때 염두에 두십시오.

더 많은 메모에서 ... camera.get_raw()에 전화하여 결과를 인쇄 해 볼 수 있습니다. 원시 이미지 데이터가있는 문자열이어야합니다. 빈 문자열 인 None 또는 일부 의미없는 텍스트가 있으면 여기에 나와있는 내용을 공유하십시오. 카메라에서 아무것도 얻지 못했다면 알려줄 것입니다.

카메라에서 가져온 이미지를 카메라의 기본 픽셀 형식으로 된 문자열로 가져옵니다. 다른 라이브러리와의 통합에 유용합니다.

+0

카메라에 따라 사진 해상도를 조정해야 할 수도 있습니다. – FakeRainBrigand

+0

그런 식으로 카메라를 찾지 못합니다. 그게 내가 다른 방식으로 해낸 이유이고 그 부분은 잘 작동한다. 이미지를 화면에 놓아야하는 부분 만 작동하지 않습니다. – Avovk

+0

Ok, 어느 쪽이든,'''get_image'''는 자동으로 실패합니다. 그 전에는 뭔가 작동하지 않습니다. – FakeRainBrigand