做了图片格式转换的小工具,下面列出:
第一个是无界面的只能转换当前目录下的图片
第二个是有界面的,可以任意选取路径
1. 配置文件信息conf.ini
2. 配置文件处理EnvConf.py
3. 界面MyWindow.py
4. Build application(setup.py)
第三种方法
import wx
import socket
import time
import os
import random
import serial
import threading
import datetime
from PIL import Image
#coding=utf-8
class MainFrame(wx.Frame):
def __init__(self,parent):
self.title="Image Format Conversion Tool"
wx.Frame.__init__(self, parent, -1, self.title, size=(400, 400))
panel = wx.Panel(self, -1)
self.labelWelcome=wx.StaticText(panel,-1,u"Image Configration",pos=(10,10))
self.MyFont_Welcome(self.labelWelcome)
self.typelist=[u'.jpg',u'.bmp',u'.png',u'.jif']
#Source Path
self.Source_Path = wx.TextCtrl(panel, -1, "")
self.MyFont_Text(self.Source_Path)
Button_Open=wx.Button(panel, -1, "Open")
self.MyFont_Button(Button_Open)
self.Bind(wx.EVT_BUTTON,self.OpenSourcePath, Button_Open)
Source_Path_Label=wx.StaticBox(panel,-1,"Source Directory")
self.MyFont_Title(Source_Path_Label)
host_sizer = wx.StaticBoxSizer(Source_Path_Label, wx.HORIZONTAL)
host_sizer.Add(Button_Open, 0, wx.ALIGN_LEFT)
host_sizer.Add(self.Source_Path, 1, wx.EXPAND|wx.LEFT, 10)
#Source Type
self.Source_Type=wx.ComboBox(panel,-1,u'.jpg',choices=self.typelist)
self.MyFont_Text(self.Source_Type)
Source_Type_Label=wx.StaticBox(panel,-1,"Source Type")
self.MyFont_Title(Source_Type_Label)
Source_Type_sizer = wx.StaticBoxSizer(Source_Type_Label, wx.VERTICAL)
Source_Type_sizer.Add(self.Source_Type, 0, wx.ALIGN_CENTER)
#Target Path
Taget_Path_lable=wx.StaticBox(panel,-1,"Target Directory")
self.MyFont_Title(Taget_Path_lable)
self.Target_Path=wx.TextCtrl(panel, -1,'')
self.MyFont_Text(self.Target_Path)
Taget_Button=wx.Button(panel, -1, "Save")
self.MyFont_Button(Taget_Button)
self.Bind(wx.EVT_BUTTON,self.SavetargetPath, Taget_Button)
input_sizer = wx.StaticBoxSizer(Taget_Path_lable,wx.HORIZONTAL)
input_sizer.Add(Taget_Button, 0, wx.ALIGN_LEFT)
input_sizer.Add(self.Target_Path, 1, wx.EXPAND|wx.LEFT, 10)
#Target Type
self.Target_Type=wx.ComboBox(panel,-1,u'.jpg',choices=self.typelist)
self.MyFont_Text(self.Target_Type)
Target_Type_Label=wx.StaticBox(panel,-1,"Target Type")
self.MyFont_Title(Target_Type_Label)
Target_Type_sizer = wx.StaticBoxSizer(Target_Type_Label, wx.VERTICAL)
Target_Type_sizer.Add(self.Target_Type, 0, wx.ALIGN_CENTER)
#Conver
Conver_Button=wx.Button(panel, -1, "Start to conver")
self.MyFont_Button(Conver_Button)
self.Bind(wx.EVT_BUTTON,self.Start_Conver, Conver_Button)
#Hor
hbox = wx.BoxSizer(wx.VERTICAL)
hbox.Add(self.labelWelcome, 0, wx.CENTER|wx.TOP, 10)
hbox.Add(host_sizer, 0, wx.EXPAND|wx.ALL, 5)
hbox.Add(Source_Type_sizer, 0, wx.EXPAND|wx.ALL, 5)
hbox.Add(input_sizer, 0, wx.EXPAND|wx.ALL,5)
hbox.Add(Target_Type_sizer, 0, wx.EXPAND|wx.ALL, 5)
hbox.Add(Conver_Button, 0, wx.ALIGN_CENTER, 5)
panel.SetSizer(hbox)
def MyFont_Welcome(self,text):
font=wx.Font(15,wx.ROMAN,wx.NORMAL,wx.NORMAL)
text.SetForegroundColour("orange")
text.SetFont(font)
def MyFont_Title(self,text):
font=wx.Font(13,wx.ROMAN,wx.NORMAL,wx.NORMAL)
text.SetForegroundColour("dark blue")
text.SetFont(font)
def MyFont_Text(self,text):
font=wx.Font(13,wx.ROMAN,wx.NORMAL,wx.NORMAL)
text.SetForegroundColour("aqua")
text.SetFont(font)
def MyFont_label(self,text):
font=wx.Font(13,wx.ROMAN,wx.NORMAL,wx.NORMAL)
text.SetForegroundColour("automatical")
text.SetFont(font)
def MyFont_Button(self,text):
font=wx.Font(12,wx.ROMAN,wx.NORMAL,wx.NORMAL)
text.SetForegroundColour("blue")
text.SetFont(font)
def OpenSourcePath(self,event):
dialog=wx.DirDialog(self,wx.DirSelectorPromptStr,"",wx.DD_CHANGE_DIR)
dialog.ShowModal()
dialog.Destroy()
self.Source_Path.Value=dialog.GetPath()
def SavetargetPath(self,event):
dialog=wx.DirDialog(self,wx.DirSelectorPromptStr,"",wx.DD_CHANGE_DIR)
dialog.ShowModal()
dialog.Destroy()
self.Target_Path.Value=dialog.GetPath()
def getImageFiles(self):
Source_Path=r'%s'%self.Source_Path.GetValue()
allfiles = os.listdir(Source_Path)
files = []
for f in allfiles:
if f[-4:].lower()==self.Source_Type.GetValue():
files.append(f)
return files
def Start_Conver(self,event):
filename=self.getImageFiles()
Source_Path=r'%s/'%self.Source_Path.GetValue()
Target_Path=r'%s/'%self.Target_Path.GetValue()
for i in filename:
x=i[:-4]
Image.open(Source_Path+i).save(Target_Path+x+self.Target_Type.GetValue())
if __name__=="__main__":
app=wx.App()
frame = MainFrame(None)
frame.Show(True)
app.MainLoop()