mirror of
https://github.com/Kilian/Trimage.git
synced 2026-01-26 01:58:41 -05:00
Small cleanup
This commit is contained in:
parent
2ed75030b2
commit
a1811a0a74
1 changed files with 50 additions and 50 deletions
100
trimage.py
100
trimage.py
|
|
@ -1,7 +1,7 @@
|
||||||
import sys
|
import sys
|
||||||
from os import system
|
from os import system
|
||||||
from os import listdir
|
from os import listdir
|
||||||
from os import path
|
import os.path
|
||||||
from subprocess import *
|
from subprocess import *
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
|
|
@ -78,11 +78,11 @@ class StartQT4(QMainWindow):
|
||||||
compress_file."""
|
compress_file."""
|
||||||
global showapp
|
global showapp
|
||||||
showapp = False
|
showapp = False
|
||||||
dirpath = path.abspath(path.dirname(directory))
|
dirpath = os.path.abspath(os.path.dirname(directory))
|
||||||
imagedir = listdir(directory)
|
imagedir = listdir(directory)
|
||||||
filelist = QStringList()
|
filelist = QStringList()
|
||||||
for image in imagedir:
|
for image in imagedir:
|
||||||
image = QString(path.join(dirpath, image))
|
image = QString(os.path.join(dirpath, image))
|
||||||
filelist.append(image)
|
filelist.append(image)
|
||||||
self.delegator(filelist)
|
self.delegator(filelist)
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ class StartQT4(QMainWindow):
|
||||||
"""Get the file and send it to compress_file"""
|
"""Get the file and send it to compress_file"""
|
||||||
global showapp
|
global showapp
|
||||||
showapp = False
|
showapp = False
|
||||||
image = path.abspath(image)
|
image = os.path.abspath(image)
|
||||||
filecmdlist = QStringList()
|
filecmdlist = QStringList()
|
||||||
filecmdlist.append(image)
|
filecmdlist.append(image)
|
||||||
self.delegator(filecmdlist)
|
self.delegator(filecmdlist)
|
||||||
|
|
@ -167,7 +167,7 @@ class StartQT4(QMainWindow):
|
||||||
"""
|
"""
|
||||||
def checkname(self, name):
|
def checkname(self, name):
|
||||||
"""Check if the file is a jpg or png."""
|
"""Check if the file is a jpg or png."""
|
||||||
if path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]:
|
if os.path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def enable_recompress(self):
|
def enable_recompress(self):
|
||||||
|
|
@ -217,14 +217,11 @@ class TriTableModel(QAbstractTableModel):
|
||||||
|
|
||||||
|
|
||||||
class Worker(QThread):
|
class Worker(QThread):
|
||||||
|
|
||||||
def __init__(self, parent = None):
|
def __init__(self, parent = None):
|
||||||
|
|
||||||
QThread.__init__(self, parent)
|
QThread.__init__(self, parent)
|
||||||
self.exiting = False
|
self.exiting = False
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
|
||||||
self.exiting = True
|
self.exiting = True
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
|
|
@ -236,57 +233,60 @@ class Worker(QThread):
|
||||||
"""Compress the given file, get data from it and call update_table."""
|
"""Compress the given file, get data from it and call update_table."""
|
||||||
for image in self.images:
|
for image in self.images:
|
||||||
#gather old file data
|
#gather old file data
|
||||||
filename = image[0]
|
filename = str(image[0])
|
||||||
print(filename)
|
|
||||||
icon = image[1]
|
icon = image[1]
|
||||||
oldfile = QFileInfo(filename)
|
oldfile = QFileInfo(filename)
|
||||||
name = oldfile.fileName()
|
name = oldfile.fileName()
|
||||||
oldfilesize = oldfile.size()
|
oldfilesize = oldfile.size()
|
||||||
oldfilesizestr = size(oldfilesize, system=alternative)
|
oldfilesizestr = size(oldfilesize, system=alternative)
|
||||||
|
|
||||||
|
# get extention
|
||||||
|
baseName, extention = os.path.splitext(filename)
|
||||||
|
|
||||||
#decide with tool to use
|
#decide with tool to use
|
||||||
if path.splitext(str(filename))[1].lower() in [".jpg", ".jpeg"]:
|
if extention in ['.jpg', '.jpeg']:
|
||||||
runstr = 'jpegoptim -f --strip-all "' + str(filename) + '"'
|
runString = 'jpegoptim -f --strip-all "%(file)s"'
|
||||||
try:
|
elif extention in ['.png']:
|
||||||
retcode = call(runstr, shell=True, stdout=PIPE)
|
runString = 'optipng -force -o7 "%(file)s"; advpng -z4 "%(file)"'
|
||||||
runfile = retcode
|
|
||||||
except OSError, e:
|
|
||||||
runfile = e
|
|
||||||
elif path.splitext(str(filename))[1].lower() in [".png"]:
|
|
||||||
runstr = ('optipng -force -o7 "' + str(filename)
|
|
||||||
+ '"; advpng -z4 "' + str(filename) + '"')
|
|
||||||
try:
|
|
||||||
retcode = call(runstr, shell=True, stdout=PIPE)
|
|
||||||
runfile = retcode
|
|
||||||
except OSError, e:
|
|
||||||
runfile = e
|
|
||||||
|
|
||||||
if runfile == 0:
|
|
||||||
#gather new file data
|
|
||||||
newfile = QFile(filename)
|
|
||||||
newfilesize = newfile.size()
|
|
||||||
newfilesizestr = size(newfilesize, system=alternative)
|
|
||||||
|
|
||||||
#calculate ratio and make a nice string
|
|
||||||
ratio = 100 - (float(newfilesize) / float(oldfilesize) * 100)
|
|
||||||
ratiostr = "%.1f%%" % ratio
|
|
||||||
|
|
||||||
# append current image to list
|
|
||||||
for i, image in enumerate(imagelist):
|
|
||||||
if image[4] == filename:
|
|
||||||
imagelist.remove(image)
|
|
||||||
imagelist.insert(i, (name, oldfilesizestr, newfilesizestr, ratiostr,
|
|
||||||
filename, icon))
|
|
||||||
self.emit(SIGNAL("updateUi"))
|
|
||||||
|
|
||||||
global showapp
|
|
||||||
if showapp != True:
|
|
||||||
# we work via the commandline
|
|
||||||
print("File:" + filename + ", Old Size:" + oldfilesizestr +
|
|
||||||
", New Size:" + newfilesizestr + ", Ratio:" + ratiostr)
|
|
||||||
else:
|
else:
|
||||||
|
# This probably should never happen...
|
||||||
|
raise Exception('File %s does not have the appropriate extention' % filename)
|
||||||
|
|
||||||
|
try:
|
||||||
|
retcode = call(runString % {'file' : filename, shell = True, stdout = PIPE)
|
||||||
|
runfile = retcode
|
||||||
|
except OSError, e:
|
||||||
|
runfile = e
|
||||||
|
|
||||||
|
if runfile != 0:
|
||||||
# TODO nice error recovery
|
# TODO nice error recovery
|
||||||
print("uh. not good")
|
raise Exception('Some error occurred!')
|
||||||
|
|
||||||
|
|
||||||
|
#gather new file data
|
||||||
|
newfile = QFile(filename)
|
||||||
|
newfilesize = newfile.size()
|
||||||
|
newfilesizestr = size(newfilesize, system=alternative)
|
||||||
|
|
||||||
|
#calculate ratio and make a nice string
|
||||||
|
ratio = 100 - (float(newfilesize) / float(oldfilesize) * 100)
|
||||||
|
ratiostr = "%.1f%%" % ratio
|
||||||
|
|
||||||
|
# append current image to list
|
||||||
|
for i, image in enumerate(imagelist):
|
||||||
|
if image[4] == filename:
|
||||||
|
imagelist.remove(image)
|
||||||
|
imagelist.insert(i, (name, oldfilesizestr, newfilesizestr, ratiostr,
|
||||||
|
filename, icon))
|
||||||
|
self.emit(SIGNAL("updateUi"))
|
||||||
|
|
||||||
|
global showapp
|
||||||
|
if not showapp:
|
||||||
|
# we work via the commandline
|
||||||
|
print("File:" + filename + ", Old Size:" + oldfilesizestr +
|
||||||
|
", New Size:" + newfilesizestr + ", Ratio:" + ratiostr)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue