最終更新日: 2019-12-14 土 21:00
ホーム | 文書トップ | 目次

basicapp3 Pythonで画像ファイルをドラッグ&ドロップして表示するひな形

Table of Contents

概要

Pythonで複数テキストファイルをドラッグ&ドロップして何かに変換するひな形です。

GUIの為にPyQt5を使っています。

画像表示の為にcv2(opencv-python)を使っています。

ファイルをドラッグ&ドロップすると、別ウィンドウで画像を表示します。

スクリーンショット

PythonBasicApp3.png

ソースコード

basicapp3.py

# coding: utf-8
import os,sys,re
import fileproc,imageproc
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit, QSlider

class BasicApp3(QWidget):
    """
    """
    def __init__(self):
        super(BasicApp3, self).__init__()
        self.setAcceptDrops(True)
        self.initUi()

    def fileProc( self, filename ):
        self.textEdit.append("inputfile: " + filename)

        name, ext = os.path.splitext( os.path.basename(filename) )
        outputfilename = os.path.join( os.path.dirname(filename), name + "_convert" + ext )
        self.textEdit.append("outputfile: " + outputfilename)
        self.textEdit.append("\n")

        self.myImage = imageproc.MyImage()
        self.myImage.openfile(filename)
        self.textEdit.append("height=")
        self.textEdit.append(str(self.myImage.height))
        self.textEdit.append("width=")
        self.textEdit.append(str(self.myImage.width))
        self.textEdit.append("scale=")
        self.textEdit.append(str(self.myImage.scale))
        self.textImageScale.setText(str(self.myImage.imageScale))
        self.myImage.show(self.mouseEvent)

    def dragEnterEvent( self, event ):
        if event.mimeData().hasUrls():  event.accept()
        else:                           event.ignore()

    def dropEvent( self, event ):
        files = [u.toLocalFile() for u in event.mimeData().urls()]
        for f in files:
            self.fileProc(f)

    def doPlus(self, value):
        self.myImage.sizePlus()
        self.textImageScale.setText(str(self.myImage.imageScale))

    def doMinus(self, value):
        self.myImage.sizeMinus()
        self.textImageScale.setText(str(self.myImage.imageScale))

    def doCenter(self, value):
        self.myImage.sizeSet(1)
        self.slider.setValue(10);
        self.textImageScale.setText(str(self.myImage.imageScale))

    def sliderMoved(self, value):
        self.myImage.sizeSet(value/100)
        self.textImageScale.setText(str(self.myImage.imageScale))

    def mouseEvent(self, event, x, y, flags, param ):
        """
        """
        self.textEdit.append("pos=")
        self.textEdit.append(str(x))
        self.textEdit.append(str(y))

        pixelValue = self.myImage.getRGB(x,y)
        self.textEdit.append("R,G,B=")
        self.textEdit.append(str(pixelValue[2]))
        self.textEdit.append(str(pixelValue[1]))
        self.textEdit.append(str(pixelValue[0]))

    def doClear(self, value):
        self.textEdit.clear()

    def initUi(self):
        self.textImageScale = QTextEdit(self)
        self.textImageScale.resize(70,20)
        self.textImageScale.move(10,220)
        self.textImageScale.setReadOnly(1)

        self.btnPlus = QPushButton(u'+',self)
        self.btnPlus.resize(20,20)
        self.btnPlus.move(80,220)
        self.btnPlus.clicked.connect(self.doPlus)

        self.btnCenter = QPushButton(u'.',self)
        self.btnCenter.resize(20,20)
        self.btnCenter.move(100,220)
        self.btnCenter.clicked.connect(self.doCenter)

        self.btnMinus = QPushButton(u'-',self)
        self.btnMinus.resize(20,20)
        self.btnMinus.move(120,220)
        self.btnMinus.clicked.connect(self.doMinus)

        self.btnClear = QPushButton(u'clear',self)
        self.btnClear.move(200,220)
        self.btnClear.clicked.connect(self.doClear)

        self.textEdit = QTextEdit(self)
        self.textEdit.resize(500,200)
        self.textEdit.setReadOnly(1)

        self.slider = QSlider(1,self)
        self.slider.setTickInterval(1);
        self.slider.setMaximum(1000);
        self.slider.setValue(10);
        self.slider.move(10,250)
        self.slider.sliderMoved.connect(self.sliderMoved)

        self.show()

def main():
    """
    """
    app = QApplication(sys.argv)

    w = BasicApp3()
    w.setWindowTitle('BasicApp3')
    w.resize(500, 300)
    w.move(0, 0)

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

fileproc.py

def FileProc(inputFileName,outputFileName):
    file = open(inputFileName)

    file_content = file.read()

    outputfile = open(outputFileName,'w')
    outputfile.write(file_content)
    outputfile.close()

imageproc.py

# coding: utf-8
import numpy as np
import cv2

class MyImage:
    """
    """

    def __init__(self):
        self.imageScale = 1;

    def openfile(self,inputFileName):
        screen_res = 2560, 1440

        self.img = cv2.imread(inputFileName,cv2.IMREAD_UNCHANGED)
        self.scale = min((screen_res[0] / self.img.shape[1]), (screen_res[1] / self.img.shape[0]))
        self.windowHeight = int(self.img.shape[0]*self.scale)
        self.windowWidth  = int(self.img.shape[1]*self.scale)

        self.height = int(self.img.shape[0])
        self.width  = int(self.img.shape[1])

    def show(self,func):

        cv2.namedWindow('img', cv2.WINDOW_AUTOSIZE)
        cv2.setMouseCallback('img', func)
        cv2.resizeWindow('img',self.windowWidth,self.windowHeight)

        self.displayImg = cv2.resize(self.img,dsize=None,fx=self.imageScale,fy=self.imageScale, interpolation = cv2.INTER_NEAREST  )
        cv2.imshow('img',self.displayImg)

    def sizePlus(self):
        self.imageScale += 0.01
        self.displayImg = cv2.resize(self.img,dsize=None,fx=self.imageScale,fy=self.imageScale)
        cv2.imshow('img',self.displayImg)

    def sizeMinus(self):
        self.imageScale -= 0.01
        self.displayImg = cv2.resize(self.img,dsize=None,fx=self.imageScale,fy=self.imageScale)
        cv2.imshow('img',self.displayImg)

    def sizeSet(self,value):
        self.imageScale = max(value,0.01)
        self.displayImg = cv2.resize(self.img,dsize=None,fx=self.imageScale,fy=self.imageScale)
        cv2.imshow('img',self.displayImg)

    def getRGB(self,x,y):
        return self.img[y,x]

参考

ホーム | 文書トップ | 目次
Created by Emacs 27.1 (Org mode 9.3)