Skip to content

Instantly share code, notes, and snippets.

@omiq
Created June 11, 2018 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omiq/413fde6cd3af551d277006d032cddcfe to your computer and use it in GitHub Desktop.
Save omiq/413fde6cd3af551d277006d032cddcfe to your computer and use it in GitHub Desktop.
Image viewer desktop app in python
import os
import glob
import sys
import subprocess
from PySide2.QtWidgets import*
from PySide2.QtGui import*
from PySide2 import QtCore, QtGui
# initialise variables
index = 0
file = ""
application = QApplication(sys.argv)
# keys
def left_press():
global index
print("LEFT!")
index -= 1
load_pic()
def right_press():
global index, files
print("RIGHT!")
if index < (len(files)-1):
index += 1
load_pic()
# function to quit
def quit_app():
global application
print("Quit!")
application.exit()
# select in file manager
def select_file():
global file
if sys.platform == 'darwin':
subprocess.run(['open', '-R', file])
elif sys.platform == 'linux':
subprocess.Popen(['nautilus', file])
else:
command = 'C:\\windows\\explorer /select,"{}"'.format(file)
print(command)
subprocess.run([command])
print("{}: {}".format(sys.platform, file))
# rotate 90
def rotate_picture():
global window, file, files, index, label
file = files[index]
picture = label.pixmap()
transform = QTransform().rotate(90)
picture = picture.transformed(transform)
label.setPixmap(picture)
label.setGeometry(QtCore.QRect(10, 40, picture.width(), picture.height()))
window.resize(picture.width()+20, picture.height()+100)
window.setWindowTitle(file)
window.show()
# Create a Window
window = QWidget()
window.setWindowTitle("View Image")
window.grabKeyboard()
label = QLabel(window)
# buttons
button = QPushButton("Quit", window)
button.clicked.connect(quit_app)
button.setShortcut(QtGui.QKeySequence.Cancel)
left = QPushButton("< Prev", window)
left.clicked.connect(left_press)
left.setShortcut(QtGui.QKeySequence.MoveToPreviousChar)
left.move(110, 0)
right = QPushButton("Next >", window)
right.clicked.connect(right_press)
right.setShortcut(QtGui.QKeySequence.MoveToNextChar)
right.move(195, 0)
select = QPushButton("Select", window)
select.clicked.connect(select_file)
select.setShortcut(QtGui.QKeySequence.MoveToNextLine)
select.move(295, 0)
rotate = QPushButton("Rotate", window)
rotate.clicked.connect(rotate_picture)
rotate.setShortcut(QtGui.QKeySequence.MoveToPreviousLine)
rotate.move(380, 0)
# get the list of images
pic_dir = "/home/chrisg/Pictures/"
files = glob.glob(pic_dir + "*.jpg")
files += glob.glob(pic_dir + "*.png")
files += glob.glob(pic_dir + "*.gif")
files.sort(key=os.path.getmtime, reverse=True)
# load pictures
def load_pic():
global window, file, files, index, label
file = files[index]
print("{} {} ".format(index, file))
picture = QPixmap(file)
label.setPixmap(picture)
label.setGeometry(QtCore.QRect(10, 40, picture.width(), picture.height()))
window.resize(picture.width()+20, picture.height()+100)
window.setWindowTitle(file)
window.show()
# display the first picture
load_pic()
# wait for events
sys.exit(application.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment