简体   繁体   English

使用Glut和OpenGL与Python捕获屏幕

[英]Capture screen using Glut and OpenGL with Python

I'm using Python and OpenGL to make some calculations related to a physical system and then display them and be able to manipulate the image rotating it, translating it, ... 我正在使用Python和OpenGL进行与物理系统有关的一些计算,然后显示它们并能够操纵旋转它,翻译它,...的图像。

I'm not a professional programmer and I have little experience using OpenGL and I'm having some issues trying to get what I want. 我不是专业程序员,使用OpenGL的经验很少,并且在尝试获得所需的内容时遇到了一些问题。 I would like to be able to save the window that I get as an output in a video file and to do so I've seen the possibility of using glReadPixels to capture each frame and then put them all together. 我希望能够将获得的窗口保存为视频文件,并这样做,我已经看到了使用glReadPixels捕获每个帧然后将它们放在一起的可能性。

I am using right now something that looks like the following code and I want to be able to save the frames to images but, although I've been able to save to save the pixels to an array using glReadPixels , I don't know where nor how to call the function that I've defined as captureScreen . 我现在正在使用类似于以下代码的内容,并且希望能够将帧保存到图像中,但是尽管我已经能够使用glReadPixels保存将像素保存到数组中,但是我不知道在哪里以及如何调用我定义为captureScreen的函数。 What should I do to get the output that I'm looking for? 我应该怎么做才能得到想要的输出?

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
from scipy import *
import numpy as np
import random as rnd

def captureScreen(file_):

    glPixelStorei(GL_PACK_ALIGNMENT, 1)
    data = glReadPixels(0, 0, 800, 800, GL_RGBA, GL_UNSIGNED_BYTE)
    image = Image.fromstring("RGBA", (800, 800), data)
    image.save(file_, 'png')

def main():  

     glutInit(sys.argv)
     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
     glutInitWindowPosition(0, 0)
     glutInitWindowSize(800, 800)

     glutCreateWindow ("Test")

     glEnable(GL_DEPTH_TEST)
     glDepthMask(GL_TRUE)
     glEnable(GL_BLEND)
     glEnable(GL_TEXTURE_2D)
     glEnable(GL_COLOR_MATERIAL)
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
     glPointSize( 6.0 )
     glLineWidth( 2.0 )
     glEnable(GL_POINT_SMOOTH)
     glEnable(GL_LINE_SMOOTH)
     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
     glEnable(GL_POLYGON_SMOOTH)
     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)

     glEnableClientState(GL_VERTEX_ARRAY)
     glEnableClientState(GL_COLOR_ARRAY)

     glClearColor(0.0, 0.0, 0.0, 0.0)

     glutFullScreen()
     glutKeyboardFunc(keyboard)
     glutIdleFunc(dynamics)
     glutDisplayFunc(display)
     glutMouseFunc(mouse)


     glutMainLoop()


if __name__ == '__main__':

     main()

keyboard, display, dynamics and mouse are functions previously defined. 键盘,显示,动态和鼠标是先前定义的功能。

You can call glReadPixels() just after glutSwapBuffers(): 您可以 glutSwapBuffers() 之后调用glReadPixels():

glutSwapBuffers()
glReadBuffer(GL_FRONT)
glReadPixels(...)

Or just before : 之前

glReadBuffer(GL_BACK)
glReadPixels(...)
glutSwapBuffers()

So captureScreen() should be called from your "display" function which is probably the one which calls glutSwapBuffers() 因此,应从“显示”函数中调用captureScreen(),该函数可能是调用glutSwapBuffers()的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM