This program may help you with using a webcam and converting to strings and arrays to avoid storing them ,same as you I started with the save to file method but found this much quicker
Gordon77
Code: Select all
#!/usr/bin/python
import pygame, sys
from pygame.locals import *
import pygame.camera
import numpy
import time
black = 0,0,0
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(352,288))
cam.start()
time.sleep(1)
windowSurfaceObj = pygame.display.set_mode((352,288))
# take first picture
image1 = cam.get_image()
#save if required
#pygame.image.save(image1,'11.jpg')
# convert to a string
strim1 = pygame.image.tostring(image1,"RGB",1)
# display first picture from string
image = pygame.image.fromstring(strim1,(352,288),"RGB",1)
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
time.sleep(2)
windowSurfaceObj.fill(black)
pygame.display.update()
# take second picture
image2 = cam.get_image()
#save if required
#pygame.image.save(image2,'12.jpg')
# convert to a string
strim2 = pygame.image.tostring(image2,"RGB",1)
# display second picture from string
image = pygame.image.fromstring(strim2,(352,288),"RGB",1)
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
time.sleep(2)
windowSurfaceObj.fill(black)
pygame.display.update()
mx = []
my = []
# convert first picture from string to a numpy.array
mx = numpy.fromstring(strim1, dtype='uint8')
# convert second picture from string to a numpy.array
my = numpy.fromstring(strim2, dtype='uint8')
# add both pictures together, average
strim3 = (mx/2) + (my/2)
# convert resultant numpy.array to a string
output = numpy.ndarray.tostring(strim3)
# display output picture from string
image3 = pygame.image.fromstring(output,(352,288),"RGB",1)
#save if required
pygame.image.save(image3,'13.jpg')
catSurfaceObj = image3
windowSurfaceObj.blit(catSurfaceObj,(1,1))
pygame.display.update()