Skip to content

Instantly share code, notes, and snippets.

@afzafri
Last active December 8, 2016 11:01
Show Gist options
  • Save afzafri/4ecdaec376cf62f0ec3b212482fb409c to your computer and use it in GitHub Desktop.
Save afzafri/4ecdaec376cf62f0ec3b212482fb409c to your computer and use it in GitHub Desktop.
Python GUI Image Upload (Copy source file to destination directory). Require Python 2.7 + PySide QT Binding
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created: Thu Dec 08 03:07:21 2016
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
import os
import re
import shutil
class Ui_PhotoUploader(object):
def setupUi(self, PhotoUploader):
PhotoUploader.setObjectName("PhotoUploader")
PhotoUploader.resize(338, 511)
PhotoUploader.setMinimumSize(QtCore.QSize(338, 511))
PhotoUploader.setMaximumSize(QtCore.QSize(338, 511))
self.labelPic = QtGui.QLabel(PhotoUploader)
self.labelPic.setGeometry(QtCore.QRect(20, 150, 300, 300))
self.labelPic.setAcceptDrops(False)
self.labelPic.setAutoFillBackground(True)
self.labelPic.setFrameShape(QtGui.QFrame.Box)
self.labelPic.setText("")
self.labelPic.setObjectName("labelPic")
self.label_2 = QtGui.QLabel(PhotoUploader)
self.label_2.setGeometry(QtCore.QRect(20, 130, 46, 13))
self.label_2.setObjectName("label_2")
self.imagePath = QtGui.QLineEdit(PhotoUploader)
self.imagePath.setGeometry(QtCore.QRect(20, 40, 201, 20))
self.imagePath.setObjectName("imagePath")
self.browseBut = QtGui.QPushButton(PhotoUploader)
self.browseBut.setGeometry(QtCore.QRect(240, 40, 75, 23))
self.browseBut.setObjectName("browseBut")
self.label_3 = QtGui.QLabel(PhotoUploader)
self.label_3.setGeometry(QtCore.QRect(20, 20, 81, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtGui.QLabel(PhotoUploader)
self.label_4.setGeometry(QtCore.QRect(20, 80, 91, 16))
self.label_4.setObjectName("label_4")
self.savePathBut = QtGui.QPushButton(PhotoUploader)
self.savePathBut.setGeometry(QtCore.QRect(240, 100, 75, 23))
self.savePathBut.setObjectName("savePathBut")
self.savePath = QtGui.QLineEdit(PhotoUploader)
self.savePath.setGeometry(QtCore.QRect(20, 100, 201, 20))
self.savePath.setObjectName("savePath")
self.uploadBut = QtGui.QPushButton(PhotoUploader)
self.uploadBut.setGeometry(QtCore.QRect(130, 470, 75, 23))
self.uploadBut.setObjectName("uploadBut")
self.retranslateUi(PhotoUploader)
QtCore.QMetaObject.connectSlotsByName(PhotoUploader)
# "event listener", when a button clicked, call function selectFile
self.browseBut.clicked.connect(lambda:self.selectFile("choosePic"))
self.savePathBut.clicked.connect(lambda:self.selectFile("choosePath"))
self.uploadBut.clicked.connect(lambda:self.selectFile("uploadPic"))
# function for select file
def selectFile(self,type):
if type == "choosePic":
# open up file chooser, and get the path. "getOpenFileName" for getting file path
fileName = str(QtGui.QFileDialog.getOpenFileName(caption="Select Image", filter="Image (*.jpg *.jpeg *.png *.gif *.bmp)")) # set image file filter
# use regex to only get the file path
m = re.search('(\'(.+?)\')', fileName)
if m:
fileName = m.group(1)
# replace unnecessary "'" in the path
fileName = fileName.replace("'","")
# set path to output box and display image to label
self.imagePath.setText(fileName)
pixmap = QtGui.QPixmap(fileName) # create pixmap object
pixmap = pixmap.scaled(300,300) # resize image
self.labelPic.setPixmap(pixmap) # set the image to the label
if type == "choosePath":
# open up file chooser, and get the path. "getExistingDirectory" for getting directory path
pathSave = str(QtGui.QFileDialog.getExistingDirectory())
pathSave = pathSave.replace("\\","/")
# set save path to the output box
self.savePath.setText(pathSave)
if type == "uploadPic":
fileName = str(self.imagePath.text()) # get save path
pathSave = str(self.savePath.text()) # get image path
# validate form
if fileName != "" and pathSave != "":
# copy/upload the image
# use "copy2" if dont have to rename file, use "copyfile" if want to rename file (include new file name in the save path)
shutil.copy2(fileName, pathSave)
self.alertBox("File uploaded!") # call alert message function
# clear all field
self.savePath.setText("")
self.imagePath.setText("")
self.labelPic.setPixmap("")
else:
self.alertBox("Please choose image and save path before upload!")
# create message box function
def alertBox(self,msg):
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle("Alert");
msgBox.setText(msg);
msgBox.exec_();
def retranslateUi(self, PhotoUploader):
PhotoUploader.setWindowTitle(QtGui.QApplication.translate("PhotoUploader", "Photo Uploader", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("PhotoUploader", "Preview", None, QtGui.QApplication.UnicodeUTF8))
self.browseBut.setText(QtGui.QApplication.translate("PhotoUploader", "Browse", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("PhotoUploader", "Choose image:", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("PhotoUploader", "Choose save path:", None, QtGui.QApplication.UnicodeUTF8))
self.savePathBut.setText(QtGui.QApplication.translate("PhotoUploader", "Browse", None, QtGui.QApplication.UnicodeUTF8))
self.uploadBut.setText(QtGui.QApplication.translate("PhotoUploader", "Upload", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
PhotoUploader = QtGui.QDialog()
ui = Ui_PhotoUploader()
ui.setupUi(PhotoUploader)
PhotoUploader.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment