Python3.4 tkinter,PIL图片转换


Posted in Python onJune 21, 2018

先给大家分享一下全部代码

import os
from PIL import Image
import tkinter
import tkinter.filedialog
import tkinter.messagebox

class Window():
  def __init__(self):
    self.root = root = tkinter.Tk()
    self.menu = tkinter.Menu(root)
    self.submenu = tkinter.Menu(self.menu, tearoff=0)
    self.submenu.add_command(label='作者: 王小涛同?W')
    root.config(menu=self.submenu)
    self.Image = tkinter.StringVar()
    self.Image.set('.bmp')
    self.mstatus = tkinter.IntVar()
    self.fstatus = tkinter.IntVar()
    self.mstatus.set(0)
    self.fstatus.set(0)
    self.status = tkinter.StringVar()
    self.label = tkinter.Label(root, text='输入百分比')
    self.label.place(x=5, y=5)
    self.entryNew = tkinter.Entry(root)
    self.entryNew.place(x=70, y=5)
    self.checkM = tkinter.Checkbutton(self.root, text='批量转换', command=self.OnCheckM, variable=self.mstatus, onvalue=1, offvalue=0)
    self.checkM.place(x=5, y=30)
    self.label = tkinter.Label(root, text='选择文件')
    self.label.place(x=5, y=55)
    self.entryFile = tkinter.Entry(root)
    self.entryFile.place(x=70, y=55)
    self.BrowserFileButton = tkinter.Button(root, text='浏览', command=self.BrowserFile)
    self.BrowserFileButton.place(x=220, y=55)
    self.label = tkinter.Label(root, text='选择目录')
    self.label.place(x=5, y=90)
    self.entryDir = tkinter.Entry(root, state=tkinter.DISABLED)
    self.entryDir.place(x=70, y=90)
    self.BrowserDirButton = tkinter.Button(root, text='浏览', command=self.BrowserDir, state=tkinter.DISABLED)
    self.BrowserDirButton.place(x=220, y=90)

    self.checkF = tkinter.Checkbutton(root, text='改变文件格式', onvalue=1, offvalue=0, variable=self.fstatus, command=self.OnCheckF)
    self.checkF.place(x=5, y=120)


    frame = tkinter.Frame(root, )
    frame.place(x=10, y=150)
    self.rBmp = tkinter.Radiobutton(frame, variable=self.Image, value='.bmp', text='BMP', state=tkinter.DISABLED)
    self.rBmp.pack(anchor='w')
    self.rJpg = tkinter.Radiobutton(frame, variable=self.Image, value='.jpg', text='JPG', state=tkinter.DISABLED)
    self.rJpg.pack(anchor='w')
    self.rPng = tkinter.Radiobutton(frame, variable=self.Image, value='.png', text='PNG', state=tkinter.DISABLED)
    self.rPng.pack(anchor='w')
    self.rGif = tkinter.Radiobutton(frame, variable=self.Image, value='.gif', text='GIF', state=tkinter.DISABLED)
    self.rGif.pack(anchor='w')
    self.ButtonCov = tkinter.Button(root, text='转换格式', command=self.Conv, )
    self.ButtonCov.place(x=120, y=180)
    self.statusLabel = tkinter.Label(root, textvariable=self.status, fg='red')
    self.statusLabel.place(x=80, y=220)




  def OnCheckM(self):
    if not self.mstatus.get():
      self.entryDir.config(state=tkinter.DISABLED)
      self.entryFile.config(state=tkinter.NORMAL)
      self.BrowserFileButton.config(state=tkinter.NORMAL)
      self.BrowserDirButton.config(state=tkinter.DISABLED)
    else:
      self.entryDir.config(state=tkinter.NORMAL)
      self.entryFile.config(state=tkinter.DISABLED)
      self.BrowserFileButton.config(state=tkinter.DISABLED)
      self.BrowserDirButton.config(state=tkinter.NORMAL)


  def OnCheckF(self):
    if not self.fstatus.get():
      self.rBmp.config(state=tkinter.DISABLED)
      self.rPng.config(state=tkinter.DISABLED)
      self.rJpg.config(state=tkinter.DISABLED)
      self.rGif.config(state=tkinter.DISABLED)
    else:
      self.rBmp.config(state=tkinter.NORMAL)
      self.rPng.config(state=tkinter.NORMAL)
      self.rJpg.config(state=tkinter.NORMAL)
      self.rGif.config(state=tkinter.NORMAL)


  def BrowserFile(self):
    file = tkinter.filedialog.askopenfilename(title='Python player', filetypes=[('JPG', '*.jpg'), ('BMP', '*.bmp'), ('GIF', '*.gif'), ('PNG', '*.png')])
    if file:
      self.entryFile.delete(0, tkinter.END)
      self.entryFile.insert(tkinter.END, file)

  def BrowserDir(self):
    directory = tkinter.filedialog.askdirectory(title='Python')
    if directory:
      self.entryDir.delete(0, tkinter.END)
      self.entryDir.insert(tkinter.END, directory)


  def make(self, file, format=None):
    im = Image.open(file)
    mode = im.mode
    if mode not in('L', 'RGB'):
      im = im.convert('RGB')
    width, height = im.size
    s = self.entryNew.get()
    if s == '':
      tkinter.messagebox.showerror('出错啦', '请输入百分比')
      return
    else:
      n = int(s)
    nwidth = int(width*n/100)
    nheight = int(height*n/100)
    thumb = im.resize((nwidth, nheight), Image.ANTIALIAS)
    if format:
      thumb.save(file[:(len(file)-4)] + '_thumb' + format)
    else:
      thumb.save(file[:(len(file)-4)] + '_thumb' + file[-4:])


  def Conv(self):
    n = 0
    if self.mstatus.get():
      path = self.entryDir.get()
      if path == "":
        tkinter.messagebox.showerror('出错啦', '请选择路径')
        return
      filenames = os.listdir(path)
      if self.fstatus.get():
        f = self.Image.get()
        print(f)
        for filename in filenames:
          if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'):
            self.make(path+'/'+filename, f)
            n += 1
      else:
        for filename in filenames:
          if filename[-3:] in ('bmp', 'jpg', 'gif', 'png'):
            self.make(path+'/'+filename)
            n += 1
    else:
      file = self.entryFile.get()
      if file == '':
        tkinter.messagebox.showerror('出错啦', '请选择文件')
        return
      if self.fstatus.get():
        f = self.Image.get()
        self.make(file, f)
        n += 1
      else:
        self.make(file)
        n += 1
    self.status.set('成功转换 %d 张图片' % n)





  def mainloop(self):
    self.root.minsize(280, 270)
    self.root.maxsize(280, 250)
    self.root.title('图片转换')
    self.root.mainloop()



if __name__ == "__main__":
  window = Window()
  window.mainloop()

运行后的实际效果如下

Python3.4 tkinter,PIL图片转换

感谢大家对三水点靠木的支持,欢迎提出宝贵意见。

Python 相关文章推荐
Python模拟百度登录实例详解
Jan 20 Python
Python中关于Sequence切片的下标问题详解
Jun 15 Python
Python2.7 实现引入自己写的类方法
Apr 29 Python
python利用7z批量解压rar的实现
Aug 07 Python
Python如何使用函数做字典的值
Nov 30 Python
Python3 pickle对象串行化代码实例解析
Mar 23 Python
Python正则表达式如何匹配中文
May 27 Python
keras 回调函数Callbacks 断点ModelCheckpoint教程
Jun 18 Python
Python之Matplotlib文字与注释的使用方法
Jun 18 Python
Python如何批量生成和调用变量
Nov 21 Python
python制作抽奖程序代码详解
Jan 15 Python
Python编程编写完善的命令行工具
Sep 15 Python
Python3实现转换Image图片格式
Jun 21 #Python
python3实现域名查询和whois查询功能
Jun 21 #Python
解决python写入mysql中datetime类型遇到的问题
Jun 21 #Python
详解Python下ftp上传文件linux服务器
Jun 21 #Python
Python爬取数据并写入MySQL数据库的实例
Jun 21 #Python
python实现黑客字幕雨效果
Jun 21 #Python
python实现内存监控系统
Mar 07 #Python
You might like
PHP脚本的10个技巧(2)
2006/10/09 PHP
用sql命令修改数据表中的一个字段为非空(not null)的语句
2010/06/04 PHP
PHP 第二节 数据类型之字符串类型
2012/04/28 PHP
Yii2基于Ajax自动获取表单数据的方法
2016/08/10 PHP
PHP实现文件上传下载实例
2016/10/18 PHP
在你的网页中嵌入外部网页的方法
2007/04/02 Javascript
JS实现一个按钮的方法
2015/02/05 Javascript
js中split和replace的用法实例
2015/02/28 Javascript
jQuery中animate动画第二次点击事件没反应
2015/05/07 Javascript
详解nodejs微信公众号开发——1.接入微信公众号
2017/04/10 NodeJs
vue实现todolist单页面应用
2017/04/11 Javascript
jQuery使用正则验证15/18身份证的方法示例
2017/04/27 jQuery
Bootstrap table使用方法汇总
2017/11/17 Javascript
详解小程序云开发数据库
2019/05/20 Javascript
亲自动手实现vue日历控件
2019/06/26 Javascript
javascript 构建模块化开发过程解析
2019/09/11 Javascript
Vue 中使用富文本编译器wangEditor3的方法
2019/09/26 Javascript
[03:28]2014DOTA2国际邀请赛 EG战队官方纪录片
2014/07/21 DOTA
python中常用检测字符串相关函数汇总
2015/04/15 Python
Python中用于检查英文字母大写的isupper()方法
2015/05/19 Python
python 生成器协程运算实例
2017/09/04 Python
Caffe均值文件mean.binaryproto转mean.npy的方法
2018/07/09 Python
20行python代码实现人脸识别
2019/05/05 Python
python使用百度文字识别功能方法详解
2019/07/23 Python
Python线上环境使用日志的及配置文件
2019/07/28 Python
Python 一键获取百度网盘提取码的方法
2019/08/01 Python
Python使用matplotlib绘制三维参数曲线操作示例
2019/09/10 Python
Tensorflow累加的实现案例
2020/02/05 Python
Tensorflow 1.0之后模型文件、权重数值的读取方式
2020/02/12 Python
浅析两列自适应布局的3种思路
2016/05/03 HTML / CSS
北美领先的牛仔品牌:Buffalo David Bitton
2017/05/22 全球购物
烹饪自我鉴定
2014/03/01 职场文书
大学生作弊检讨书
2014/09/11 职场文书
用人单位的规章制度,怎样制定才是有效的?
2019/07/09 职场文书
Python源码解析之List
2021/05/21 Python
解决MultipartFile.transferTo(dest) 报FileNotFoundExcep的问题
2021/07/01 Java/Android