How can I instruct Nautilus to pre-generate thumbnails?

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.


The script below should do the job. It uses evince-thumbnailer which - as far as I know - comes with every gnome installation and is the default thumbnailer.
Save as pdfthumbnailer.sh and make it executable.
Usage: pdfthumbnailer.sh dir1 [dir2, ...]

#!/bin/bash

F1=$HOME/.thumbnails/normal
F2=$HOME/.cache/thumbnails/normal
SAVE_FOLDER=$F1
[ -e $F2 ] && SAVE_FOLDER=$F2

# the thumbnailing function
evincethumb() {
    outname=$(echo -n "$(readlink -f "$0")" | \
    perl -MURI::file -MDigest::MD5=md5_hex -ne 'print md5_hex(URI::file->new($_));')
    # no work if thumbnail already present
    [ ! -e $SAVE_FOLDER/${outname}.png ] && {
        echo "$0"
        #uncomment only one of both thumbnailers
        #convert -thumbnail 128x128 "$0"[0] $SAVE_FOLDER/${outname}.png 2>/dev/null
        evince-thumbnailer -s 128 "$0" $SAVE_FOLDER/${outname}.png 2>/dev/null
    }
}

# make our function visible to the subshell in "find -exec" below
export -f evincethumb

# loop through all given folders
for folder in "$@" ; do
    find "$folder" -type f -exec bash -c evincethumb {} \;
done

Restriction:

  • does not add Thumb::URI and Thumb::MTime attributes to the thumbnails as pointed out by James Henstridge. So far I have seen no evidence that the default evince-thumbnailer is doing so. In other words..as long as nautilus does not regenerate the thumbnails the script can be used for the job.

Notes:

  • prints the name of the file when generating a new thumbnail, skips generation if existing
  • speed: 37 pdf files tested with both evince-thumbnailer and convert (from imagemagick): 3seconds for evince-thumbnailer and 14seconds for convert.
  • generates thumbnails recognized by nautilus
  • path names handled by the perl URL:file module (spaces and other characters are correctly translated into a file uri)
  • needs perl, present in a default installation
  • files unhandled by evince-thumbnailer will simply output an error - muted with 2>/dev/null
  • look at the MimeType line in /usr/share/thumbnailers/evince.thumbnailer to see a list of handled file types
  • updates: starting from 12.04 the thumbnail folder seems to be ~/.cache/thumbnails.
    More robust paths using readlink.

Inspiration:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=683394


Got distracted for a while and rosch beat me to it :) Didn't know evince-thumbnailer existed (I'm not a Gnome user) but anyway, since I've already written it, here it goes. It requires imagemagick installed, check and install if not there with:

which convert || sudo apt-get install imagemagick

Save as mkthumb.sh (for instance), chmod +x mkthumb.sh it and execute it with absolute paths as arguments (you can use -s as its first paramterer to skip generating thumbnails that already exist) i.e.:

user@host $ ./mkthumb.sh -s /home/user/Downloads /home/user/blah
Processing directory /home/user/Downloads/pics/
OK   /home/user/Downloads/pics/FeO08.jpg
OK   /home/user/Downloads/pics/UrOCu.jpg
OK   /home/user/Downloads/pics/34ATZ.gif
OK   /home/user/Downloads/pics/WBRE3.jpg
OK   /home/user/Downloads/pics/LjLdH.jpg
OK   /home/user/Downloads/pics/xvvae (1).jpg
SKIP /home/user/Downloads/pics/itcrowd.jpg
OK   /home/user/Downloads/pics/76180344.jpg
OK   /home/user/Downloads/pics/fgk5N.jpg
....

The script (I've modified it slightly to support most images, you can add more extensions if you need them):

#!/bin/bash

# USAGE: mkthumb.sh [-s] <abs_path> [abs_path]
# create nautilus thumbnails for images and PDFs in the directories (and their
# sub-directories) given as parameters.
# -s is used to skip generating thumbnails that already exist

skip_existing=0
if [[ "${1}" == "-s" ]]; then
  skip_existing=1
  shift
fi

mkthumb() {
  file="${1}"
  dest="${2}"
  convert -thumbnail 128x128 "${file}[0]" "${dest}" &>/dev/null
  if (( $? == 0 )); then
    echo "OK   ${file}"
  else
    echo "FAIL ${file}"
  fi
}

OLDIFS="${IFS}"
IFS=$'\n'
for dir in $@; do
  realdir=`realpath "${dir}"`
  echo "Processing directory ${realdir}"
  for file in $(find "${realdir}" -regextype posix-egrep -iregex \
  '.*\.(pdf|png|jpg|gif|jpeg)'); do
    md5=$(echo -n "${file}" | perl -MURI::file -MDigest::MD5=md5_hex -ne \
          'print md5_hex(URI::file->new($_));')
    dest="${HOME}/.thumbnails/normal/${md5}.png"
    if [[ -f "${dest}" ]]; then
      if [[ "${skip_existing}" == "0" ]]; then
        mkthumb "${file}" "${dest}"
      else
        echo "SKIP ${file}"
      fi
    else
      mkthumb "${file}" "${dest}"
    fi
  done
done
IFS="${OLDIFS}"

It handles files with spaces in their names without issues.

A bit of testing here:

user@host $ find .thumbnails/
.thumbnails/
.thumbnails/fail
.thumbnails/fail/gnome-thumbnail-factory
.thumbnails/normal

# ok - no thumbnails present.

user@host $ ./mkthumb.sh -s /home/user/Downloads/pdf/test/
Processing directory /home/user/Downloads/pdf/test/
OK   /home/user/Downloads/pdf/test/800pdf.pdf
OK   /home/user/Downloads/pdf/test/3_TO_pricelist.pdf
OK   /home/user/Downloads/pdf/test/111011-speisekarte-mit-desserts.pdf
OK   /home/user/Downloads/pdf/test/1186157_r4f3a355eb104a (1).pdf

user@host $ touch tstamp

user@host $ ./mkthumb.sh -s /home/user/Downloads/pdf/test/
Processing directory /home/user/Downloads/pdf/test/
SKIP /home/user/Downloads/pdf/test/800pdf.pdf
SKIP /home/user/Downloads/pdf/test/3_TO_pricelist.pdf
SKIP /home/user/Downloads/pdf/test/111011-speisekarte-mit-desserts.pdf
SKIP /home/user/Downloads/pdf/test/1186157_r4f3a355eb104a (1).pdf

# running nautilus once now to see if it generates new thumbnails

# checking for new thumbnails:

user@host $ find .thumbnails/ -newer tstamp

# None.