Last active
November 13, 2024 07:39
-
-
Save rptb1/5c1f16298f7da9912c595c3a90601c0b to your computer and use it in GitHub Desktop.
Pre-create thumbnails for e.g. Nautilus (Gnome Files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# make-thumbs -- pre-create thumbnails for e.g. Nautilus (Gnome Files) | |
# | |
# Usage: make-thumbs <directory>... | |
# | |
# Adapted from <https://askubuntu.com/a/201997/466189> to work on | |
# Ubuntu 22.04 by updating to Python 3 and fixing up errors. | |
# | |
# CC BY-SA <https://creativecommons.org/licenses/by-sa/3.0/> | |
import os | |
import sys | |
from gi import require_version as gi_require_version | |
gi_require_version('GnomeDesktop', '3.0') | |
from gi.repository import Gio, GnomeDesktop | |
def make_thumbnail(factory, filename): | |
mtime = os.path.getmtime(filename) | |
# Use Gio to determine the URI and mime type | |
f = Gio.file_new_for_path(filename) | |
uri = f.get_uri() | |
info = f.query_info( | |
'standard::content-type', Gio.FileQueryInfoFlags.NONE, None) | |
mime_type = info.get_content_type() | |
if factory.lookup(uri, mtime) is not None: | |
print("FRESH %s" % uri) | |
return False | |
if not (mime_type and factory.can_thumbnail(uri, mime_type, mtime)): | |
print("UNSUPPORTED %s" % uri) | |
return False | |
thumbnail = factory.generate_thumbnail(uri, mime_type) | |
if thumbnail is None: | |
print("ERROR %s" % uri) | |
return False | |
print("OK %s" % uri) | |
factory.save_thumbnail(thumbnail, uri, mtime) | |
return True | |
def thumbnail_folder(factory, folder): | |
for dirpath, dirnames, filenames in os.walk(folder): | |
for filename in filenames: | |
make_thumbnail(factory, os.path.join(dirpath, filename)) | |
def main(argv): | |
factory = GnomeDesktop.DesktopThumbnailFactory() | |
for filename in argv[1:]: | |
if os.path.isdir(filename): | |
thumbnail_folder(factory, filename) | |
else: | |
make_thumbnail(factory, filename) | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment