2017-02-13 8 views
0

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および/またはopencvを含む回答が受け入れられるかもしれませんが、私は、まっすぐ進む道がQtを経由していると思います。

+2

ちょっと質問:あなたが表示するすべてのイメージに対して新しいQApplicationを起動し、単に「Pixmap」でイメージを更新するだけではなく、起動するのはなぜですか?初期化には時間がかかります。そして1つのイメージを表示している間に、新しい '' QPixmap''を作り、 '' QPixmap.swap''を使って '' x''秒後に切り替えるのですか? – alexblae

+0

こんにちは!私はQtを経験していないので、最適な解決策が何であるか想像できません。あなたの答えは素敵なアイデアのようです!それで、私はそれに取り組んでいます。 QPixmap.swapメソッドの使用例を教えてください。 – funk

答えて

1

は最後に、私は解決策を考え出しました。 qtアプリケーションを各画像更新時に再起動してはならないというアレクセレの考えが重要なポイントでした。しかし、まず... Qtフレームワークのアーキテクチャー、あるクラスが他のクラスを継承する方法、イベントの生成と処理方法についていくつか詳しく理解しなければなりませんでした。

他のユーザーが同様の問題に直面したときに、レビューして利点を得るために、更新されたコードを引用してください。

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クラスは、timerEventメソッドを定義するためにQLabelから継承します。タイマーイベントを使用して、表示された画像をいつ更新するかをqtアプリに通知することができます。

また、マルチプロセッシングモジュールの使用法は少し残酷すぎるようです。誰かがdisplayImages関数を直接呼び出すことができます。

+0

これはLinuxシステム固有のものですか? –

+1

いいえ、コードはLinuxシステムとWindowsシステムの両方で実行できます。唯一の依存関係はQtフレームワークです。 – funk

0

私はこれを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

+0

こんにちは!あなたの努力をありがとう!この解決策は、解決策がカバーすべき具体的な要件がないため、a)ウィンドウデコレータのないフルスクリーンモード、およびb)特定のモニタを選択する能力のためにうまく適合しない。 – funk

+0

うれしい私はいくつかの洞察力を提供することができます。 'cv2.namedWindow()'の機能をチェックしてください。役に立つかもしれません。 –

+0

あなたが1つに出会ったら、解決策を投稿してください:) –

関連する問題