在pyglet文档中,我发现:
以下示例显示如何获取应用程序窗口的屏幕截图:
pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')
但是当使用它时,一切都会停止,直到我点击鼠标.是否有另一种方法来获取Pyglet中的屏幕内容,或强制它回到事件循环?
编辑:我发现实际上有一个短暂的延迟(0.2秒〜),但没有别的.实际上它与停止pyglet的F10键有关.> _>
我无法关闭或删除,因为有开放的赏金.
我需要为摄影测量应用程序生成一系列测试图像.这些应包含具有非常精确已知位置的简单对象(磁盘,矩形等).
考虑到白色背景上的黑色矩形的8位灰度图像,最小的可观察位移(插值后)应为1/256像素,因为每个像素有256种可能的强度等级.
我决定使用OpenGL(使用python + pyglet)来渲染这样的图像,因为稍后我将需要渲染更复杂的(3d场景,立体图像对)
不幸的是,我所达到的最佳精度是在像素/ 10左右,没有使用全强度深度.
是否有可能做得更好,理想情况下 - 实现完整的1/256像素精度?有关如何做到这一点的任何提示,好吗?
示例代码,生成部分磁盘的图像,每帧移动0.01像素
#-*- coding: utf-8 -*-
import pyglet
from pyglet.gl import *
from PIL import Image, ImageGrab
config = pyglet.gl.Config(width = 800, height = 600, sample_buffers=1, samples=16)
window = pyglet.window.Window(config=config, resizable=True)
window.set_size(800, 600)
printScreenNr = 0
@window.event
def on_draw():
global printScreenNr
window.clear()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity()
glEnable(GL_LINE_SMOOTH)
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glTranslated(200, 200,0)
circleQuad = gluNewQuadric()
glTranslated(200+(printScreenNr*0.01), 0, 0)
gluPartialDisk(circleQuad, 0, 50, 500, 500, 0, 180)
@window.event
def on_resize(width, height):
glViewport(0, 0, …Run Code Online (Sandbox Code Playgroud) 这是代码.5000弹跳旋转红色正方形.(16x16 png)在pygame版本上,我得到30 fps但是带有pyglet的10 fps.对于这种事情,Isnt OpenGl应该更快吗?
pygame版本:
import pygame, sys, random
from pygame.locals import *
import cProfile
# Set FPS
FPS = 60.0
clock = pygame.time.Clock()
# Set window
WINDOWWIDTH= 800
WINDOWHEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
screen.fill((0,0,0))
background = screen.copy().convert()
image = pygame.image.load("square.png").convert()
class Square(object):
def __init__(self,x,y):
self.x = x
self.y = y
self.v_x = random.randint(1,100)
self.v_y = random.randint(1,100)
self.v_r = random.randint(-100,100)
self.rotation = 0
def __rep__(self):
return "Square %d,%d"%(self.x,self.y)
def update(self,dt):
if self.x > WINDOWWIDTH:
self.v_x *= -1 …Run Code Online (Sandbox Code Playgroud) 我正在使用python的pyglet模块(Windows上的python 3).当我引用pyglet.image中的任何类时,python的CPU使用率会跳起来并且在我退出python之前不会下降.例如:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Anaconda3>python.exe
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Mar 6 2015, 12:06:10) [MSC v.1
600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyglet #No problem!
>>> pyglet.image.ImageData #Heavy CPU load until I exit python
<class 'pyglet.image.ImageData'>
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?为什么提这个类(甚至没有实例化它)会导致如此高的CPU负载?
我测试的系统:
带有Anaconda python 3.4.3的Windows 7桌面和通过'pip install pyglet'安装的pyglet:高CPU使用率(我的问题)
与Anaconda python 3.4.3相同的Win7桌面,但通过'pip install hg + https://bitbucket.org/pyglet/pyglet ' 安装了pyglet :高CPU使用率.
来自python.org的python …
我的问题从这里开始:
gTTS运行良好,从文本文件中获取文本,但首先创建mp3文件,然后如果我想听,我必须调用这个mp3,所以这是好的,但如果我可以避免任何音频文件,并且只是从文本文件.也许不知何故,我可以使用谷歌语音从文本文件中读取..?无论如何,现在主要问题是其他问题
如果我只能使用gTTS在Windows 10-64位,Python 3.5上播放mp3的最佳方法是什么
好吧用os:
import os
os.startfile("D:\my path/rec1.mp3")
Run Code Online (Sandbox Code Playgroud)
这很好,但我不想使用默认播放器,需要像simpleaudio这样的mp3 ...
与pygame我有安装问题,但不确定,这样使用它有多好:
from pygame import mixer
mixer.init()
mixer.music.load('D:/my path/rec1.mp3')
mixer.music.play()
Run Code Online (Sandbox Code Playgroud)
vlc只是如何安装它?与easy_install vlc我有错误:could not find suitable distribution for requirement.parse ('vlc')与pip install vlc错误:could not find a version that satisfies the requirement vlc (from versions: ) no matching distribution found for vlc
import vlc
p = vlc.MediaPlayer("file:/my path/rec1.mp3")
p.play()
p.stop()
Run Code Online (Sandbox Code Playgroud)
与pyglet:
import pyglet
music=pyglet.media.load('D:/my path/rec1.mp3')
music.play()
pyglet.app.run()
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
'AVbin is required to decode compressed …Run Code Online (Sandbox Code Playgroud) 编辑:rotoglup在我的代码中发现了问题,添加了我删除的着色器完成了解决方案.请参阅下面的答案以获取正确的代码(使用着色器).
大家好 !
我正在尝试从本教程中学习一些现代OpenGL的基础知识.
我想用python/pyglet而不是C++来做.我知道pyglet可以抽取大部分低级OpenGL; 我想先了解一些基础知识,然后再将它们隐藏在抽象层之后.
我的问题非常简单:下面的代码只绘制一个点而不是我期望的3个点.据我所知,我的代码与教程中的C++相同,除了删除顶点和片段着色器(通过python中的gletools完成),这似乎对我的问题没有任何影响.
将事物简化为单点显示我不理解的行为(第一个坐标似乎是唯一影响任何事物的坐标),这让我回想起我完全不理解pyglet,OpenGL,或者甚至是一般的3D:p
这是相关的代码:
import pyglet
from pyglet.gl import *
window = pyglet.window.Window()
positionBufferObject = GLuint()
vao = GLuint()
vertexPositions = [0.0, 0.0, 0.0,
0.25, 0.0, 0.0,
1.75, 1.75, 0.0]
vertexPositionsGl = (GLfloat * len(vertexPositions))(*vertexPositions)
@window.event
def on_draw():
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)
glDrawArrays(GL_POINTS, 0, 3)
glDisableVertexAttribArray(0)
glGenBuffers(1, positionBufferObject)
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject)
glBufferData(GL_ARRAY_BUFFER, len(vertexPositionsGl)*4, vertexPositionsGl, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glClearColor(0.0, 0.0, 0.0, 0.0)
pyglet.app.run()
Run Code Online (Sandbox Code Playgroud) 为了让它快速而肮脏 - 我是一个正在努力学习Pyglet的新手程序员,它看起来像一个非常干净和友好的模块使用,不像PyGame那样,即使是用我自己没有经验的眼睛看一头野兽.
然而.PyGame不断被许多人使用,更新和重用,似乎有很多追随者.Pyglet自2010年1月以来一直没有更新.大多数艺术作品从未完成,只是放弃了 - 但两年仍然在1.1.4版本上似乎令人不安.
因此,虽然我可能会特别询问Pyglet与PyGame,但我也不是,因为它让我想知道可能潜伏在那里的其他幽灵模块,曾经有过承诺,但由于某种原因,它被丢弃了或者推到一个角落,并不是真的相关.这些被遗弃的项目是否值得花时间和脑空间投资?
我有一个奇怪的问题。当 pyglet 应用程序启动时,它只绘制 1-2 帧然后冻结。on_draw 事件刚刚停止发生。但是每次我移动鼠标或按键时,on_draw 事件也会调度。简而言之,我必须移动鼠标才能使我的 pyglet 应用程序基本正常工作。
这实际上发生在 Windows 中。在带有 compiz 的 Ubuntu 中,我只需要移动一次鼠标,然后应用程序就可以正常工作了。
这是我的代码示例:
#!/usr/bin/env python
import pyglet
win = pyglet.window.Window(width=800, height=600)
label = pyglet.text.Label('Abc', x=5, y=5)
@win.event
def on_draw():
win.clear()
label.x += 1
label.draw()
pyglet.app.run()
Run Code Online (Sandbox Code Playgroud)
由于某种原因,尽管我的on_key_press()功能可以正常工作,但我的程序发生的任何鼠标事件都不会使窗口中的图像消失或重新出现。
我已经尝试过状态标志并声明新的图像资源,但是它不会更改窗口中的任何内容。
有没有办法使这项工作?我应该还原到以前的pyglet版本吗?如果是这样,哪个版本?
这是我的程序代码;它在测试图像可见的情况下运行,并且每按一次键或单击鼠标,图像就会消失或重新出现:
import pyglet
window = pyglet.window.Window()
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
@window.event
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
@window.event
def on_mouse_press(x, y, button, modifiers):
global state, image
if button == pyglet.window.mouse.LEFT:
print('mouse press')
if state:
state = False
else:
state = True
@window.event
def on_key_press(symbol, modifiers):
global state
print('key press')
if state:
state = False
else:
state …Run Code Online (Sandbox Code Playgroud) 如果我在 VSCode 终端中运行此代码
import pyglet
window = pyglet.window.Window(500, 500)
Run Code Online (Sandbox Code Playgroud)
我明白了
Traceback (most recent call last):
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 334, in __getattr__
return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'Window'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/justin/Dropbox/jr/py/pyglet_games/pyglet_demo/displaytest.py", line 5, in <module>
window = pyglet.window.Window(500, 500)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 340, in __getattr__
__import__(import_name)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/__init__.py", line 1891, in <module>
gl._create_shadow_window()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/gl/__init__.py", line 220, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False) …Run Code Online (Sandbox Code Playgroud)