如何在KDE / Dolphin上提前生成缩略图?

重温这个关于在Nautilus / GNOME上预生成缩略图的问答我最近不得不发现那里发布的任何脚本都不适用于Dolphin文件管理器中的KDE Plasma 5 /。

有没有办法可以提前在KDE上生成缩略图,而无需手动浏览每个文件夹?

介绍

事实certificate,KDE和GNOME现在遵循略有不同的缩略图命名和元数据约定。 这是非常不幸的,因为这些问题应该被自由标准组织的工作所取消。

我已经向KDE提交了一个错误报告,希望最终能够解决这个问题,但是现在由KDE和GNOME生成的缩略图很难相互兼容。

用于桥接KDE / GNOME差距的Thumbnailer脚本

为了解决这种不兼容问题,我最终修改了James Henstridge在上面链接的Q&A中发布的Python脚本 。 我实现的主要更改是更新生成的缩略图以便KDE识别的function(通过重命名和更新PNG元数据块)。

以下是其当前版本中的上述脚本:

#!/usr/bin/python3 """ Thumbnail generator for KDE/GNOME Largely based on a script by James Henstridge (https://askubuntu.com/a/201997) Unfortunately there seems to be some disagreement between GNOME and KDE towards how to follow the XDG specs for saving thumbnails. This script is meant as a workaround to that issue, generating thumbnails that follow both specifications. Dependencies: python3 gir1.2-gnomedesktop-3.0 python-pillow pillow can be installed with `pip install pillow` You will also need to have the corresponding thumbnailers installed (eg evince-thumbnailer). KDE thumbnailers are not supported. All previews are generated through GNOME's thumbnail factory and then made compatible with KDE. Further references: Thumbnail specifications in KDE/GNOME: - https://bugs.kde.org/show_bug.cgi?id=393015 - https://api.kde.org/frameworks/kio/html/previewjob_8cpp_source.html - https://lazka.github.io/pgi-docs/GnomeDesktop-3.0/classes/DesktopThumbnailFactory.html Setting PNG metadata: - http://ubuntu.miximages.com/kde/PIL.htmlImagePlugin.PngInfo - https://stackoverflow.com/a/10552742/1708932 Copyright: (c) 2012 James Henstridge  (c) 2018 Glutanimate  License: MIT license """ import os import sys from hashlib import md5 from PIL import Image from PIL import PngImagePlugin import gi gi.require_version('GnomeDesktop', '3.0') from gi.repository import Gio, GnomeDesktop # FIXME: Hardcoding the Thumbnailer to a generic name # regardless of MIME type might not always work KDE_THUMBNAILER = "KDE Thumbnail Generator" def update_name_and_meta(thumb_path, filename, mtime, mime_type, size): print("Making thumb compatible with KDE...") abs_path = os.path.abspath(filename) # The spaces in our URI are not escaped. This is not in accordance # with the URI RFC2396 which is listed in the freedesktop specs, # but it's what KDE currently uses # (cf.: https://bugs.kde.org/show_bug.cgi?id=393015) kde_uri = "file://" + abs_path kde_md5 = md5(kde_uri.encode("utf-8")).hexdigest() thumb_dir = os.path.dirname(thumb_path) kde_thumb_path = os.path.join(thumb_dir, kde_md5 + ".png") if os.path.exists(kde_thumb_path): print("KDE thumb already exists. Skipping") return im = Image.open(thumb_path) # Set PNG metadata chunk meta = PngImagePlugin.PngInfo() meta.add_itxt("Software", KDE_THUMBNAILER) meta.add_text("Thumb::MTime", str(int(mtime))) meta.add_text("Thumb::Mimetype", mime_type) meta.add_text("Thumb::Size", str(size)) meta.add_itxt("Thumb::URI", kde_uri) im.save(kde_thumb_path, "png", pnginfo=meta) # uncomment this to remove GNOME thumbnails: # os.remove(thumb_path) 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() size = info.get_size() if factory.lookup(uri, mtime) is not None: print("FRESH %s" % uri) return False if not 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 factory.save_thumbnail(thumbnail, uri, mtime) thumb_path = factory.lookup(uri, mtime) update_name_and_meta(thumb_path, filename, mtime, mime_type, size) print("OK %s" % uri) 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)) 

安装

将上面的代码部分复制并粘贴到新文件中,为其选择一个拟合名称(例如thumbnailer ),并将其标记为可执行文件。

依赖

为了使脚本正常工作,您需要安装GNOME的python绑定。 该脚本还依赖于Python的pillow库,可以通过pip安装。

以下命令应该处理所有依赖项:

 sudo apt install gir1.2-gnomedesktop-3.0 python3-pip pip3 install pillow 

缩略图首先通过GNOME的缩略图工厂生成,然后与KDE兼容。 所以你仍然需要安装所有相应的GNOME thumbnailer模块。 不支持KDE自己的缩略图。 例如:对于支持生成PDF缩略图的脚本,您必须安装evince

(我本来喜欢直接使用KDE的python绑定,但看起来pykde4和pykde5已经被放弃多年了)。

用法

一般用法与任何其他缩略图脚本相同。 只需使用要生成缩略图作为参数的文件或文件夹调用它,例如:

 thumbnailer /home/Documents 

参考

KDE / GNOME中的缩略图规范:

设置PNG元数据: