pygame headless no font [duplicate] - python

I am using pygame's joystick api to use a joystick with my project on a headless system, but pygame requires a "screen" so I have setup a dummy video system to over come this. It worked fine but now all of a sudden it gives me this error:
Traceback (most recent call last):
File "compact.py", line 10, in <module>
screen = display.set_mode((1, 1))
pygame.error: Unable to open a console terminal
Here is what I have as the headless setup that is supposed to over come this issue.
from pygame import *
import os
import RPi.GPIO as GPIO
os.environ["SDL_VIDEODRIVER"] = "dummy"
screen = display.set_mode((1, 1))

Pygame is trying to open a console which means you're running this script through ssh or cron or somewhere else that doesn't have access to the console. I would try skipping set_mode (since the dummy driver likely doesn't have modes to set) and just try to initialize the display. You can try running it as root which might give it access. You can also try telling it to use fbcon.
os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()

If you don't have an actual monitor connected to the Raspberry Pi, pygame.display will not work. However, there are 2 ways to trick the system into creating a virtual display so that you can run Pygame without a monitor:
You can trick the raspberry pi into thinking there is a monitor attached by using a hdmi emulator dummy plug:
Alternatively, go to /boot folder, edit the config.txt file with sudo or root access, set:
hdmi_force_hotplug=1 (without the #)
And the pi will create a virtual display even if there is no actual monitor attached.

Related

Why doesn't my simple python pygame program open a window?

I am trying to learn how to make a game of Snake in Python. I found a tutorial using pygame, but I'm having trouble making it work. Here's my code :
import os, pygame
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
dis = pygame.display.set_mode((400,300))
pygame.display.set_caption('Test')
while 1 :
pygame.display.update()
pygame.quit()
quit()
The problem is that the game window simply doesn't open. I get ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred errors but I think those are related to the sound. Other than that, no message in the console.
The code is running in a debian 11 VM inside a Windows 7 host.
Delete the os.environ["SDL_VIDEODRIVER"] = "dummy"
From this
You can use PyGame without opening a visible display (e.g. for testing, or for integrating with other frameworks that have their own displays) by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
As the documentation states, the dummy mode is designed to create an SDL environment without opening a window. Because in this case, we do actually want to create a window, You should remove the line os.environ["SDL_VIDEODRIVER"] = "dummy". This does also mean that you don't have to import the os module.

Pygame headless setup

I am using pygame's joystick api to use a joystick with my project on a headless system, but pygame requires a "screen" so I have setup a dummy video system to over come this. It worked fine but now all of a sudden it gives me this error:
Traceback (most recent call last):
File "compact.py", line 10, in <module>
screen = display.set_mode((1, 1))
pygame.error: Unable to open a console terminal
Here is what I have as the headless setup that is supposed to over come this issue.
from pygame import *
import os
import RPi.GPIO as GPIO
os.environ["SDL_VIDEODRIVER"] = "dummy"
screen = display.set_mode((1, 1))
Pygame is trying to open a console which means you're running this script through ssh or cron or somewhere else that doesn't have access to the console. I would try skipping set_mode (since the dummy driver likely doesn't have modes to set) and just try to initialize the display. You can try running it as root which might give it access. You can also try telling it to use fbcon.
os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()
If you don't have an actual monitor connected to the Raspberry Pi, pygame.display will not work. However, there are 2 ways to trick the system into creating a virtual display so that you can run Pygame without a monitor:
You can trick the raspberry pi into thinking there is a monitor attached by using a hdmi emulator dummy plug:
Alternatively, go to /boot folder, edit the config.txt file with sudo or root access, set:
hdmi_force_hotplug=1 (without the #)
And the pi will create a virtual display even if there is no actual monitor attached.

Offline rendering of animation on server with Paraview/Python script fails

I'm running this Python script on my (Debian) server to write out an animation from vtk data:
import paraview.simple as pv
pv.servermanager.LoadState('plot.pvsm')
pv.SetActiveView(pv.GetRenderView())
pv.WriteAnimation('images/bj.png', Quality=2)
It is supposed to create this animation while I'm not logged on the server. For that I use screen and run the script with:
pvbatch --use-offscreen-rendering plot3d.py &
It does its job well by writing the image files. However, when I detach from the screen session and log out the script stops completely.
In my understanding the option --use-offscreen-rendering should make sure that no rendering on my screen happens. This works on my client machine where I can run the same script and kill the terminal and the script continues.
Maybe relevant: For every image I get the error message:
libGL error: failed to authenticate magic 1
libGL error: failed to load driver: i965
where the number after "magic" increases with each image.
My system:
Debian Linux 3.16.7-ckt11-1 (2015-05-24), kernel 3.16.0-4-amd64
Paraview 4.1.0
Python 2.7.9
Screen 4.02.01
You still need access to X server even though you're using --use-offscreen-screen flag. If you want truly offscreen, you should rebuild your ParaView with OSMesa support as described here

Prevent interruption of wand display method on Raspbian

I am using wand library in my raspberry project running raspbian and python 2.7.
I have a code part as below to display picture from an url:
with Image(file=urllib2.urlopen(r.text)) as imageOBJ:
display(imageOBJ)
These lines display the image correctly. However, I want this window to stay open and my process to continue with other things in my script. Because after 30 seconds I want to repeat the same thing and change the image in the window. Right now, my code is not running until I close the display window.
Please note that this is not the case on my mac but only on raspberry pi B+ , wheezy raspbian.
How can I prevent this behaviour without closing the image display window?
Thanks in advance
This behavior is expected as both Windows & OS X call the OS's run command start & open respectively -- see reference. On the Raspberry Pi, and other like systems, the wand library calls MagickDisplayImage directly on Python's MainThread.
To emulate like behavior on the Raspberry Pi use the xdg-open utility, and Python's subprocess and tmpfile modules.
Create a temporary file to hold the image
Write image to temporary file
Call xdg-open to open temporary file in an isolated process.
import subprocess, tempfile
from wand.image import Image
with Image(filename='wizard:') as imageOBJ:
tempOBJ = tempfile.NamedTemporaryFile(suffix='.jpg',
prefix='/tmp/myProject-',
delete=False)
imageOBJ.save(file=tempOBJ)
subprocess.call('xdg-open {}'.format(tempOBJ.name), shell=True)
Of course mileage will vary across OS distro/version/desktop-environment.

Pygame error "Video system not initialized" on ubuntu server with only terminal

I'm using pygame on a PC (PC-104) with ubuntu server, for this reason it only has terminal, not UI.
On my laptop the code works perfecly, running the code on the terminal "eg: python game.py", but on the PC104 it gives "Video system not initialized", I read the error is shown because it is a way to use events on a system without UI?
Thanks.
Thank for your answer jsbueno. I was able to found a solution a few weeks ago but forget to answer the question.
I wasn't able to run any pygame script with any other library than X11. But I found that one can run the script with no graphics library. Just setting the SDL_VIDEOLIBRARY enviroment variable to "dummy".
This is posible on bash but I prefer to do it on python:
os.environ["SDL_VIDEODRIVER"] = "dummy"
With this I was able to run the pygame script, detect joystick events, etc.
It is possible to run pygame programs in a system without X11 if you set it to use framebuffer or vgalib - the docs even talk about using aalib (which would display graphics using ascii art on the terminal.)
This part of the documentation has it:
Pygame will select from one of several internal display backends when
it is initialized. The display mode will be chosen depending on the
platform and permissions of current user. Before the display module is
initialized the environment variable SDL_VIDEODRIVER can be set to
control which backend is used. The systems with multiple choices are
listed here.
Windows : windib, directx
Unix : x11, dga, fbcon, directfb,
ggi, vgl, svgalib, aalib
So, what you have to do is set the SDL_VIDEODRIVER environment variable before starting your code. And being shure the proper lib. is installed.
For more information:
http://www.pygame.org/docs/ref/display.html

Categories

Resources