服务器之家

服务器之家 > 正文

python利用tkinter实现图片格式转换的示例

时间:2020-09-29 00:26     来源/作者:君莫笑丶丶

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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='作者:君莫笑')
    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()

运行效果:

python利用tkinter实现图片格式转换的示例

以上就是python利用tkinter实现图片格式转换的示例的详细内容,更多关于python 图片格式转换的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/manbaout/p/13232404.html

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部