mirror of
https://github.com/Kilian/Trimage.git
synced 2026-01-26 10:08:40 -05:00
Fix #28
This commit is contained in:
parent
a3f417c43e
commit
5f5757f575
1 changed files with 10 additions and 7 deletions
|
|
@ -4,6 +4,7 @@ import time
|
||||||
import sys
|
import sys
|
||||||
from os import listdir, path, remove, access, W_OK
|
from os import listdir, path, remove, access, W_OK
|
||||||
from shutil import copy
|
from shutil import copy
|
||||||
|
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
from multiprocessing import cpu_count
|
from multiprocessing import cpu_count
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
@ -304,7 +305,7 @@ class ImageRow:
|
||||||
"""Build the information visible in the table image row."""
|
"""Build the information visible in the table image row."""
|
||||||
self.image = image
|
self.image = image
|
||||||
d = {
|
d = {
|
||||||
'shortname': lambda i: self.statusStr() % i.shortname,
|
'filename_w_ext': lambda i: self.statusStr() % i.filename_w_ext,
|
||||||
'oldfilesizestr': lambda i: human_readable_size(i.oldfilesize)
|
'oldfilesizestr': lambda i: human_readable_size(i.oldfilesize)
|
||||||
if i.compressed else "",
|
if i.compressed else "",
|
||||||
'newfilesizestr': lambda i: human_readable_size(i.newfilesize)
|
'newfilesizestr': lambda i: human_readable_size(i.newfilesize)
|
||||||
|
|
@ -315,7 +316,7 @@ class ImageRow:
|
||||||
'icon': lambda i: i.icon if i.compressed else waitingIcon,
|
'icon': lambda i: i.icon if i.compressed else waitingIcon,
|
||||||
'fullpath': lambda i: i.fullpath, #only used by cli
|
'fullpath': lambda i: i.fullpath, #only used by cli
|
||||||
}
|
}
|
||||||
names = ['shortname', 'oldfilesizestr', 'newfilesizestr',
|
names = ['filename_w_ext', 'oldfilesizestr', 'newfilesizestr',
|
||||||
'ratiostr', 'icon']
|
'ratiostr', 'icon']
|
||||||
for i, n in enumerate(names):
|
for i, n in enumerate(names):
|
||||||
d[i] = d[n]
|
d[i] = d[n]
|
||||||
|
|
@ -345,13 +346,14 @@ class Image:
|
||||||
self.valid = False
|
self.valid = False
|
||||||
self.reset()
|
self.reset()
|
||||||
self.fullpath = fullpath
|
self.fullpath = fullpath
|
||||||
|
self.filename_w_ext = path.basename(self.fullpath)
|
||||||
|
self.filename, self.filetype = path.splitext(self.filename_w_ext)
|
||||||
if path.isfile(self.fullpath) and access(self.fullpath, W_OK):
|
if path.isfile(self.fullpath) and access(self.fullpath, W_OK):
|
||||||
self.filetype = path.splitext(self.fullpath)[1][1:].lower()
|
self.filetype = self.filetype[1:].lower()
|
||||||
if self.filetype == "jpg":
|
if self.filetype == "jpg":
|
||||||
self.filetype = "jpeg"
|
self.filetype = "jpeg"
|
||||||
if self.filetype in ["jpeg", "png"]:
|
if self.filetype in ["jpeg", "png"]:
|
||||||
oldfile = QFileInfo(self.fullpath)
|
oldfile = QFileInfo(self.fullpath)
|
||||||
self.shortname = oldfile.fileName()
|
|
||||||
self.oldfilesize = oldfile.size()
|
self.oldfilesize = oldfile.size()
|
||||||
self.icon = QIcon(self.fullpath)
|
self.icon = QIcon(self.fullpath)
|
||||||
self.valid = True
|
self.valid = True
|
||||||
|
|
@ -375,7 +377,8 @@ class Image:
|
||||||
"png": "optipng" + exe + " -force -o7 '%(file)s'&&advpng" + exe + " -z4 '%(file)s' && pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time '%(file)s' '%(file)s.bak' && mv '%(file)s.bak' '%(file)s'"
|
"png": "optipng" + exe + " -force -o7 '%(file)s'&&advpng" + exe + " -z4 '%(file)s' && pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time '%(file)s' '%(file)s.bak' && mv '%(file)s.bak' '%(file)s'"
|
||||||
}
|
}
|
||||||
# create a backup file
|
# create a backup file
|
||||||
copy(self.fullpath, self.fullpath + '~')
|
backupfullpath = '/tmp/' + self.filename_w_ext
|
||||||
|
copy(self.fullpath, backupfullpath)
|
||||||
try:
|
try:
|
||||||
retcode = call(runString[self.filetype] % {"file": self.fullpath},
|
retcode = call(runString[self.filetype] % {"file": self.fullpath},
|
||||||
shell=True, stdout=PIPE)
|
shell=True, stdout=PIPE)
|
||||||
|
|
@ -387,11 +390,11 @@ class Image:
|
||||||
|
|
||||||
# checks the new file and copy the backup
|
# checks the new file and copy the backup
|
||||||
if self.newfilesize >= self.oldfilesize:
|
if self.newfilesize >= self.oldfilesize:
|
||||||
copy(self.fullpath + '~', self.fullpath)
|
copy(backupfullpath, self.fullpath)
|
||||||
self.newfilesize = self.oldfilesize
|
self.newfilesize = self.oldfilesize
|
||||||
|
|
||||||
# removes the backup file
|
# removes the backup file
|
||||||
remove(self.fullpath + '~')
|
remove(backupfullpath)
|
||||||
else:
|
else:
|
||||||
self.failed = True
|
self.failed = True
|
||||||
self.compressing = False
|
self.compressing = False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue