10

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.

2
  • @Rmano, what size would the icons need to be? Feb 28, 2015 at 7:35
  • @JacobVlijm The idea is to create a thumbnailer --- the basic form is script -s width input_url output_file which is the format used by nautilus. See for example rlog.rgtti.com/2011/11/24/… and the linked page...
    – Rmano
    Feb 28, 2015 at 9:13

2 Answers 2

6
+50

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
11
  • Is this solution persistent? Feb 28, 2015 at 8:12
  • What do you mean by persistent? The script creates the icons, or did I misunderstand the question? Feb 28, 2015 at 8:12
  • 1
    @Kasiya To use it as a thumbnailers, see rlog.rgtti.com/2011/11/24/… and rlog.rgtti.com/2010/11/28/adding-a-gnomenautilus-thumbnailer --- Nautilus will update it automatically. Nice idea; now just thinking about using pygment to syntax highlighting it and it will be perfect. Thanks!
    – Rmano
    Feb 28, 2015 at 9:17
  • 2
    @JacobVlijm not needed. Just drop the script in the correct place and write the /usr/share/thumbnailers/... file and the icons will automagically appears, and be managed by system (cached, updated on file changes, etc.)
    – Rmano
    Feb 28, 2015 at 10:24
  • 1
    @JacobVlijm whatever --- I got the idea. I think that the answer is sufficient as it is. A pity we can't leverage the system way of doing it, but I start supposing it's hard coded in some low-level library.
    – Rmano
    Mar 1, 2015 at 20:22
1

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

2
  • 1
    Hmmm... text file are NOT wordprocessor files. There is a quite basic misconception here. Text file are program sources, logs, just ".txt" fles, etc. Office documents and PDFs have their thumbnails working ok.
    – Rmano
    Feb 28, 2015 at 8:48
  • 1
    @Rmano: Going via PDF is a technique often used by ECM software when generating previews, even for formats that noone would usually convert to PDF, such as log files. So, this is a good idea. Feb 22, 2016 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .