Now one can choose to override picture quality for thumbnails
To use it, change this : - type: pictures-group images: - - image1.jpg - image2.jpg - - image3.jpg - image4.jpg To this : - type: pictures-group images: - - name: image1.jpg quality: 80 - image2.jpg - - image3.jpg - name: image4.jpg quality: 100 Next step would be to compress pictures instead of just copying them to reduce their size Signed-off-by: CapsLock <faimaison@legeox.net>
This commit is contained in:
parent
dca0f3623a
commit
0ab845eada
@ -12,6 +12,15 @@ templates = Environment(loader=FileSystemLoader([os.path.realpath(os.path.join(o
|
|||||||
index_template = templates.get_template("index.html")
|
index_template = templates.get_template("index.html")
|
||||||
gallery_index_template = templates.get_template("gallery-index.html")
|
gallery_index_template = templates.get_template("gallery-index.html")
|
||||||
|
|
||||||
|
DEFAULT_GM_QUALITY = 75
|
||||||
|
|
||||||
|
class CacheKeys(object):
|
||||||
|
SIZE = "size"
|
||||||
|
GM_QUALITY = "gm_quality"
|
||||||
|
|
||||||
|
class ImageAttributes(object):
|
||||||
|
NAME = "name"
|
||||||
|
QUALITY = "quality"
|
||||||
|
|
||||||
class Cache(object):
|
class Cache(object):
|
||||||
cache_file_path = os.path.join(os.getcwd(), ".prosopopee_cache")
|
cache_file_path = os.path.join(os.getcwd(), ".prosopopee_cache")
|
||||||
@ -22,20 +31,25 @@ class Cache(object):
|
|||||||
else:
|
else:
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
|
||||||
def thumbnail_needs_to_be_generated(self, source, target):
|
def thumbnail_needs_to_be_generated(self, source, target, gm_quality):
|
||||||
if not os.path.exists(target):
|
if not os.path.exists(target):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if target not in self.cache:
|
if target not in self.cache:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if self.cache[target] != os.path.getsize(source):
|
cache_data = self.cache[target]
|
||||||
|
|
||||||
|
if cache_data[CacheKeys.SIZE] != os.path.getsize(source) or cache_data[CacheKeys.GM_QUALITY] != gm_quality:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def cache_thumbnail(self, source, target):
|
def cache_thumbnail(self, source, target, gm_quality):
|
||||||
self.cache[target] = os.path.getsize(source)
|
cache_data = {}
|
||||||
|
cache_data[CacheKeys.SIZE] = os.path.getsize(source)
|
||||||
|
cache_data[CacheKeys.GM_QUALITY] = gm_quality
|
||||||
|
self.cache[target] = cache_data
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
json.dump(self.cache, open(self.cache_file_path, "w"))
|
json.dump(self.cache, open(self.cache_file_path, "w"))
|
||||||
@ -48,33 +62,46 @@ class TemplateFunctions():
|
|||||||
self.base_dir = base_dir
|
self.base_dir = base_dir
|
||||||
self.target_dir = target_dir
|
self.target_dir = target_dir
|
||||||
|
|
||||||
|
def get_image_name(self, image):
|
||||||
|
if ImageAttributes.NAME not in image:
|
||||||
|
return image
|
||||||
|
|
||||||
|
return image[ImageAttributes.NAME]
|
||||||
|
|
||||||
def copy_image(self, image):
|
def copy_image(self, image):
|
||||||
source, target = os.path.join(self.base_dir, image), os.path.join(self.target_dir, image)
|
image_name = self.get_image_name(image)
|
||||||
|
source, target = os.path.join(self.base_dir, image_name), os.path.join(self.target_dir, image_name)
|
||||||
|
|
||||||
# XXX doing this DOESN'T improve perf at all (or something like 0.1%)
|
# XXX doing this DOESN'T improve perf at all (or something like 0.1%)
|
||||||
# if os.path.exists(target) and os.path.getsize(source) == os.path.getsize(target):
|
# if os.path.exists(target) and os.path.getsize(source) == os.path.getsize(target):
|
||||||
# print "Skiped %s since the file hasn't been modified based on file size" % source
|
# print "Skiped %s since the file hasn't been modified based on file size" % source
|
||||||
# return ""
|
# return ""
|
||||||
|
|
||||||
shutil.copyfile(source, target)
|
shutil.copyfile(source, target)
|
||||||
|
|
||||||
print source, "->", target
|
print source, "->", target
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def generate_thumbnail(self, image, gm_geometry, gm_quality=75):
|
def generate_thumbnail(self, image, gm_geometry):
|
||||||
thumbnail_name = image.split(".")
|
image_name = self.get_image_name(image)
|
||||||
|
thumbnail_name = image_name.split(".")
|
||||||
thumbnail_name[-2] += "-small"
|
thumbnail_name[-2] += "-small"
|
||||||
thumbnail_name = ".".join(thumbnail_name)
|
thumbnail_name = ".".join(thumbnail_name)
|
||||||
|
|
||||||
source, target = os.path.join(self.base_dir, image), os.path.join(self.target_dir, thumbnail_name)
|
if ImageAttributes.QUALITY not in image:
|
||||||
|
gm_quality = DEFAULT_GM_QUALITY
|
||||||
|
else:
|
||||||
|
gm_quality = image[ImageAttributes.QUALITY]
|
||||||
|
|
||||||
if CACHE.thumbnail_needs_to_be_generated(source, target):
|
source, target = os.path.join(self.base_dir, image_name), os.path.join(self.target_dir, thumbnail_name)
|
||||||
command = "gm convert %s -resize %s -quality %s" % (source, gm_geometry, gm_quality, target)
|
|
||||||
|
if CACHE.thumbnail_needs_to_be_generated(source, target, gm_quality):
|
||||||
|
command = "gm convert %s -resize %s -quality %s %s" % (source, gm_geometry, gm_quality, target)
|
||||||
print command
|
print command
|
||||||
os.system(command)
|
os.system(command)
|
||||||
|
|
||||||
CACHE.cache_thumbnail(source, target)
|
CACHE.cache_thumbnail(source, target, gm_quality)
|
||||||
else:
|
else:
|
||||||
print "skiped %s since it's already generated (based on source unchanged size)" % target
|
print "skiped %s since it's already generated (based on source unchanged size and thumbnail quality set in your gallery's settings.yaml)" % target
|
||||||
|
|
||||||
return thumbnail_name
|
return thumbnail_name
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{{ helpers.copy_image(section.image) }}
|
{{ helpers.copy_image(section.image) }}
|
||||||
<section class="bordered-picture baguette">
|
<section class="bordered-picture baguette">
|
||||||
<a href="{{ section.image }}">
|
<a href="{{ helpers.get_image_name(section.image) }}">
|
||||||
<img src="{{ section.image }}">
|
<img src="{{ helpers.get_image_name(section.image) }}">
|
||||||
</a>
|
</a>
|
||||||
</section>
|
</section>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{{ helpers.copy_image(section.image) }}
|
{{ helpers.copy_image(section.image) }}
|
||||||
<section class="full-picture" style="background: transparent url('{{ section.image }}') no-repeat scroll center top / cover;">
|
<section class="full-picture" style="background: transparent url('{{ helpers.get_image_name(section.image) }}') no-repeat scroll center top / cover;">
|
||||||
{% if section.text %}
|
{% if section.text %}
|
||||||
<div class="picture-text">
|
<div class="picture-text">
|
||||||
<div class="picture-text-column">
|
<div class="picture-text-column">
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
{% for image in line %}
|
{% for image in line %}
|
||||||
{{ helpers.copy_image(image) }}
|
{{ helpers.copy_image(image) }}
|
||||||
<div class="picture">
|
<div class="picture">
|
||||||
<a href="{{ image }}">
|
<a href="{{ helpers.get_image_name(image) }}">
|
||||||
<img src="{{ helpers.generate_thumbnail(image, "x600") }}">
|
<img src="{{ helpers.generate_thumbnail(image, "x600") }}">
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user