Ubuntu – How to instruct Nautilus to pre-generate thumbnails

nautiluspdfscriptsthumbnails

I have a large library of PDF documents (papers, lectures, handouts) that I want to be able to quickly navigate through. For that I need thumbnails.

At the same time however, I see that the ~/.thumbnails folder is piling up with thumbs I don't really need. Deleting thumbnail junk without removing the important thumbs is impossible. If I were to delete them, I'd have to go to each and every folder with important PDF documents and let the thumbnail cache regenerate.

I would love to be able to automate this process. Is there any way I can tell nautilus to pre-cache the thumbs for a set of given directories?

Note: I did find a set of bash scripts that appear to do this for pictures and videos, but not for any other documents. Maybe someone more experienced with scripting might be able to adjust these for PDF documents or at least point me in the right direction on what I'd have to modify for this to work with PDF documents as well.


Edit:

The response to this question has been quite overwhelming. Let me first thank everyone who participated in solving this. The question, its answers and all the discussion around it are a great example of how the collaborative effort of many parties can lead to an optimal solution. This is exactly what makes Linux and Open Source so great.

All of the provided answers would deserve the bounty I originally put up for this question. Still, there's only one bounty to award. I owe it to all future readers to choose the answer that solves the problem in the most efficient way. To determine which solution that is, I did a final test run, comparing the three scripts in compatibility, speed and output quality. Here are the results:


Thumbnailer 1, by rosch:

Compatibility: ✔ spaces in file name ; ✔ spaces in directory name ; ✘ freedesktop compliant

Speed: 95 PDFs in 12,6 sec

Quality: stock nautilus quality

Additional Perks: 1.) automatically skips files with preexisting thumbs ; 2.) No additional packages needed

Thumbnailer 2, by Martin Orda:

Compatibility: ✔ spaces in file name ; ✔ spaces in directory name ; ✘ freedesktop compliant

Speed: 95 PDFs in 70,0 sec

Quality: significantly better scaling than the stock images.

Additional Perks: 1.) automatically skips files with preexisting thumbs 2.) compatible with a wide range of image formats besides PDF 3.) platform-independent, does not rely on GNOME-components

Thumbnailer 3, by James Henstridge:

Compatibility: ✔ spaces in file name ; ✔ spaces in directory name ; ✔ freedesktop compliant

Speed: 95 PDFs in 10,8 sec

Quality: stock nautilus quality

Additional Perks: 1.) automatically skips files with preexisting thumbs 2.) compatible with all file formats that are identified by the preinstalled thumbnailers


All three scripts are excellent. Each has its distinct set of advantages and disadvantages. Rosch's solution works out of the box and might be the right choice for users with a minimal installation.

Marcin Kaminski created a very versatile script that works with a variety of file formats and is DE-independent. It excels at thumbnail quality but does so at the expense of speed.

In the end it was James' solution that fit my use case best. It's fast, versatile and offers the options to skip over files with preexisting thumbnails.


Overall winner: James Henstridge


Additional information: All three scripts are fully compatible with nautilus-scripts. You can easily install them following this tutorial.


Edit 2: Updated review with improved script by rosch.

Best Answer

Nautilus's thumbnailing routines actually come from the libgnome-desktop library, so it is possible to run the same thumbnailers outside of the file manager.

The API is a little complex, but the following Python script should help:

#!/usr/bin/python
import os
import sys

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 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))

Save this to a file and mark it executable. You may also need to install the gir1.2-gnomedesktop-3.0 package if it is not already installed.

After that, simply invoke the script with the files or folders you want to thumbnail as arguments. Thumbnails will be saved to ~/.thumbnails where applications like Nautilus expect to find them.