实时图像
写了一个关于实时图像滚动显示的例子,做个记录。
滚动算法:
难点:
将内存数据绘制到界面,需要用到QImage和QPixmap,使用QImage转换一下,具体参见代码。这个费了好大劲才弄出来(网上的资料大都很简单,处理个QImage打开保存啊等等操作,项目实用性不强。)
from PIL import Image
import numpy as np
import threading
import time
class GraphicWidget(QWidget):
def __init__(self):
super(GraphicWidget,self).__init__()
self.threadStop = False
self.drawWidth = 1080
self.drawHeight = 800
self.imgWholeData = None
self.imgScreenData = np.zeros([self.drawHeight,self.drawWidth],np.uint8)
self.imgTotalLines = 0
self.imgWidth = 0
self.threadStop = True
pass
def doscroll(self):
if self.threadStop:
img = Image.open("d:/test2.png")
self.imgWholeData = np.array(img)
self.imgTotalLines, self.imgWidth = self.imgWholeData.shape
scrollThread = threading.Thread(target = self.scroll)
scrollThread.start()
def stop(self):
self.threadStop = True
def scroll(self):
step = 5
srcStartLine = 0
srcEndLine = step
destEndLine = step
self.threadStop = False
while not self.threadStop:
if destEndLine > self.drawHeight:
destEndLine = self.drawHeight
if srcEndLine > self.drawHeight:
srcStartLine = srcEndLine - self.drawHeight
if srcEndLine > self.imgTotalLines:
print("scroll end")
self.threadStop = True
break
if srcStartLine < self.drawHeight:
self.imgScreenData = np.zeros([self.drawHeight,self.drawWidth],np.uint8)
self.imgScreenData[0:destEndLine] = self.imgWholeData[srcStartLine:srcEndLine]
self.update()
destEndLine = destEndLine + step
srcEndLine = srcEndLine + step
time.sleep(0.005)
pass
def paintEvent(self,event):
try:
painter = QPainter()
destRect = QRect(0,0,self.drawWidth,self.drawHeight)
srcRect = QRect(0,0,self.drawWidth,self.drawHeight)
img = QImage(self.imgScreenData.data, self.drawWidth, self.drawHeight, QImage.Format_Indexed8)
pix = QPixmap.fromImage(img)
painter.begin(self)
painter.drawPixmap(destRect, pix, srcRect)
painter.end()
except Exception as e:
print(e)
raise
Pass
经验证,图像“拖尾”比较严重,图像滚动平滑效果比不上用DirectX做的效果,但是刷新效率很高,QT这块做的比C#好很多。
下次,实用Opengl做一个,看看效果咋样。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?