imagemagick Convertを使用して、テキストファイルの各行を別々の画像に変換します。

imagemagick Convertを使用して、テキストファイルの各行を別々の画像に変換します。

約1,000行のテキストファイルをインポートし、可能なImageMagick変換を使用してファイルの各行に別々のpng画像を作成したいと思います。画像は1920 x 1080で、背景は黒で、テキストは白でなければなりません。以下を使用して、リストを一度に画像にインポートできます。

convert -size 1980x1020 xc:black -font Play-Regular.ttf -pointsize 85 -fill white -gravity center -draw "text 0,0 '$(cat list.txt)'" image.png

また、各行を繰り返すためにbashファイルを作成してみました。

#!/bin/bash
File="list.txt"
Lines=$(cat $File)
for Line in $Lines
do
convert -size 1980x1020 xc:black -font Play-Regular.ttf -pointsize 85 -fill white -gravity center -draw "text 0,0 '$(cat Line)'" $line.png
done

近づいているような気がしますが、bash-fuが弱く、コマンドで複数のエラーが発生します。

ベストアンサー1

convertまず、bashの代わりにzshを使用してこの行を.hereに変換する必要があります。

#! /bin/zsh -
file=list.txt

typeset -Z4 n=1 # line counter 0-padded to length 4.
set -o extendedglob

while IFS= read -ru3 line; do
  # remove NUL characters if any:
  line=${line//$'\0'}
  # escape ' and \ characters with \:
  escaped_line=${line//(#m)[\'\\]/\\$MATCH}

  convert -size 1920x1080 \
          xc:black \
          -font Play-Regular.ttf \
          -pointsize 85 \
          -fill white \
          -gravity center \
          -draw "text 0,0 '$escaped_line'" line$n.png
  (( n++ ))
done 3< $file

おすすめ記事