图片格式转换 png,jpg,gif等格式 的python可视化程序
一:前提
python实现图片的各种格式转换,支持较多格式(png、jpg、gif、bmp、im、pcx、ppm)的可视化界面
二:代码
直接上代码!
import os
from PIL import Image
import shutil
import sys
import PySimpleGUI as sg
import tkinter as tk
from tkinter import filedialog
def image2png(dataset_dir,type):
files = []
image_list = os.listdir(dataset_dir)
files = [os.path.join(dataset_dir, _) for _ in image_list]
for index,jpg in enumerate(files):
if index > 100000:
break
try:
sys.stdout.write('\r>>Converting image %d/100000 ' % (index))
sys.stdout.flush()
im = Image.open(jpg)
png = os.path.splitext(jpg)[0] + "." + type
im.save(png)
'''
if jpg.split('.')[-1] == 'jpg':
shutil.move(png,output_dirLR)
else:
shutil.move(png,output_dirHR)
'''
shutil.move(png, dataset_dir)
except IOError as e:
print('could not read:',jpg)
print('error:',e)
print('skip it\n')
sys.stdout.write('Convert Over!\n')
sys.stdout.flush()
def transforSeparateFile(dataset_dir,type):
try:
im = Image.open(dataset_dir)
Targetim = os.path.splitext(dataset_dir)[0] + "." + type
im.save(Targetim)
except IOError as e:
print('could not read:',dataset_dir)
print('error:',e)
print('skip it\n')
print("Converting "+dataset_dir+" 成功的转为"+type+"格式")
def getLocalFile():
root=tk.Tk()
root.withdraw()
filePath=filedialog.askopenfilename().replace('/','\\')
return filePath
def getLocalFolder(values):
root=tk.Tk()
root.withdraw()
FolderPath=filedialog.askdirectory().replace('/','\\')
return FolderPath
def startTransfor(values):
Location=values['keyMainWindowPlaintextTagretToBeTransfor']
type=values['keyMainWindowTargetFormat']
if os.path.splitext(Location)[1] == "":
image2png(os.path.splitext(Location)[0],type)
else:
transforSeparateFile(Location,type)
def main():
try:
column1 = [
[sg.Text(' 1',background_color='#CCCCCC',text_color='#000000')],
[sg.Button(' 选择转换的图片文件 ', key='keyMainWindowFileChoose',button_color='#CD5C5C')],
[sg.Button('选择转换的图片文件目录', key='keyMainWindowFolderChoose',button_color='#CD5C5C'),
sg.Text('< =整个文件内的图片都会生成其对选择类型的图片,若已存在目标同名的文件会被覆盖')],
]
column2 = [
[sg.Text(' 2',background_color='#CCCCCC',text_color='#000000')],
[sg.Text(' 选择的图片文件或者目录如下 ',background_color='#696969')],
[sg.Input(key='keyMainWindowPlaintextTagretToBeTransfor', size=(90, 1))],
[sg.Text(' ',background_color='#CCCCCC')],
]
column3 = [
[sg.Text(' 3',background_color='#CCCCCC',text_color='#000000')],
[sg.Text(' 选择目标格式 ',background_color='#696969')],
[sg.InputCombo(['png', 'jpg','gif','bmp','im','pcx','ppm'],key='keyMainWindowTargetFormat',size=(50, 3))],
[sg.Text(' ',background_color='#CCCCCC',text_color='#000000')],
[sg.Text(' ',background_color='#CCCCCC',text_color='#000000')],
[sg.Text(' ',background_color='#CCCCCC',text_color='#000000')],
[sg.Button(' 开始转换 ', key='keyMainWindowTransforStart',button_color='#4682B4')],
]
mainWindowLayout = [
[sg.Text(' ')],
[sg.Column(column1, background_color='#CCCCCC',size=(700,120),)],
[sg.Text(' ')],
[sg.Column(column2, background_color='#CCCCCC',size=(700,110),)],
[sg.Text(' ')],
[sg.Column(column3, background_color='#CCCCCC',size=(700,200),)],
]
mainWindow = sg.Window('图片格式转换', layout=mainWindowLayout, finalize=True)
while True:
window, event, values = sg.read_all_windows()
print("点击了主窗口")
if window == mainWindow:
if event in (sg.WIN_CLOSED, 'keyMainWindowOk', 'keyMainWindowCancel'):
break
elif event == 'keyMainWindowFileChoose':
filePath = getLocalFile();
mainWindow['keyMainWindowPlaintextTagretToBeTransfor'].update(filePath)
elif event == 'keyMainWindowFolderChoose':
folderPath = getLocalFolder(values);
mainWindow['keyMainWindowPlaintextTagretToBeTransfor'].update(folderPath)
elif event == 'keyMainWindowTransforStart':
Location=values['keyMainWindowTargetFormat']
print(Location)
startTransfor(values)
mainWindow.close()
except Exception as result:
print("函数 main 捕捉到异常:%s" % result)
if __name__ == "__main__":
current_dir = os.getcwd()
print(current_dir)
main()
最后的运行效果如下(比较简陋)
|