# -*- coding: utf-8 -*-
__author__ = 'linfuyuan'
import base_window
import wx
import os
import my_process as mp
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import platform
import codecs
import shutil
class MyDialog(base_window.MyDialog1):
def __init__(self, parent):
base_window.MyDialog1.__init__(self, parent)
self.my_process_dlg = mp.MyProcess(None, self.onCancel)
self.is_cancel = False
self.env = platform.system()
def on_choose_dir(self, event):
file_wildcard = "*"
dlg = wx.FileDialog(self, u"请选择图片目录",
os.getcwd(),
style=wx.OPEN | wx.MULTIPLE,
wildcard=file_wildcard)
if dlg.ShowModal() == wx.ID_OK:
file_names = dlg.GetPaths()
for fn in file_names:
self.m_listBox1.Append(fn)
# self.pic_gallery.Add(wx.StaticText(self, -1, fn), 0, wx.ALIGN_LEFT)
dlg.Destroy()
pass
def on_png_to_webp(self, event):
items = self.m_listBox1.GetItems()
if len(items) > 0 and self.lib_exist():
command = 'lib_mac%simg2webp' % os.sep
if self.env == 'Linux':
command = 'lib_linux%simg2webp' % os.sep
elif self.env == 'Windows':
command = 'lib_windows%simg2webp.exe' % os.sep
self.my_process_dlg.m_staticText3.SetLabel(u'处理中')
self.my_process_dlg.m_button5.SetLabel(u'取消')
self.is_cancel = False
self.my_process_dlg.m_gauge1.SetRange(len(items))
self.my_process_dlg.m_gauge1.SetValue(0)
self.my_process_dlg.Show()
count = 0
dest_dir = '%s%shappy_webp' % (os.getcwd(), os.sep)
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
tmp_dir = '%s%stmp' % (os.getcwd(), os.sep)
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
for item in items:
if not self.is_cancel:
filename = item[item.rindex(os.sep):]
shutil.copy(item, '%s%s' % (tmp_dir, os.sep))
os.system(
'%s %s%s%s -o %s%s%s.webp' % (
command, tmp_dir, os.sep, filename, dest_dir, os.sep,
item[item.rindex(os.sep) + 1:item.rindex('.')]))
os.remove('%s%s%s' % (tmp_dir, os.sep, filename))
count += 1
self.my_process_dlg.m_gauge1.SetValue(count)
else:
break
self.my_process_dlg.m_staticText3.SetLabel(u"输出路径:%s" % dest_dir)
self.my_process_dlg.m_button5.SetLabel(u'完成')
def on_webp_to_animate(self, event):
items = self.m_listBox1.GetItems()
if len(items) > 0 and self.lib_exist():
command = 'lib_mac%swebpmux' % os.sep
if self.env == 'Linux':
command = 'lib_linux%swebpmux' % os.sep
elif self.env == 'Windows':
command = 'lib_windows%swebpmux' % os.sep
self.my_process_dlg.m_staticText3.SetLabel(u'处理中')
self.my_process_dlg.m_button5.SetLabel(u'取消')
self.is_cancel = False
self.my_process_dlg.m_gauge1.SetRange(len(items))
self.my_process_dlg.m_gauge1.SetValue(0)
self.my_process_dlg.Show()
dest_dir = '%s%shappy_animate_webp' % (os.getcwd(), os.sep)
gap = int(self.m_textCtrl1.GetValue())
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
tmp_dir = '%s%stmp' % (os.getcwd(), os.sep)
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
tmp_files = []
for item in items:
filename = item[item.rindex(os.sep):]
shutil.copy(item, '%s%s' % (tmp_dir, os.sep))
tmp_files.append('%s%s%s' % (tmp_dir, os.sep, filename))
# webpmux -frame 1.webp +100 -frame 2.webp +100+50+50 -frame 3.webp +100+50+50+1+b -loop 10 -bgcolor 255,255,255,255 -o anim_container.webp
os.system(
'%s %s -o %s%sanimate.webp' % (command,
' '.join(['-frame %s +%d+0+0+1+b' % (x, gap) for x in tmp_files]),
dest_dir,
os.sep))
for tf in tmp_files:
os.remove(tf)
self.my_process_dlg.m_gauge1.SetValue(len(items))
self.my_process_dlg.m_staticText3.SetLabel(u"输出路径:%s" % dest_dir)
self.my_process_dlg.m_button5.SetLabel(u'完成')
def onClear(self, event):
self.m_listBox1.Clear()
def onCancel(self):
self.is_cancel = True
self.my_process_dlg.Close()
def lib_exist(self):
if self.env == 'Darwin':
if not os.path.exists('lib_mac'):
dlg = wx.MessageDialog(None,
u"请到http://download.csdn.net/detail/xanxus46/9794013 下载并解压到%s下" % os.getcwd(),
u"lib_mac/webp类库找不到")
dlg.ShowModal()
dlg.Destroy()
return False
else:
return True
elif self.env == 'Linux':
if not os.path.exists('lib_linux'):
dlg = wx.MessageDialog(None,
u"请到http://download.csdn.net/detail/xanxus46/9793858 下载并解压到%s下" % os.getcwd(),
u"lib_linux/webp类库找不到"
)
dlg.ShowModal()
dlg.Destroy()
return False
else:
return True
elif self.env == 'Windows':
if not os.path.exists('lib_windows'):
dlg = wx.MessageDialog(None,
u"请到http://download.csdn.net/detail/xanxus46/9794046 下载并解压到%s下" % os.getcwd(),
u"lib_windows\\webp类库找不到")
dlg.ShowModal()
dlg.Destroy()
return False
else:
return True
else:
return True