一、python小项目(指定抽取形式从一堆图像中抽取指定数量的图像)

#coding=utf-8
'''
时间:2020.8.2 20:35:56
作者: 小宇
功能:从海量文件里面抽取自己一定数量的文件用于统计,使用过程中会产生界面
使用函数库:tkinter、os、Image、time、random
声明:本源码仅限于交流学习,禁止任何人拿此源码用于商业用途
'''
import os
import tkinter as tk
from tkinter import *
from tkinter.filedialog import askdirectory
import os
import random
from PIL import Image
import time
#主窗口
main_win=tk.Tk()
main_win.title("欢迎使用小宇抽取系统")
main_win.geometry("400x250+380+300")
main_win.configure(bg="pale turquoise")
main_win.resizable(0,0)
#事件函数
def func_in():
file_name=askdirectory()
pathin.set(file_name)
def func_out():
file_name_out=askdirectory()
pathout.set(file_name_out)
def texty():
'''文件初始化及操作'''
path_in=tex1_.get()
path_out=tex12_.get()
path_in=path_in.replace('/','\\')
path_out=path_out.replace('/','\\')
num_get_=int(tex13_.get())
filelist = os.listdir(path_in)
filelist.sort(reverse=True)
cho_two=int(ButtonList.get())
'''抽取提示输出'''
button_state1 = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
button_state1.pack(fill='x',padx=38,pady=5)
button_state2 = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
button_state2.pack(fill='x',padx=38,pady=5)
button_state3 = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
button_state3.pack(fill='x',padx=38,pady=5)
L5 =tk.Label(button_state1,text='正在抽取...',font=("宋体",10),bg="pale turquoise")
L5.pack(side=LEFT)
if cho_two==0:
step_get=int(len(filelist)/num_get_)
for i in range(0,len(filelist),step_get):
img = Image.open(path_in+'\\'+filelist[i])
img.save(path_out+'\\'+filelist[i])
elif cho_two==1:
for i in random.sample(filelist,num_get_):
img = Image.open(path_in+'\\'+i)
img.save(path_out+'\\'+i)
else:
print("请重新输入:")
L6 =tk.Label(button_state2,text='抽取完成',font=("宋体",10),bg="pale turquoise")
L6.pack(side=LEFT)
L7 =tk.Label(button_state3,text='文件总数:'+str(len(filelist))+' 抽取数量:'+str(num_get_),font=("宋体",10),bg="pale turquoise")
L7.pack(side=LEFT)
#打开路径
pathout=StringVar()
pathin=StringVar()
pathin_frame = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
pathin_frame.pack(fill='x',padx=38,pady=5)
L1 =tk.Label(pathin_frame,text='读取路径: ',font=("宋体",12),bg="pale turquoise")
L1.pack(side=LEFT)
tex1_=Entry(pathin_frame,width=27,textvariable=pathin,bd=2)
tex1_.pack(side=LEFT)
button_=Button(pathin_frame,text="选择",command=func_in,bg="cyan")
button_.pack(side=RIGHT)
#存储路径
pathout_frame = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
pathout_frame.pack(fill='x',padx=38,pady=5)
L2 =tk.Label(pathout_frame,text='存储路径: ',font=("宋体",12),bg="pale turquoise")
L2.pack(side=LEFT)
tex12_=Entry(pathout_frame,width=27,textvariable=pathout,bd=2)
tex12_.pack(side=LEFT)
button2_=Button(pathout_frame,text="选择",command=func_out,bg="cyan")
button2_.pack(side=RIGHT)
#输入要抽取的数量
num_frame = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
num_frame.pack(fill='x',padx=38,pady=5)
L3 =tk.Label(num_frame,text='抽取数量: ',font=("宋体",12),bg="pale turquoise")
L3.pack(side=LEFT)
tex13_=tk.Entry(num_frame,width=4,bd=2)
tex13_.pack(side=LEFT)
#单选按钮及标签
L5 =tk.Label(num_frame,text=' 抽取方式:',font=("宋体",12),bg="pale turquoise")
L5.pack(side=LEFT)
ButtonList=tk.StringVar()
r1=Radiobutton(num_frame,variable=ButtonList,value=0,text="均匀",bg="pale turquoise")
r2=Radiobutton(num_frame,variable=ButtonList,value=1,text="随机",bg="pale turquoise")
ButtonList.set(0)
r2.pack(side=RIGHT)
r1.pack(side=RIGHT)
#抽取控件
button_frame = Frame(main_win,height=30,bd=0,relief="sunken",bg="pale turquoise")
button_frame.pack(fill='x',padx=38,pady=5)
button_start=Button(button_frame,text="抽取",command=texty,bg="cyan")
button_start.pack(side=RIGHT)
#退出控件
button_quit=Button(button_frame,text="退出",command=main_win.quit,bg="cyan")
button_quit.pack(side=RIGHT)
main_win.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小树苗m

您的打赏,是我的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值