I'm using pygame on a raspberry pi.
This same code used to cover the full screen at 800x600, now, at 1280x720 it doesn't, and it's not over/underscan:
Pygame example output:
Video playback via omxplayer:
All the code in the pygame example image is just a demo for the problem:
import pygame
import time
pygame.display.init()
pygame.font.init()
screen = pygame.display.set_mode((1280, 720)) #, pygame.FULLSCREEN)
screen.fill((255, 0, 0))
pygame.display.flip()
time.sleep(45)
You can use the list_modes function which returns a list of available full screen resolutions:
modes = pygame.display.list_modes()
if modes: # check if the list is not empty
screen = pygame.display.set_mode(modes[0], pygame.FULLSCREEN) # use the first one
else:
screen = pygame.display.set_mode((800, 600)) # use a default resolution
Note: if you have problems with high DPI scaling on Windows (e. g. a part of your display is not visible), you can use this code to fix them:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(1)
Related
I was trying to do my project in full screen and i used full screen but sometimes the display comes for 1 second then it disappears and i tried it with no full screen and it works but it doesn't in full screen and my code is:
import pygame
from pygame.locals import*
pygame.init() screen=pygame.display.set_mode((1360,768),pygame.FULLSCREEN,pygame.RESIZABLE)
pygame.display.init()
It looks like you're having trouble because you pass pygame.RESIZABLE as the third argument (the depth) to pygame.display.set_mode whereas you should combine it with the pygame.FULLSCREEN flag with a bitwise OR.
screen = pygame.display.set_mode((1360,768), pygame.FULLSCREEN|pygame.RESIZABLE)
I'm making a firework simulation in pygame, and I want to be able to run the program, and have it open up in full screen. Not the pygame.FULLSCREEN, I still want to be able to use pygame.QUIT.
I don't know if this is possible, but if anyone could help, please share your ideas!
Here's my code for the screen:
import pygame
screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
You could use the width and height of the screen to setup the resolution, like in this post, by using the VideoInfo object provided by pygame:
import pygame
pygame.init()
video_infos = pygame.display.Info()
width, height = video_infos.current_w, video_infos.current_h
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
# [...]
I am trying to use overlays in pygame to display video. The trouble is that my frames are loaded as RGB Surface()s while Overlay().display() requires YUV format.
I saw that pygame.camera module contains a colorspace() function that should be able to convert RGB Surface() to YUV one. Does anyone know how to do the trick? The conversion and the displaying?
pygame.camera.colorspace() is not very well documented.
If this doesn't work, does anyone know how to do this by using PIL to convert to YUV?
I haven't had much time to play with .Overlay()
but the colorspace function seems to go as follows:
yuv_surface = pygame.camera.colorspace(rgb_surface,"YUV")
This example runs without error:
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
screen = pygame.display.set_mode((400,400))
rgb_surface = pygame.Surface((400,400))
yuv_surface = pygame.camera.colorspace(rgb_surface,"YUV")
screen.blit(yuv_surface,(0,0))
clock = pygame.time.Clock()
while True:
pygame.display.flip()
clock.tick(30)
Trying to build my first pygame project and wanted to import the following map. Used image:
My code is:
import pygame
pygame.init()
size = (1300, 700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
map = pygame.image.load('map.jpg')
map = map.convert()
map = pygame.transform.scale(map, (466,700))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((255,255,255))
screen.blit(map, (430, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
and the output looks like:
I have also tried converting the jpg to a bmp but still the same results. I cannot figure out what is distorting the image, trying with other images it appears that the outputted images is slightly wider than the original. I suspect that pygame is just reading the width wrong and then putting the pixels into an array with that width.
Any advice on fixing this would be much appreciated.
There is a bug in SDL_image (which Pygame is based on) that corrupts images in OS X 10.11.
https://bugzilla.libsdl.org/show_bug.cgi?id=3154
Currently, the only known workaround is to downgrade to SDL_image 1.2.10, but I have not been able to confirm.
I'm trying to get my webcam to show video through pygame. Here is the code:
# 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()
When i try this I get an error from the screen.blit( image, ( 0, 0 ) ) part
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
I assume It's because I didn't convert the image into whatever works with pygame, but i don't know.
Any help would be appreciated.Thanks.
-Alex
OK so here is the new code:
This one works because it saves the picture to the current folder. figured out why the last one work. the screen is still black although=\
# 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()
Try creating a Camera like this.
cam = pygame.camera.Camera(camlist[0],(640,480))
That's how it's done on this page of the pygame docs.
Looking at the API page for pygame.camera, I found two things that might help. First,
Pygame currently supports only Linux and v4l2 cameras.
EXPERIMENTAL!: This api may change or disappear in later pygame
releases. If you use this, your code will very likely break with the
next pygame release.
Keep that in mind when wondering why this surprisingly fails.
On a more up-beat note... you can try calling camera.get_raw() and printing the result. It should be a string with raw image data. If you get an empty string, None, or some non-sense text: please share that with us here. It'll tell if you're getting anything from the camera.
Gets an image from a camera as a string in the native pixelformat of the camera. Useful for integration with other libraries.