Generate thumbnails for text?

I need to generate thumbnails for some text files. Obviously the system somehow has the ability to do exactly this (see the screen shot). Is there any way I can access these images and just copy them for later use?

Or is there a special command (tool) for this?

somefolder.png

I looked at this: command line thumbnailing

And this: How can I instruct Nautilus to pre-generate thumbnails?

Which were useful but none could deal with text.


Using Imagemagick to create text icons

Based on the same principle as here, the script below creates a text icon from a text file with the help of Imagemagick.

The color of the rounded background image and the text color can be set in the head of a script (as well as a number of other properties).

enter image description here

What it does
It reads the textfile, takes th first four lines (set in n_lines = 4), the first seven characters (set in n_chars = 10) of each line, and creates an overlay over an image of the size, set in e.g. psize = "100x100".

How to use

The script needs imagemagick to be installed:

sudo apt-get install imagemagick

Then:

  1. Copy the script into an empty file
  2. Save it as create_texticon.py
  3. set in the head section:

    • the color of the icon's background
    • the color of the icon's textlayer
    • The size of the created icon
    • The number of lines to show in the icon
    • The number of (first) characters per line to show in the icon
    • The path where to save the image
  4. Run it with your textfile as an argument:

    python3 /path/to/create_texticon.py </path/to/textfile.txt>
    

The script

#!/usr/bin/env python3
import subprocess
import sys
import os
import math

temp_dir = os.environ["HOME"]+"/"+".temp_iconlayers"
if not os.path.exists(temp_dir):
    os.mkdir(temp_dir)

# --- 
bg_color = "#DCDCDC"                                # bg color
text_color = "black"                                # text color
psize = [64, 64]                                    # icon size
n_lines = 4                                         # number of lines to show
n_chars = 9                                         # number of (first) characters per line
output_file = "/path/to/output/icon.png"            # output path here (path + file name)
#---

temp_bg = temp_dir+"/"+"bg.png"; temp_txlayer = temp_dir+"/"+"tx.png"
picsize = ("x").join([str(n) for n in psize]); txsize = ("x").join([str(n-8) for n in psize])

def create_bg():
    work_size = (",").join([str(n-1) for n in psize])
    r = str(round(psize[0]/10)); rounded = (",").join([r,r])
    command = "convert -size "+picsize+' xc:none -draw "fill '+bg_color+\
              ' roundrectangle 0,0,'+work_size+","+rounded+'" '+temp_bg
    subprocess.call(["/bin/bash", "-c", command])

def read_text():
    with open(sys.argv[1]) as src:
        lines = [l.strip() for l in src.readlines()]
        return ("\n").join([l[:n_chars] for l in lines[:n_lines]])

def create_txlayer():
    subprocess.call(["/bin/bash", "-c", "convert -background none -fill "+text_color+\
                      " -border 4 -bordercolor none -size "+txsize+" caption:"+'"'+read_text()+'" '+temp_txlayer])

def combine_layers():
    create_txlayer(); create_bg()
    command = "convert "+temp_bg+" "+temp_txlayer+" -background None -layers merge "+output_file
    subprocess.call(["/bin/bash", "-c", command])

combine_layers

Idea :

convert the text file to pdf and use pdfdraw to generate the thumbnail.

unoconv is a software that converts between various documents that the OpenOffice office suite understands.

Advantage of this method : Bulk thumbnails for almost all document can be generated easily by creating a script.

See gist for the steps .

  1. Install OpenOffice headless package

    sudo apt-get install  openoffice.org-headless  openoffice.org-java-common  openoffice.org-writer  openoffice.org-calc  openoffice.org-impress
    
  2. Install UNO python library

    sudo apt-get install python-uno unoconv
    
  3. Install necessary fonts (Especially for international language)

    Copy fonts to /usr/share/fonts/truetype/ Then run fc-cache

  4. Run OpenOffice as a service

    soffice -headless -nofirststartwizard -accept="socket,host=localhost,port=8100;urp;StarOffice.Service"
    
  5. Convert document to PDF using unoconv command

    unoconv -f pdf __[filename]__
    
  6. Create PDF thumbnail by using MuPDF tool

    pdfdraw -r 100 -o __[output-thumbnail]__ __[pdf-file]__ 1  
    

similiar question on SO