Python Tkinter编写的一个小程序

最近在做机器学习相关的工作的时候,需要进行挑选图片数据,就顺手编写了一个很小的手动挑选图片的GUI工具,虽说最后也没有怎么用上,但是对于python tkinter编程的初学者还是有一定的参考价值的。

  • 主要需求

对选择的路径的图片进行显示,并由操作者决定对这张图片的分类,点击相应的button,后台讲图片移到相应的文件夹中,然后显示下一张进行同样的工作。

程序的截图如下图所示: 选择工作路径后

  • 代码环境:python 3.6.2,

  • 所有代码如下:

from  tkinter import *
from  tkinter.filedialog import askdirectory

from PIL import Image, ImageTk

import glob
import os

import shutil

imglist = []
current_img = ''
next_img = ''
current_img_file = None
next_img_file = None
normal_dir = ''
normal_dark_dir = ''
abandon_dir = ''
other_dir = ''
img_label = ''
root = Tk()
root.title('Image Selector')
workspace = StringVar()


def selectPath():
    path_ = askdirectory()
    workspace.set(path_)
    global imglist
    global current_img
    global next_img
    global normal_dir
    global normal_dark_dir
    global abandon_dir
    global other_dir
    global img_label
    global current_img
    global current_img_file
    global next_img_file
    normal_dir = path_ + '/' + 'normal'
    normal_dark_dir = path_ + '/' + 'normal_dark'
    abandon_dir = path_ + '/' + 'abandon'
    other_dir = path_ + '/' + 'other'
    current_img = ''
    imglist = []
    for file in glob.glob(path_ + '/*.jpg'):
        imglist.append(file)
    current_img = imglist.pop(0)
    next_img = imglist.pop(0)
    # create related diretories
    if not os.path.exists(normal_dir):
        os.makedirs(normal_dir)
    if not os.path.exists(normal_dark_dir):
        os.makedirs(normal_dark_dir)
    if not os.path.exists(abandon_dir):
        os.makedirs(abandon_dir)
    if not os.path.exists(other_dir):
        os.makedirs(other_dir)
    im = Image.open(current_img)
    current_img_file = ImageTk.PhotoImage(im)
    img_label.configure({'height': 720, 'width': 1280, 'image': current_img_file})


def loadNextImage():
    global img_label
    global current_img
    global next_img
    global current_img_file

    if next_img != '':
        current_img = next_img
        im = Image.open(current_img)
        current_img_file = ImageTk.PhotoImage(im)
        img_label.configure({'image': current_img_file, 'height': 720, 'width': 1280})
        if len(imglist) != 0:
            next_img = imglist.pop(0)
        else:
            pass
    else:
        print("null in the image")

    print('load image')


def normal():
    global normal_dir
    global current_img
    move_file(current_img, normal_dir)
    loadNextImage()


def normal_dark():
    global normal_dark_dir
    global current_img
    move_file(current_img, normal_dark_dir)
    loadNextImage()


def abandon():
    global abandon_dir
    global current_img
    move_file(current_img, abandon_dir)
    loadNextImage()


def move_file(file_path, destination):
    shutil.move(file_path, destination)


def other():
    global other_dir
    global current_img
    move_file(current_img, other_dir)
    loadNextImage()


Label(root, {"text": "Workspace:"}).grid({"row": 0, "column": 0})
Entry(root, {"textvariable": workspace}).grid({'row': 0, "column": 1})
Button(root, {"text": "View", "command": selectPath}).grid({"row": 0, "column": 2})
 

img_label = Label(root)
img_label.grid({'row': 1, "column": 0, 'columnspan': 4})

Button(root, {'text': 'normal', 'command': normal}).grid({'row': 2, 'column': 0})

Button(root, {'text': 'normal_dark', 'command': normal_dark}).grid({'row': 2, 'column': 1})

Button(root, {'text': 'abandon', 'command': abandon}).grid({'row': 2, 'column': 2})
Button(root, {'text': 'other', 'command': other}).grid({'row': 2, 'column': 3})

root.mainloop()

转载于:https://my.oschina.net/zhangwenwen/blog/1560230

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值