Open Belissimo-T opened 1 year ago
It's probably because get_color_buffer()
reads the back buffer by default. It would work in the next frame.
@BelTol3011 you should probably add most of the example reproducible code before all the pyglet.info
. It's kind of long. People might use the scrollbar to go past it accidentally, seeing the giant pyglet.info
output. Just a suggestion.,
It would work in the next frame.
Sadly can't be. I added a call to pyglet.clock.schedule_interval()
such that the on_draw
method gets called repeatedly until the screenshot is made. It still doesn't work.
I hope that's what you mean with next frame.
import pyglet
pyglet.options["headless"] = True
from pyglet.graphics import Batch
from pyglet.window import Window
import PIL.Image
import io
batch = Batch()
window = Window(500, 500)
circle = pyglet.shapes.Circle(250, 250, 100, color=(255, 255, 0), batch=batch)
@window.event
def on_draw():
print("on_draw")
window.clear()
batch.draw()
def make_screenshot(_):
out = io.BytesIO()
pyglet.image.get_buffer_manager().get_color_buffer().save(file=out)
pil_img = PIL.Image.open(out, formats=("png",))
pil_img.show()
pyglet.app.exit()
pyglet.clock.schedule_interval(lambda _: None, 1 / 60)
pyglet.clock.schedule_once(make_screenshot, 2)
pyglet.app.run()
You can try changing the buffer which ColorBuffer is taking from. For example:
pyglet.image.ColorBufferImage.gl_buffer = pyglet.gl.GL_FRONT
On my Linux machine (AMD, Mesa stack) your example works. In my case it works with either GL_FRONT or GL_BACK buffers.
@benmoran56 I included your line in my program:
import pyglet
pyglet.options["headless"] = True
from pyglet.graphics import Batch
from pyglet.window import Window
import PIL.Image
import io
batch = Batch()
window = Window(500, 500)
window.set_caption("Transparent Window")
circle = pyglet.shapes.Circle(250, 250, 100, color=(255, 255, 0), batch=batch)
@window.event
def on_draw():
print("on_draw")
window.clear()
batch.draw()
def make_screenshot(_):
out = io.BytesIO()
pyglet.image.ColorBufferImage.gl_buffer = pyglet.gl.GL_FRONT
pyglet.image.get_buffer_manager().get_color_buffer().save(file=out)
pil_img = PIL.Image.open(out, formats=("png",))
pil_img.show()
pyglet.app.exit()
pyglet.clock.schedule_interval(lambda _: None, 1 / 60)
pyglet.clock.schedule_once(make_screenshot, 2)
pyglet.app.run()
I now get the following exception when the make_screenshot()
-function is called:
Traceback (most recent call last):
File "/home/bela/.config/JetBrains/PyCharm2022.3/scratches/scratch_29.py", line 40, in <module>
pyglet.app.run()
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/app/__init__.py", line 107, in run
event_loop.run()
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/app/base.py", line 169, in run
timeout = self.idle()
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/app/base.py", line 239, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/clock.py", line 292, in call_scheduled_functions
item.func(now - item.last_ts, *item.args, **item.kwargs)
File "/home/bela/.config/JetBrains/PyCharm2022.3/scratches/scratch_29.py", line 29, in make_screenshot
pyglet.image.get_buffer_manager().get_color_buffer().save(file=out)
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/image/__init__.py", line 500, in save
encoder.encode(self, file, filename)
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/image/codecs/pil.py", line 129, in encode
image = image.get_image_data()
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/image/__init__.py", line 2046, in get_image_data
glReadBuffer(self.gl_buffer)
File "/home/bela/venv310/lib/python3.10/site-packages/pyglet/gl/lib.py", line 107, in errcheck
raise GLException(msg)
pyglet.gl.lib.GLException: b'invalid operation'
With pyglet.gl.GL_BACK
I still get the same blank image as in the initial bug report.
Describe the bug When executing the code below, I get shown the following image: I expected this image: wich is exactly what I get, when I comment line 3 (
pyglet.options["headless"] = True
).I expect the headless and not-headless outputs to be identical (at least that's what I think is intuitive).
Let me know if the functionality in the
make_screenshot
function supported in headless mode or how I can get an image of the frame buffer of a headless window, if they even have that.How To Reproduce I used the following code:
System Information: