Ubuntu – Generate thumbnails for text

command linenautilusthumbnails

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.

Best Answer

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