【发布时间】:2017-02-13 09:30:18
【问题描述】:
我正在尝试使用 Xubuntu 14.04 下的以下 Python 代码在我的辅助显示器上连续显示存储在 ./pics 目录下的图像。
from PyQt4 import QtCore, QtGui
import sys
import os
import multiprocessing as mp
import time
def displayImage( impath ):
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
# Select monitor
screenres = QtGui.QApplication.desktop().screenGeometry(1);
window.move(QtCore.QPoint(screenres.x(), screenres.y()))
window.resize(screenres.width(), screenres.height())
# Set the image to be shown
pic = QtGui.QLabel(window)
pic.setGeometry(0, 0, 1960, 1080)
pic.setPixmap(QtGui.QPixmap( impath ))
window.showFullScreen()
app.exec_()
return
def controller( imdir ):
# List the contents of the directory
im_paths = os.listdir( imdir )
# Keep only image files
pc = list(im_paths)
for p in pc:
print p
if not p.split('.')[-1] in [ 'jpg', 'JPG', 'png', 'PNG' ]:
im_paths.remove( p )
if not im_paths:
return 1
# Start showing the images by calling project function as a new process (multiprocessing module)
for f in im_paths:
print 'Showing image:', p, '...'
# Run project process
p = mp.Process( target=displayImage, args=( imdir+f, ) )
p.start()
# Keep dispaying image for 3 seconds
time.sleep(2)
p.terminate()
return 0
if __name__ == '__main__':
controller( './pics/' )
exit(0)
问题
显示图像A的进程终止有一段时间,直到进程结束 显示图像 B 出现在应用程序不显示任何内容的地方,并且有一段时间呈现桌面破坏了用户体验。
任何想法如何使用 Qt4 连续显示图像?
附言。可能会接受涉及 matplotlib 和/或 opencv 的答案,但我认为直接的路径是通过 Qt。
【问题讨论】:
-
只是一个问题:为什么要为显示的每张图像启动一个新的 QApplication,而不是只启动一个而不只是更新
Pixmap中的图像?正确初始化需要时间。然后当你已经显示一个图像时,创建一个新的QPixmap并在x秒后使用QPixmap.swap切换? -
嗨!我对 Qt 没有经验,所以我无法想象最佳解决方案是什么。你的回答似乎是个好主意!所以,我正在努力。您能否提供一个使用 QPixmap.swap 方法的示例?