一部のテキストファイルのサムネイルを生成する必要があります。
明らかに、システムにはこれを正確に実行する機能があります(スクリーンショットを参照)。これらの画像にアクセスして、後で使用するためにコピーする方法はありますか?
または、このための特別なコマンド(ツール)はありますか?
私はこれを見ました:
コマンドラインのサムネイル
そしてこれ:
サムネイルを事前に生成するようにノーチラスに指示するにはどうすればよいですか?
これは便利でしたが、テキストを処理できるものはありませんでした。
承認された回答:
Imagemagickを使用してテキストアイコンを作成する
ここと同じ原理に基づいて、以下のスクリプトは、Imagemagickを使用してテキストファイルからテキストアイコンを作成します。
丸みを帯びた背景画像の色とテキストの色は、スクリプトの先頭(およびその他の多くのプロパティ)で設定できます。
機能
テキストファイルを読み取り、最初の4行を取得します(n_lines = 4
で設定) )、最初の7文字(n_chars = 10
で設定) )各行の、およびサイズの画像の上にオーバーレイを作成します。 psize = "100x100"
。
使用方法
スクリプトにはimagemagick
が必要です インストールするもの:
sudo apt-get install imagemagick
次に:
- スクリプトを空のファイルにコピーします
-
create_texticon.py
として保存します -
ヘッドセクションで設定:
- アイコンの背景の色
- アイコンのテキストレイヤーの色
- 作成されたアイコンのサイズ
- アイコンに表示する行数
- アイコンに表示する1行あたりの(最初の)文字数
- 画像を保存するパス
-
テキストファイルを引数として実行します:
python3 /path/to/create_texticon.py </path/to/textfile.txt>
スクリプト
#!/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 = "//eadn-wc01-5196795.nxedge.io/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