使用Python在OpenGL中的屏幕截图
我们如何使用 python 截取 OpenGL 窗口的屏幕截图?使用 jupyter、opengl 和 pygame。 代码是:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from math import *
a=[[cos(0.5*pi/180),sin(0.5*pi/180),0],
[-sin(0.5*pi/180),cos(0.5*pi/180),0],
[0,0,1]]
def zarb_matris(p,b):
c=[b[0][0]*p[0][0]+b[0][1]*p[1][0]+b[0][2]*p[2][0],
b[1][0]*p[0][0]+b[1][1]*p[1][0]+b[1][2]*p[2][0],
b[2][0]*p[0][0]+b[2][1]*p[1][0]+b[2][2]*p[2][0]]
return c
verticies= [
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, -1],
[1, -1, 1],
[1, 1, 1],
[-1, -1, 1],
[-1, 1, 1]
]
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces= (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
colors = (
(0.9,0,0),
(0,1,0),
(0.75,0.38,0),
(0,0,1),
(1,1,0),
(1,1,1),
(1,0,0),
(0,1,0),
(0.75,0.38,0),
(0,0,1),
(1,1,0),
(0.9,1,1)
)
def Cube():
global verticies
glBegin(GL_QUADS)
x = 0
for surface in surfaces:
x+=1
for vertex in surface:
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glBegin(GL_LINES)
glColor3fv((1,1,1))
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
global s
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glEnable(GL_DEPTH_TEST)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(1,1, -10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 12, 0,55115 )
for i in range(8):
s=[]
for j in verticies[i]:
s.append([j])
k=zarb_matris(s,a)
verticies[i]=k
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
我尝试了几种方法,但似乎都不起作用,或者需要对当前代码进行大量修改。有什么可以用来最小程度地更改代码,或者存在一个库或文档以便我可以尝试实现它?
How do we take a screenshot of the OpenGL windows using python? Using jupyter, opengl and pygame.
The code is:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from math import *
a=[[cos(0.5*pi/180),sin(0.5*pi/180),0],
[-sin(0.5*pi/180),cos(0.5*pi/180),0],
[0,0,1]]
def zarb_matris(p,b):
c=[b[0][0]*p[0][0]+b[0][1]*p[1][0]+b[0][2]*p[2][0],
b[1][0]*p[0][0]+b[1][1]*p[1][0]+b[1][2]*p[2][0],
b[2][0]*p[0][0]+b[2][1]*p[1][0]+b[2][2]*p[2][0]]
return c
verticies= [
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, -1],
[1, -1, 1],
[1, 1, 1],
[-1, -1, 1],
[-1, 1, 1]
]
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces= (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
colors = (
(0.9,0,0),
(0,1,0),
(0.75,0.38,0),
(0,0,1),
(1,1,0),
(1,1,1),
(1,0,0),
(0,1,0),
(0.75,0.38,0),
(0,0,1),
(1,1,0),
(0.9,1,1)
)
def Cube():
global verticies
glBegin(GL_QUADS)
x = 0
for surface in surfaces:
x+=1
for vertex in surface:
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glBegin(GL_LINES)
glColor3fv((1,1,1))
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
global s
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
glEnable(GL_DEPTH_TEST)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(1,1, -10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 12, 0,55115 )
for i in range(8):
s=[]
for j in verticies[i]:
s.append([j])
k=zarb_matris(s,a)
verticies[i]=k
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
I tried a couple of methods, but none of them seem to work, or require heavy modification of the current code. Is there anything that can be used to change the code minimally, or a library or a documentation that exists so that i can try to implement it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在之前使用
glReadPixels
读取帧缓冲区显示已更新(在pygame.display.flip()
或pygame.display.update()
之前)。使用pygame.image.fromstring()
从缓冲区创建新的表面。最后,将Surface保存到文件中:Read the framebuffer with
glReadPixels
before the display is updated (beforepygame.display.flip()
orpygame.display.update()
). Usepygame.image.fromstring()
to create new Surfaces from the buffer. Finally, save the Surface to a file: