pygame camera on Windows [duplicate] - python

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required

Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()

The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()

This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6

Related

blit opencv camera capture with pygame throws TypeError: argument 1 must be pygame.Surface, not cv2.VideoCapture

I'm new to pygame and never used it before and wanted to know how I can blit or display my webcam into the surface by using pygame and opencv but I keep getting the message:
Traceback (most recent call last):
File "<filename>.py", line 51, in <module>
mainWindow()
File "<filename>.py", line 43, in mainWindow
draw_window()
File "<filename>.py", line 24, in draw_window
WINDOW.blit(camera)
TypeError: argument 1 must be pygame.Surface, not cv2.VideoCapture
import pygame
import cv2
pygame.init()
# setting the width and height of the window
WIDTH, HEIGHT = 1280, 720
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("name me")
# background color
color = (0, 0, 0)
# 0 is the built in webcam
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 700)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 900)
def draw_window():
# background color
WINDOW.fill((color))
# display object onto the surface (screen)
WINDOW.blit(camera)
# update the display
pygame.display.update()
FPS = 30
def mainWindow():
# keeping the window open
run = True
clock = pygame.time.Clock()
while run:
# capping it at the set frame rate
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
# closing the window
pygame.quit()
# main #
mainWindow()
You can only blit a pygame.Surface. Therefore you have to get frame by frame from the camera and convert it to a pygame.Surface object.
Grab a camera frame:
success, camera_image = capture.read()
Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:
camera_surf = pygame.image.frombuffer(
camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
Do that in the function draw_window:
def draw_window():
# background color
WINDOW.fill((color))
# display object onto the surface (screen)
success, camera_image = camera.read()
if success:
camera_surf = pygame.image.frombuffer(camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
WINDOW.blit(camera_surf, (0, 0))
# update the display
pygame.display.update()
Also see python pygame.camera.init() NO vidcapture and PyGameExamplesAndAnswers - Camera and Video
Alternative but minimal example:
import pygame
import cv2
capture = cv2.VideoCapture(0)
success, camera_image = capture.read()
window = pygame.display.set_mode(camera_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, camera_image = capture.read()
if success:
camera_surf = pygame.image.frombuffer(
camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
else:
run = False
window.blit(camera_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
If your goal is to get webcam input into pygame, you could use pygame.camera
It supports Windows and Linux webcams natively, falls back to an OpenCV backend on MacOS.
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
screen = pygame.display.set_mode((720,500))
cam_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(cam_list[0], (720,500))
camera.start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("black")
screen.blit(camera.get_image(), (0,0))
pygame.display.flip()
pygame.quit()

CV2 Camera output inside PyGame [duplicate]

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6

No video device on chromebook

I'm trying to code a game using pygame in googles colaboratory using pygame. I succesfully added pygame by running:
import os
!git clone https://github.com/ntasfi/PyGame-Learning-Environment.git
os.chdir('PyGame-Learning-Environment')
!pip install -e .
!pip install pygame
os.chdir('/content')
But when I wrote this code:
import pygame
pygame.init()
BLACK = [0,0,0]
size = [100,100]
screen = pygame.display.set_mode((size))
screen.fill(BLACK)
screen.update()
pygame.quit()
I get the following error message:
Traceback (most recent call last)
<ipython-input-10-b26800e3009a> in <module>()
BLACK = [0,0,0]
size = [100,100]
screen = pygame.display.set_mode((size))
screen.fill(BLACK)
Error: No available video device
Is there any way i can add make pygame detect the laptop screen?
first of all try this
import pygame
pygame.init()
BLACK = (0, 0, 0) #dont use []'s
WIDTH = 0 #yourwidth
HEIGHT = 0 #yourheight
screen = pygame.display.set_mode((WIDTH,HEIGHT))
screen.fill(BLACK)
running = True
while(running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

Pygame on mac error with loading images

I recently started programming on pygame, I use python 3.5.1 and the latest pygame and when I load a bitmap(bmp) it puts it on the screen like this
At school when I program on the computer it works fine just on my macbook pro 13'' with el capitain
import pygame #imports pygame
width = 550 #width is 550
height = 420 #height is 420
size = width, height # size is a tuple
screen = pygame.display.set_mode(size)
white = 255,225,255
screen.fill(white)
gameOn = True
mario = pygame.image.load("mario.bmp") #load the bitmap
marioflip = pygame.transform.flip(mario, True, True)
screen.blit(marioflip, (0,0)) # add it to screen
marioscale = pygame.transform.scale(mario, (50,50))
screen.blit(marioscale, (250,250))
mariorotate = pygame.transform.rotate(mario, 45)
screen.blit(mariorotate,(350,0))
while gameOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
You are seeing a known bug in SDL_image on that version of OS X. See this link for details and a workaround:
https://bitbucket.org/pygame/pygame/issues/284/max-osx-el-capitan-using-the-deprecated

Rendering unicode in pygame

I want to render some unicode characeters on screen.
Using pygame.font displays a weird character.
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.font.Font(None, 64)
font_surf = FONT.render("♛", True, pygame.Color("red"))
screen.blit(font_surf, (20, 20))
pygame.display.flip()
pygame.time.delay(1000)
I also tried using pygame.freetype. It displays nothing at all.
import pygame.freetype
pygame.freetype.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.freetype.Font(None)
FONT.render_to(screen, (20, 20), "♛", size=(40, 40))
pygame.display.flip()
pygame.time.delay(1000)
You need to add your font name and location .
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
On Python 3.4 you no longer need the u before "♛" like in Python 2.7.
unistr = "♛"
sample based on that other link but for 3.4 as example is 2.7
# -*- coding: utf-8 -*-
import pygame
import sys
unistr = "♛"
pygame.font.init()
srf = pygame.display.set_mode((500,500))
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
srf.blit(f.render(unistr,True,(255,0,0)),(0,0))
pygame.display.flip()
while True:
srf.blit(f.render(unistr,True,(255,255,255)),(0,0))
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()

Categories

Resources