Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thumbnail.save configuration option. #660

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Added:
the original image. Thanks `@Yutsuten`_ for reviving this!
* Add the ``none`` sorting type for the ``sort.image_order`` and ``sort.directory_order``
options, implemented by `@buzzingwires`_
* Add the ``thumbnail.save`` option, implemented by `@buzzingwires`_

Changed:
^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_read_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
UPDATED_CONFIG = {
"SORT": {"shuffle": "True"},
"IMAGE": {"overzoom": "4.2"},
"THUMBNAIL": {"size": "64"},
"THUMBNAIL": {"size": "64", "save": "False"},
buzzingwires marked this conversation as resolved.
Show resolved Hide resolved
}


Expand All @@ -46,7 +46,7 @@
UPDATED_CONFIG_INVALID = {
"SORT": {"shuffle": "not a bool"},
"IMAGE": {"overzoom": "not a float"},
"THUMBNAIL": {"size": "not an int"},
"THUMBNAIL": {"size": "not an int", "save": "not a bool"},
buzzingwires marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down Expand Up @@ -86,6 +86,7 @@ def test_read_config(configpath):
assert api.settings.sort.shuffle.value is True
assert api.settings.image.overzoom.value == 4.2
assert api.settings.thumbnail.size.value == 64
assert api.settings.thumbnail.save.value is False
buzzingwires marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/utils/test_thumbnail_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import pytest

from vimiv.api import settings
from vimiv.utils import thumbnail_manager


Expand All @@ -24,6 +25,28 @@ def manager(qtbot, tmp_path, mocker):
yield thumbnail_manager.ThumbnailManager(None)


def test_thumbnail_save_disabled(monkeypatch, qtbot, tmp_path, manager):
monkeypatch.setattr(settings.thumbnail.save, "value", False)
no_thumbnail_path = str(tmp_path / "no_thumbnail.jpg")
QPixmap(300, 300).save(no_thumbnail_path, "jpg")
manager.create_thumbnails_async([no_thumbnail_path])
check_thumbails_created(qtbot, manager, 0)


def test_thumbnail_save_disabled_no_delete_old(monkeypatch, qtbot, tmp_path, manager):
monkeypatch.setattr(settings.thumbnail.save, "value", True)
has_thumbnail_path = str(tmp_path / "has_thumbnail.jpg")
QPixmap(300, 300).save(has_thumbnail_path, "jpg")
manager.create_thumbnails_async([has_thumbnail_path])
check_thumbails_created(qtbot, manager, 1)

monkeypatch.setattr(settings.thumbnail.save, "value", False)
no_thumbnail_path = str(tmp_path / "no_thumbnail.jpg")
QPixmap(300, 300).save(no_thumbnail_path, "jpg")
manager.create_thumbnails_async([has_thumbnail_path, no_thumbnail_path])
check_thumbails_created(qtbot, manager, 1)
karlch marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize("n_paths", (1, 5))
def test_create_n_thumbnails(qtbot, tmp_path, manager, n_paths):
# Create images to create thumbnails of
Expand Down
3 changes: 3 additions & 0 deletions vimiv/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ class thumbnail: # pylint: disable=invalid-name
"""Namespace for thumbnail related settings."""

size = ThumbnailSizeSetting("thumbnail.size", 128, desc="Size of thumbnails")
save = BoolSetting(
"thumbnail.save", True, desc="Save new thumbnails to disk for later use"
karlch marked this conversation as resolved.
Show resolved Hide resolved
)


class slideshow: # pylint: disable=invalid-name
Expand Down
29 changes: 21 additions & 8 deletions vimiv/utils/thumbnail_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from PyQt5.QtGui import QIcon, QPixmap, QImage

import vimiv
from vimiv import api
from vimiv.utils import xdg, imagereader, Pool


Expand Down Expand Up @@ -131,6 +132,24 @@ def _get_thumbnail_filename(self, path: str) -> str:
def _get_source_mtime(path: str) -> int:
return int(os.path.getmtime(path))

def _save_thumbnail(self, image: QImage, thumbnail_path: str) -> None:
"""Save the thumbnail file to the disk.

Args:
image: The QImage representing the thumbnail.
thumbnail_path: Path to which the thumbnail is stored.
Returns:
None.
"""
# First create temporary file and then move it. This avoids
# problems with concurrent access of the thumbnail cache, since
# "move" is an atomic operation
handle, tmp_filename = tempfile.mkstemp(dir=self._manager.directory)
os.close(handle)
os.chmod(tmp_filename, 0o600)
image.save(tmp_filename, format="png")
os.replace(tmp_filename, thumbnail_path)

def _create_thumbnail(self, path: str, thumbnail_path: str) -> QPixmap:
"""Create thumbnail for an image.

Expand All @@ -153,14 +172,8 @@ def _create_thumbnail(self, path: str, thumbnail_path: str) -> QPixmap:
return self._manager.fail_pixmap
for key, value in attributes.items():
image.setText(key, value)
# First create temporary file and then move it. This avoids
# problems with concurrent access of the thumbnail cache, since
# "move" is an atomic operation
handle, tmp_filename = tempfile.mkstemp(dir=self._manager.directory)
os.close(handle)
os.chmod(tmp_filename, 0o600)
image.save(tmp_filename, format="png")
os.replace(tmp_filename, thumbnail_path)
if api.settings.thumbnail.save:
self._save_thumbnail(image, thumbnail_path)
return QPixmap(image)

def _get_thumbnail_attributes(self, path: str, image: QImage) -> Dict[str, str]:
Expand Down