在更新期间像演示文稿一样显示图像而不显示桌面
Displaying images like a presentation without showing the desktop during update
我正在尝试在 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 连续显示图像吗?
PS。涉及 matplotlib and/or opencv 的答案可能会被接受,但我认为直接的路径是通过 Qt。
我有一个使用 OpenCV 和 python 的解决方案。
这是代码和解释:
import cv2 # for read/display images
import glob # for linking to image file (.jpg)
#--- following is the directory containing my images of jpg format ---
jpg_files=glob.glob("C:/Users/Desktop/Stack/*.jpg")
#--- Here I run through every image file, read it and then display it with a lapse time of 1 second ---
for img_no in range(len(jpg_files)):
x = jpg_files[img_no]
img = cv2.imread(x,1)
cv2.imshow('BINGO.jpg',img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
希望对您有所帮助
:D
终于,我找到了解决办法。 alexblae 认为不应在每次图像更新时重新启动 qt 应用程序的想法是关键点。但首先...我必须详细了解 Qt 框架的架构,一个 class 如何从另一个继承,以及如何生成和处理事件。
让我引用更新后的代码,以便其他用户在遇到类似问题时查看并获得优势。
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import sys
import os
import time
class Pic( QtGui.QLabel ):
def __init__( self, w, im_paths ):
QtGui.QLabel.__init__(self,w)
self.ims = im_paths
# Set the first image that will be displayed
self.setPixmap(QtGui.QPixmap( self.ims[0] ) )
self.i = 1
# Catch timer events. On each event display the next image
def timerEvent(self):
# Update the displayed image
self.setPixmap(QtGui.QPixmap( self.ims[self.i] ) )
self.i = ( self.i + 1 ) % len( self.ims )
def displayImages( im_paths ):
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
screenres = QtGui.QApplication.desktop().screenGeometry(1);
window.move(QtCore.QPoint(screenres.x(), screenres.y()))
window.resize(screenres.width(), screenres.height())
pic = Pic(window, im_paths)
pic.setGeometry(0, 0, 1960, 1080)
window.showFullScreen()
# Generate a timerEvent every 1 second. On each second the handler of
# the event updates the displayed image
timer = QtCore.QTimer()
timer.timeout.connect(pic.timerEvent)
timer.start(1000) #1 second
sys.exit(app.exec_())
def controller( imdir ):
# List the contents of the directory
im_paths = sorted( os.listdir( imdir ) )
for i, path in enumerate( im_paths ):
im_paths[i] = imdir + '/' + path
# Keep only image files
pc = list(im_paths)
for p in pc:
if not p.split('.')[-1] in [ 'jpg', 'JPG', 'png', 'PNG' ]:
im_paths.remove( p )
if not im_paths:
return 1
# Run project process
p = mp.Process( target=displayImages, args=( im_paths, ) )
p.start()
# Keep displaying images for 1 minute
time.sleep(5)
p.terminate()
return 0
if __name__ == '__main__':
controller( './pics/' )
exit(0)
Pic class继承自QLabel,以便定义timerEvent方法。使用计时器事件,我们可以通知 qt 应用程序何时更新显示的图像。
另外,请注意,多处理模块的使用似乎有点矫枉过正。有人可能会直接调用 displayImages 函数。
我正在尝试在 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 连续显示图像吗?
PS。涉及 matplotlib and/or opencv 的答案可能会被接受,但我认为直接的路径是通过 Qt。
我有一个使用 OpenCV 和 python 的解决方案。
这是代码和解释:
import cv2 # for read/display images
import glob # for linking to image file (.jpg)
#--- following is the directory containing my images of jpg format ---
jpg_files=glob.glob("C:/Users/Desktop/Stack/*.jpg")
#--- Here I run through every image file, read it and then display it with a lapse time of 1 second ---
for img_no in range(len(jpg_files)):
x = jpg_files[img_no]
img = cv2.imread(x,1)
cv2.imshow('BINGO.jpg',img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
希望对您有所帮助 :D
终于,我找到了解决办法。 alexblae 认为不应在每次图像更新时重新启动 qt 应用程序的想法是关键点。但首先...我必须详细了解 Qt 框架的架构,一个 class 如何从另一个继承,以及如何生成和处理事件。
让我引用更新后的代码,以便其他用户在遇到类似问题时查看并获得优势。
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import sys
import os
import time
class Pic( QtGui.QLabel ):
def __init__( self, w, im_paths ):
QtGui.QLabel.__init__(self,w)
self.ims = im_paths
# Set the first image that will be displayed
self.setPixmap(QtGui.QPixmap( self.ims[0] ) )
self.i = 1
# Catch timer events. On each event display the next image
def timerEvent(self):
# Update the displayed image
self.setPixmap(QtGui.QPixmap( self.ims[self.i] ) )
self.i = ( self.i + 1 ) % len( self.ims )
def displayImages( im_paths ):
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
screenres = QtGui.QApplication.desktop().screenGeometry(1);
window.move(QtCore.QPoint(screenres.x(), screenres.y()))
window.resize(screenres.width(), screenres.height())
pic = Pic(window, im_paths)
pic.setGeometry(0, 0, 1960, 1080)
window.showFullScreen()
# Generate a timerEvent every 1 second. On each second the handler of
# the event updates the displayed image
timer = QtCore.QTimer()
timer.timeout.connect(pic.timerEvent)
timer.start(1000) #1 second
sys.exit(app.exec_())
def controller( imdir ):
# List the contents of the directory
im_paths = sorted( os.listdir( imdir ) )
for i, path in enumerate( im_paths ):
im_paths[i] = imdir + '/' + path
# Keep only image files
pc = list(im_paths)
for p in pc:
if not p.split('.')[-1] in [ 'jpg', 'JPG', 'png', 'PNG' ]:
im_paths.remove( p )
if not im_paths:
return 1
# Run project process
p = mp.Process( target=displayImages, args=( im_paths, ) )
p.start()
# Keep displaying images for 1 minute
time.sleep(5)
p.terminate()
return 0
if __name__ == '__main__':
controller( './pics/' )
exit(0)
Pic class继承自QLabel,以便定义timerEvent方法。使用计时器事件,我们可以通知 qt 应用程序何时更新显示的图像。
另外,请注意,多处理模块的使用似乎有点矫枉过正。有人可能会直接调用 displayImages 函数。