728x90
반응형
Python 환경에서 OpenGL 창을 image로 저장할 때 사용하는 코드는 아래와 같다.
여기서는 PIL 라이브러리를 사용한다.
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
from PIL import ImageOps
import sys
width, height = 300, 300
def init():
glClearColor(0.5, 0.5, 0.5, 1.0)
glColor(0.0, 1.0, 0.0)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
glViewport(0, 0, width, height)
def render():
glClear(GL_COLOR_BUFFER_BIT)
# draw xy axis with arrows
glBegin(GL_LINES)
# x
glVertex2d(-1, 0)
glVertex2d(1, 0)
glVertex2d(1, 0)
glVertex2d(0.95, 0.05)
glVertex2d(1, 0)
glVertex2d(0.95, -0.05)
# y
glVertex2d(0, -1)
glVertex2d(0, 1)
glVertex2d(0, 1)
glVertex2d(0.05, 0.95)
glVertex2d(0, 1)
glVertex2d(-0.05, 0.95)
glEnd()
glFlush()
def draw():
render()
glutSwapBuffers()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(300, 300)
glutCreateWindow(b"OpenGL Offscreen")
glutHideWindow()
init()
render()
glPixelStorei(GL_PACK_ALIGNMENT, 1)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
image = Image.frombytes("RGBA", (width, height), data)
image = ImageOps.flip(image) # in my case image is flipped top-bottom for some reason
image.save('glutout.png', 'PNG')
main()
여기서 OpenGL 윈도우 창을 PIL image로 변환하는 핵심 코드는 아래 부분이다.
glPixelStorei(GL_PACK_ALIGNMENT, 1)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
image = Image.frombytes("RGBA", (width, height), data)
image = ImageOps.flip(image) # in my case image is flipped top-bottom for some reason
image.save('glutout.png', 'PNG')
참고자료 : https://stackoverflow.com/questions/41126090/how-to-write-pyopengl-in-to-jpg-image
728x90
반응형
'Programming > Etc.' 카테고리의 다른 글
[Tmux] Tmux 사용법 (0) | 2023.04.27 |
---|---|
[GitHub] 주로 사용하는 GitHub 명령어 모음 (4) | 2022.10.28 |
[Ubuntu] 우분투 하드디스크 이름 변경 (0) | 2022.06.27 |
[FFmpeg] 비디오 자르기(video crop) (0) | 2022.01.24 |
[FFmpeg] usb cam 이용하여 실시간 영상 저장하기 (0) | 2022.01.13 |