2

I have a rather large set of documents (150 GiB ~5000 files) and would like to know how to pre-generate thumbnails, preferably from the command line, so that when I navigate to the folder, I don't have to wait for them to be created.

Scott Goodgame
  • 2,636
  • 15
  • 20

2 Answers2

3

Thanks to the comments I decided to use one of the scripts from this recommendend AskUbuntu post.

I just copied it to a file, saved it to my ~/bin (in my case as generate-thumbnails) and issued chmod +x on it. Then I just navigated to the ~/Documents folder and ran generate-thumbnails * I don't know How long it took, but when I woke up the next, all was good.

Be aware that you might need to install gir1.2-gnomedesktop-3.0:

sudo apt-get install gir1.2-gnomedesktop-3.0

Since it doesn't waste time generating duplicates, it would be a low impact cron job (after the first run).

From a terminal prompt crontab -e If it is your first time editing the crontab, it will ask you to choose an editor. I just use nano and I'll give the keyboard commands for it.

Add the following to the end...

0 4 * * * /home/user/bin/generate-thumbnails /home/user*
  • Ctl o Will save the file.
  • Ctl x Will exit. (it you have a syntax error it'll let you know and you can just crontab -e again. This sets up cron to run at 4am every morning.
  • If you want to change it or look at other cron options.. https://help.ubuntu.com/community/CronHowto is a good place to find out options.

Main reasons for running this script:

  • it will recurse through sub-directories
  • it is aware of duplicates
  • it works equally well for videos
  • it will work in other DE's (I switch often based on what I'm doing)
  • it would be good for a cron job

Here is the script.... (Thanks James Henstridge !!

#!/usr/bin/env python3
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))

Shivanshu
  • 139
Scott Goodgame
  • 2,636
  • 15
  • 20
2

Bash script (kudos to Tim) . From the link the following script and comments:

for i in *.jpg
do
djpeg $i | pnmscale -xysize 48 38 | cjpeg -opti -progr -qual ‘75%’ > TN_$i
echo $i processed
done
  • This will make thumbnails for files ending in .jpg and create a thumbail with name TN_*.jpg. Adjust the parameters to your liking.

  • For thumbnails, you quite often don’t want to use imagemagick or convert or whatever; for my normal size (48×38), it creates a whopping huge 50K jpg when there are other ways of getting them much much smaller:

  • This keeps the average image-size down to about 1K – rather more suited to a page of thumbnails!

  • pnmscale will be installed by default

  • Installing djpeg and cjpeg

    sudo apt-get install libjpeg-turbo-progs
    
  • edit: wont work with PDF


Ignoring the advice in the comment about using imagemagick ...

sudo apt-get install imagemagick

and use this as a similar method as above but in this case using the convert command from IM:

#!/bin/bash
FILES="$@"
for i in $FILES
do
echo "Processing image $i ..."
/usr/bin/convert -thumbnail 100X100 $i $i.png
done

Here is an elaborate document on thumbnailing using IM. You can specify height, width and lots more. Example from the link:

For example, this converts JPG images into GIF thumbnails in a "thumbs" sub-directory that was just created 100 pixels wide and 100 pixels high

mkdir thumbs  
mogrify  -format gif -path thumbs -thumbnail 100x100 *.jpg

Oh the title is specific about PDF. Using IM...

convert -thumbnail 100x100 1.pdf 1.png
Rinzwind
  • 299,756