在python中的单个窗口上显示刷新的RGB矩阵的好方法是什么?

时间:2019-04-15 12:35:34

标签: python performance

我需要将矩阵显示在一个以高频率刷新的窗口上。有没有办法在python中完成这项工作?

我利用pygame库显示了一个简单的闪烁屏幕。完成的工作是我所需要的,但是刷新率太低,我认为这与程序内部矩阵的按位运算有关。

import pygame, sys, random
import fetch_module
screen = ""
class Frontend:
    def __init__(self,**kwargs):
        self.length = 1280
        self.height = 720
        if "length" in kwargs.keys():
            self.length = kwargs["length"]
        if "height" in kwargs.keys():
            self.height = kwargs["height"]

        #self.screen = pygame.display.set_mode((self.length,self.height),flags=(pygame.FULLSCREEN|pygame.HWSURFACE|pygame.DOUBLEBUF))
        self.screen = pygame.display.set_mode((self.length,self.height))
        pygame.display.set_caption('Evolution')
        red = pygame.Color(250,255,250,0)
        zero = pygame.Color(0,0,0,0)
        self.buffer1 = [[int(red) for i in range(self.length)] for j in range(self.height)]
        self.buffer2 = [[int(zero) for i in range(self.length)] for j in range(self.height)]
        print("Init routine completed")
    def get_arr(self):
        resp = []
        buff = []
        if (self.ctr%2) == 0:
            #print("buff1")
            buff = self.buffer1
        else:
            #print("buff2")
            buff = self.buffer2
        for i in buff:
            for j in i:
                b =  j & 255
                g = (j >> 8 ) & 255
                r = (j >> 16) & 255
                a = (j >> 32) & 255
                resp.append(r)
                resp.append(g)
                resp.append(b)
                resp.append(a)
        #self.buffer1 = self.buffer2
        self.ctr+=1
        return bytes(resp)
    def service(self):
        print("Inside service routine")
        self.ctr = 0
        while True:
            buff = self.screen.get_buffer()
            buff.write(self.get_arr())
            pygame.display.update()
            if self.ctr == 10:
                break
            #self.buffer2 = fetch_module.fetch("http://localhost:8000",name="Evolution Frontend",height=self.height,length=self.length)
        pygame.quit()

front = Frontend()
front.service()

帧速率大约为1帧/秒,并且需要更高。

0 个答案:

没有答案