Browse Source

Adding support for PDF thumbnails using optional dependency pdf2image

pull/536/head
Lucas Cimon 7 months ago
parent
commit
9d025c08dd
No known key found for this signature in database
GPG Key ID: 8DA831E717571EE
  1. 36
      src/sigal/plugins/nonmedia_files.py
  2. 58
      src/sigal/themes/colorbox/templates/album.html
  3. 22
      src/sigal/themes/photoswipe/templates/album_items.html

36
src/sigal/plugins/nonmedia_files.py

@ -26,6 +26,10 @@ Settings available as dictionary in ``nonmedia_files_options``:
import logging
import os
try: # Optional dependency:
from pdf2image import convert_from_path as pdf2img
except ImportError:
pdf2img = None
from PIL import Image as PILImage
from PIL import ImageDraw, ImageFont
from pilkit.utils import save_image
@ -102,9 +106,7 @@ def generate_thumbnail(
logger.info(f"kwargs: {kwargs}")
d.text(anchor, text, anchor="mm", **kwargs)
outformat = "JPEG"
logger.info("Save thumnail image: %s (%s)", outname, outformat)
save_image(img, outname, outformat, options=options, autoconvert=True)
save_image(img, outname, "JPEG", options=options, autoconvert=True)
def process_thumb(media):
@ -113,18 +115,22 @@ def process_thumb(media):
utils.copy(media.src_path, media.dst_path, symlink=settings["orig_link"])
if plugin_settings.get("ext_as_thumb", DEFAULT_CONFIG["ext_as_thumb"]):
logger.info("plugin_settings: %r", plugin_settings)
kwargs = {}
for key in ("bg_color", "font", "font_color", "font_size"):
if f"thumb_{key}" in plugin_settings:
kwargs[key] = plugin_settings[f"thumb_{key}"]
generate_thumbnail(
media.src_ext[1:].upper(),
media.thumb_path,
settings["thumb_size"],
options=settings["jpg_options"],
**kwargs,
)
if pdf2img and media.src_ext.lower() == ".pdf":
images = pdf2img(media.src_path, single_file=True, size=settings["thumb_size"])
images[0].save(media.thumb_path)
else:
kwargs = {}
for key in ("bg_color", "font", "font_color", "font_size"):
if f"thumb_{key}" in plugin_settings:
kwargs[key] = plugin_settings[f"thumb_{key}"]
generate_thumbnail(
media.src_ext[1:].upper(),
media.thumb_path,
settings["thumb_size"],
options=settings["jpg_options"],
**kwargs,
)
logger.info("Saved thumbnail image: %s", media.thumb_path)
def process_nonmedia(media):

58
src/sigal/themes/colorbox/templates/album.html

@ -16,47 +16,49 @@
{% include 'map.html' %}
<div id="gallery">
{% for media in album.medias | selectattr("type", "in", ("image", "video")) %}
{% for media in album.medias %}
{% if loop.index % nb_columns == 1 %}
<div id="albums" class="row">
<div id="albums" class="row">
{% endif%}
{% if media.type == "image" %}
{% if media.thumbnail %}
<div class="{{ column_size_t }} columns thumbnail">
{% if media.type == "image" %}
<a class="gallery" title="{{ media.title }}" {{ img_description(media) }}
{% if 'sigal.plugins.media_page' in settings.plugins %}
href="{{ media.url }}.html" data-href="{{ media.url }}"
{% else %}
href="{{ media.url }}"
{% endif %}
{% elif media.type == "video" %}
{% set mhash = media.url|replace('.', '')|replace(' ', '') %}
{% if 'sigal.plugins.media_page' in settings.plugins %}
<a href="{{ media.url}}.html" class="gallery"
title="{{ media.title }}"
data-href="{{ media.url }}" {{ img_description(media) }}>
<a href="{{ media.url }}.html" data-href="#{{ mhash }}"
{% else %}
<a href="{{ media.url }}" class="gallery" title="{{ media.title }}"
{{ img_description(media) }}>
<a href="#{{ mhash }}" class="gallery" inline='yes' title="{{ media.url }}"
{% endif %}
{% if media.big %} data-big="{{ media.big_url }}"{% endif %}
{% else %}
<a href="{{ media.url }}" title="{{ media.title }}"
{% endif %}
>
<img src="{{ media.thumbnail }}" alt="{{ media.url }}"
title="{{ media.title }}" /></a>
title="{{ media.title }}">
</a>
</div>
{% endif %}
{% if media.type == "video" %}
{% set mhash = media.url|replace('.', '')|replace(' ', '') %}
<div class="{{ column_size_t }} columns thumbnail">
{% if 'sigal.plugins.media_page' in settings.plugins %}
<a href="{{ media.url }}.html" data-href="#{{ mhash }}"
{% else %}
<a href="#{{ mhash }}" class="gallery" inline='yes' title="{{ media.url }}"
{% endif %}
{% if media.big %} data-big="{{ media.big_url }}"{% endif %}>
<img src="{{ media.thumbnail }}" alt="{{ media.url }}"
title="{{ media.title }}" /></a>
</div>
<!-- This contains the hidden content for the video -->
<div style='display:none'>
<div id="{{ mhash }}">
<video controls>
<source src='{{ media.url }}' type='{{ media.mime }}' />
</video>
</div>
<!-- This contains the hidden content for the video -->
<div style='display:none'>
<div id="{{ mhash }}">
<video controls>
<source src='{{ media.url }}' type='{{ media.mime }}' />
</video>
</div>
</div>
{% endif %}
{% if loop.last or loop.index % nb_columns == 0 %}
</div>
</div>
{% endif%}
{% endfor %}
</div>

22
src/sigal/themes/photoswipe/templates/album_items.html

@ -4,35 +4,27 @@
{% include 'map.html' %}
<div class="gallery">
{% for media in album.medias %}
{% if media.type == "image" %}
{% if media.thumbnail %}
<figure class="gallery__img--{{ "main" if loop.first else "secondary" }} thumbnail">
<a href="{{ media.url }}"
{% if media.type == "image" %}
data-pswp-width="{{media.size.width}}"
data-pswp-height="{{media.size.height}}"
{% if media.big %}
data-big="{{ media.big_url }}"
{%- endif -%}
>
<img src="{{ media.thumbnail }}" alt="{{ media.url }}" />
</a>
<div class="pswp-caption-content">
{{ img_description(media, with_big=False) }}
</div>
<figcaption>{{ media.title }} - {{ media.exif.datetime }}</figcaption>
</figure>
{% endif %}
{% if media.type == "video" %}
<figure class="gallery__img--secondary thumbnail" >
<a href="{{ media.url }}"
{% elif media.type == "video" %}
data-pswp-type="video"
data-pswp-width="800"
data-pswp-height="600">
data-pswp-height="600"
{% endif %}
>
<img src="{{ media.thumbnail }}" alt="{{ media.url }}" />
</a>
<div class="pswp-caption-content">
{{ img_description(media, with_big=False) }}
</div>
<figcaption>{{ media_title }}</figcaption>
<figcaption>{{ media.title }}{{ (" - " + media.exif.datetime) if media.exif else "" }}</figcaption>
</figure>
{% endif %}
{% endfor %}

Loading…
Cancel
Save