为文本生成缩略图?

我需要为一些文本文件生成缩略图。 显然,系统有能力做到这一点(见屏幕截图)。 有什么方法可以访问这些图像,只是复制它们供以后使用?

或者有一个特殊的命令(工具)吗?

somefolder.png

我看着这个: 命令行缩略图

而且: 我如何指示Nautilus预先生成缩略图?

哪个是有用的,但没有一个可以处理文本。

使用Imagemagick创建文本图标

基于与此处相同的原理,下面的脚本在Imagemagick的帮助下从文本文件创建文本图标。

圆角背景图像的颜色和文本颜色可以在脚本的头部(以及许多其他属性)中设置。

在此处输入图像描述

它能做什么
它读取文本文件,取前四行(在n_lines = 4设置),每行的前七个字符(在n_chars = 10设置),并在大小的图像上创建一个叠加,例如psize = "100x100"

如何使用

该脚本需要安装imagemagick

 sudo apt-get install imagemagick 

然后:

  1. 将脚本复制到空文件中
  2. 将其另存为create_texticon.py
  3. 设置在头部:

    • 图标背景的颜色
    • 图标的文本层的颜色
    • 创建的图标的大小
    • 图标中显示的行数
    • 每行显示的(第一个)字符数量
    • 保存图像的路径
  4. 使用您的文本文件作为参数运行它:

     python3 /path/to/create_texticon.py  

剧本

 #!/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 

想法:

将文本文件转换为pdf并使用pdfdraw生成缩略图。

unoconv是一种在OpenOffice办公套件理解的各种文档之间进行转换的软件。

此方法的优点:通过创建脚本可以轻松生成几乎所有文档的批量缩略图。

请参阅要点以了解相关步骤。

  1. 安装OpenOffice无头包

     sudo apt-get install openoffice.org-headless openoffice.org-java-common openoffice.org-writer openoffice.org-calc openoffice.org-impress 
  2. 安装UNO python库

     sudo apt-get install python-uno unoconv 
  3. 安装必要的字体(特别是国际语言)

    将字体复制到/usr/share/fonts/truetype/然后运行fc-cache

  4. 将OpenOffice作为服务运行

     soffice -headless -nofirststartwizard -accept="socket,host=localhost,port=8100;urp;StarOffice.Service" 
  5. 使用unoconv命令将文档转换为PDF

     unoconv -f pdf __[filename]__ 
  6. 使用MuPDF工具创建PDF缩略图

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

关于SO的类似问题