How do I play a video using pygame? [duplicate] - python

This question already has answers here:
How to load and play a video in pygame
(3 answers)
Closed 1 year ago.
I want to play a video as a background in a pygame window, and draw some images on it, like this:
http://imgur.com/a/qRiEe
However, since pygame no longer has pygame.movie module I'm wondering what could be the alternative way of playing video in pygame window.

You can use moviepy module to display videos now since pygame.movie is removed. MoviePy will display the video with pygame. Here's an example:
from moviepy.editor import VideoFileClip
import pygame
pygame.display.set_caption('My video!')
clip = VideoFileClip('myvideo.mp4')
clip.preview()
pygame.quit()
You can also visit their docs for mor explanation

You can use the cv2 module (OpenCV) that can be installed with the command prompt command:
pip install opencv-python
Then, you can run the code:
import cv2
import pygame
cap = cv2.VideoCapture('video.mp4')
success, img = cap.read()
shape = img.shape[1::-1]
wn = pygame.display.set_mode(shape)
clock = pygame.time.Clock()
while success:
clock.tick(60)
success, img = cap.read()
for event in pygame.event.get():
if event.type == pygame.QUIT:
success = False
wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
pygame.display.update()
pygame.quit()

Related

How to load a complex SVG into pygame correctly?

I am making a chess game in Python with PyGame using the python-chess library.
When I implement my program in PyGame the window only shows the empty board without the pieces. However, if I manually open the svg file that my code generates it shows correctly, with the pieces on the board. The problem is - when using blit to display the same image in PyGame I get an empty board.
To be clear, the issue is not a general 'loading SVG in PyGame' question. I am successfully displaying my SVG in this example. The question is a PyGame/python-chess question:
Why is PyGame loading the image that has the pieces in it as an empty board if it's the same file?
Code:
import pygame as pg
import chess
import chess.svg
import chess.pgn
WIDTH, HEIGHT = 900, 500
WINDOW = pg.display.set_mode((WIDTH,HEIGHT))
pg.display.set_caption('Opening mastery')
FPS = 60
board = chess.BaseBoard()
def get_board_img(brd):
SVG = chess.svg.board(board=brd)
f = open("image.svg", "w")
f.write(SVG)
f.close()
image = ('image.svg')
return image
def main():
clock = pg.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
image = get_board_img(board)
WINDOW.blit(pg.image.load(image),(0,0))
pg.display.update()
pg.quit()
if __name__ == '__main__':
main()
This is the generated SVG file opened manually (it has the pieces)
This is the result (no pieces on the board)
It appears that pygame's SVG loading library (nanosvg) can't handle that SVG.
I found a workaround using cairosvg to convert the SVG to a PNG and then using pygame to load that.
I made a new get_board_image function that returns a surface directly
def get_board_img(brd):
svg = chess.svg.board(board=brd)
png_io = io.BytesIO()
cairosvg.svg2png(bytestring=bytes(svg, "utf8"), write_to=png_io)
png_io.seek(0)
surf = pg.image.load(png_io, "png")
return surf
It uses cairosvg to get the svg bytes into a png BytesIO object that pygame can load.
^ To get that snippet working in your code you need to add the imports for io and cairosvg (and install cairosvg) ofc, as well as changing your main loop a bit:
while run:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
WINDOW.blit(get_board_img(board),(0,0))
pg.display.update()

pygame.error: File is not a Windows BMP file on m1 Mac running Mac os Monteray

I've looked around but haven't found any solutions that work.
I'm just learning pygame using the tutorial: https://www.youtube.com/watch?v=jO6qQDNa2UY&list=WL&index=44&t=1379s
but when I try to link the image I get the error: pygame.error: File is not a Windows BMP file.
I've tried everything short of reinstalling pygame again (previous issue), or reinstalling python.
Here is my code
import pygame
import os
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
WHITE = (255,255,255)
RED = (255,0,0)
FPS = 60
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png')).convert()
RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png')).convert()
def draw_window():
WIN.fill((RED))
WIN.blit(YELLOW_SPACESHIP_IMAGE, (300,100))
pygame.display.update()
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.QUIT()
if __name__ == "__main__":
main()
You try to load spaceship_yellow.png and spaceship_red.png which judging from extensions are PNGs not BMPs, your error
pygame.error: File is not a Windows BMP file
suggest that pygame is expecting BMP file. pygame.image docs says
The image module is a required dependency of pygame, but it only
optionally supports any extended file formats. By default it can only
load uncompressed BMP images. When built with full image support, the
pygame.image.load() function can support the following formats.
then enumarate formats, apparently you do not have full image support, I think simplest solution is to convert PNGs you have to uncompressed BMP image format.
Turning the image to a BMP does work but I would rather still have the full functionality of pygame. What I did to finally make it work was from a thread here. I put this in the terminal to get it working:
python3 -m pip install git+https://github.com/nelsonlove/pygame.git
This will update your pygame to version 2.0.2.dev1, modified for m1 Mac.

blurring background in pygame [duplicate]

This question already has an answer here:
Blurring in PyGame
(1 answer)
Closed 2 years ago.
As a beginner in pygame, I am trying to build a game and want to blur the background as the game ends and show player his/her score and ask if he/she wants to play again.
I don't know how to blur the background in pygame. Can anyone help me?
Thank You.
To blur the background in PyGame, you can try using the Python Imaging Library (PIL). Here's a link which has sample code on how to do it:
stackoverflow.com/questions/30723253/blurring-in-pygame
Hope this helps!
Maybe try to insert an video that blur at the end unfortunately i dont think there is an blurring function.
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('the bluring video.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
if i find a way to blur the image ill post it. :)

Displaying a video in pygame

I followed what was done in the question in the following link (How to load and play a video in pygame)
But what actually happens is a smaller window pops up with a black screen, video doesnt play.
Video is in .mpg
def game_credits():
Creds = pygame.movie.Movie("C:\Users\itzrb_000\Documents\gameCreds.mpg")
screen = pygame.display.set_mode(Creds.get_size())
Creds_screen = pygame.Surface(Creds.get_size()).convert()
Creds.set_display(Creds_screen)
Creds.play
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(Creds_screen, (0, 0))
pygame.display.update()
clock.tick(50)
Thanks for reading
For new readers, please note, that the Movie class, has been removed from pygame.
You could try the moviepy or pyglet modules. But it's not integrated to pygame.

image not blitting correctly in pygame [duplicate]

This question already has answers here:
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
pygame installation issue in mac os
(5 answers)
Closed 2 years ago.
I wanted to blit an image in python, a png image. But for some reason about half the image has the wrong pixel colours, it looks all messy, having random coloured pixels in it. I have made a few games on my Macbook Pro (OSX) using pygame with python 3.4, and I had no trouble when it came to blitting images. Here's the code:
import pygame
import os
w = pygame.display.set_mode((300,300))
bug = os.path.join("/Users/Snyman/Desktop/images/double-block_spm_R.png")
bug = pygame.image.load(bug).convert_alpha()
bug = pygame.transform.scale(bug,(64,64))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
w.fill((255,255,255))
w.blit(bug,(10,10))
pygame.display.update()
pygame.quit()
"bug" is my image

Categories