It works, but when I opened the result I saw little squares where objects with an alpha layer were:
The little squares aren’t put there by panda of course, they’re there because my image viewer puts them there to show me the transparency. So the picture above is actually a screenshot of the screenshot seen in my image viewer.
Questions: How does it work ? Does it make sense for a screenshot to have some transparency ?
I could avoid the problem by using jpg or bmp, but I like png for its lossless compression.
It’s the alpha channel of the framebuffer. You could disable it by disabling alpha writes, or by requesting a framebuffer without alpha channel (framebuffer-alpha #f). Alternatively, you could use the alternate lower-level screenshot method that saves to a PNMImage, and then remove the alpha channel from the PNMImage, and write that to disk.
I chose to take the PNMImage way. Although I am not doing any post-processing, I don’t want to limit my options in the future by removing frame buffer planes.
This code works very well, no more little squares.
#! /usr/bin/python
"""
Module screenshot.
Save screenshots to disk using threads.
"""
import threading
from panda3d.core import PNMImage #@UnresolvedImport
def _shoot(screen, file_name):
print "Screenshot..."
screen.write(file_name)
print "...taken", file_name
def take(win, file_name):
screen = PNMImage()
if win.getScreenshot(screen):
screen.removeAlpha()
t = threading.Thread(target=_shoot, args=(screen, file_name))
t.start()
else:
print "Screenshot derped."
Thanks!
Question: what could cause win.getScreenshot(screen) to return False?