[enh] cache images that are auto-oriented by gm

This commit is contained in:
Laurent Peuch 2016-02-18 08:29:08 +01:00
parent b4ddd0a0b6
commit 01bf77249d
2 changed files with 25 additions and 2 deletions

View File

@ -36,9 +36,26 @@ class Cache(object):
return False return False
def image_needs_to_be_oritend(self, source, target, command):
if not os.path.exists(target):
return True
if target not in self.cache:
return True
cached_image = self.cache[target]
if cached_image["size"] != os.path.getsize(source) or cached_image["command"] != command:
return True
return False
def cache_thumbnail(self, source, target, image): def cache_thumbnail(self, source, target, image):
self.cache[target] = {"size": os.path.getsize(source), "options": image.options} self.cache[target] = {"size": os.path.getsize(source), "options": image.options}
def cache_auto_oriented_image(self, source, target, command):
self.cache[target] = {"size": os.path.getsize(source), "command": command}
def __del__(self): def __del__(self):
self.json.dump(self.cache, open(self.cache_file_path, "w")) self.json.dump(self.cache, open(self.cache_file_path, "w"))

View File

@ -55,10 +55,16 @@ class Image(object):
if not self.autoorient: if not self.autoorient:
shutil.copyfile(source, target) shutil.copyfile(source, target)
print source, "->", target print source, "->", target
else: return ""
command = "gm convert %s -strip -auto-orient %s" % (source, target)
command = "gm convert %s -strip -auto-orient %s" % (source, target)
if CACHE.image_needs_to_be_oritend(source, target, command):
print command print command
os.system(command) os.system(command)
CACHE.cache_auto_oriented_image(source, target, command)
else:
print "skipped %s since it's already generated (based on source unchanged size and images options)" % target
return "" return ""