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.

user10607
  • 337
  • @Rmano, what size would the icons need to be? – Jacob Vlijm Feb 28 '15 at 07: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 http://rlog.rgtti.com/2011/11/24/xfig-thumbnailers-with-gnome3nautilus3/ and the linked page... – Rmano Feb 28 '15 at 09:13

2 Answers2

6

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
Jacob Vlijm
  • 83,767
  • Is this solution persistent? – αғsнιη Feb 28 '15 at 08:12
  • What do you mean by persistent? The script creates the icons, or did I misunderstand the question? – Jacob Vlijm Feb 28 '15 at 08:12
  • I mean does this "thumbnails" is persistent after rebooting system or temporally (need to rerunning the script)? – αғsнιη Feb 28 '15 at 08:15
  • Thye script only creates the thumbnails, once set, it will be persistent, but setting the icons will not be the problem (of the question) I guess? – Jacob Vlijm Feb 28 '15 at 08:16
  • 1
    @Kasiya To use it as a thumbnailers, see http://rlog.rgtti.com/2011/11/24/xfig-thumbnailers-with-gnome3nautilus3/ and http://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 '15 at 09:17
  • @Rmano I will look into the links! (probably not today). In the meantime, I edited the script a bit. The (re-) naming section for bulk creating icons will follow. – Jacob Vlijm Feb 28 '15 at 10:14
  • 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 '15 at 10:24
  • @Jacob to be more specific --- the thumbnailer system will call the script with the arguments -s size input-url output-file.png. the size is 16, 32, 48 ... etc (as in icons themes), and the input-url will be something like file://path/to/the/file.txt --- the script is supposed to leave in output-file.png the thumbnail. – Rmano Mar 01 '15 at 19:59
  • @Rmano that is clear. One question: with 16 and probably 32 px there will probably not much to read (visually). should the script change n-lines, n_caharacters automatically? – Jacob Vlijm Mar 01 '15 at 20:19
  • 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 01 '15 at 20:22
  • Perfect, thanks. I might post an edited (ready-to-use) version tomorrow. – Jacob Vlijm Mar 01 '15 at 20:25
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

Prinz
  • 611
  • 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 '15 at 08: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. – Nicolas Raoul Feb 22 '16 at 09:00