名前を含む画像のリストを確認する

名前を含む画像のリストを確認する

次の形式で画像整列規則を作成するには:

image_name_1 [IMAGE#1]
image_name_2 [IMAGE#2]
image_name_3 [IMAGE#3]
...

Imagemagickでは、次のように話すことができます。

montage -label '%f' -mode concatenate -tile 1x foo*.png out.png

しかし、これは追加されますファイル名画像+の下には名前の幅は考慮されません。

どこファイルの左側にある限り、どのファイル名が印刷されるかは問題ではありません(たとえば、既存のデカルトy軸)正しいビデオ。

32ビットLinuxで動作する限り、どのツールであるかは問題ではありません。

答え1

私はImageMagickの専門家ではないので、より良い方法があるかもしれませんが、2つのステップで実行できます。まず、各画像の左側にあるテキストを中間ファイルに追加してからモンタージュを実行します。

for file in foo*.png
do  convert "$file" -splice 100x0 -gravity west -annotate +10+0 '%f' /tmp/"$(basename "$file")"
done
montage -mode concatenate -tile 1x /tmp/*.png out.png

100最も広いファイル名タグに合うようにスプライス値を十分に広く調整する必要があります。


単一のコマンドを使用するための興味深い選択肢は次のとおりです。

convert \
 $(for file in foo*.png
   do echo '(' label:"$(basename "$file")" -gravity center "$file" +append ')'
   done) -gravity west -append out.png

+appendラベルと画像を水平に結合してから-append結果を垂直に結合する場所です。正確には必要ではありませんが、さらなる実験のための出発点になることができます。

答え2

誰かがこれをより詳細に解読したい場合は、混乱するかもしれませんが、興味深いヒントやヒントがいくつかあります。

プロセスは、適切なラベルを生成するためにファイル名の最大高さと幅を要求します。欠けている機能の1つは、テキストの垂直方向の中央揃えです(正直なところ、表示には優れています)。

テキストの幅+高さを整数として取得するには、次のいずれかの方法を使用します。

convert -debug annotate  xc: -font Arial -pointsize 24 \
-annotate 0 'Test' null: 2>&1 | \
awk -vRS='[;.]' -F': ' '/width|height/{print $2}'

ただし、ファイル数が通常の場合、ループ内の各ファイル名に対してこれを行うと、時間/リソースが大幅に増加する可能性があります。

したがって、以下のスクリプトは、すべてのファイル名を含む2つの一時ファイルを生成します。 1つは最大高さを、もう1つは最大幅を取得します。

ファイル名に改行文字がある場合、プロセスは中断されます。

#!/bin/bash

# Default Options
font=Arial
font_size=12
pad=20

usage()
{
    cat <<EOF_USAGE
Usage: $0 [OPTION] <images>

  -f | --font       <font>
  -z | --font_size  <N>
  -h | --help       [f|font]
EOF_USAGE
    exit 1
}

# Check for user-options
while [[ "$1" ]]
do
    case "$1" in
    --font|-f)font=$2; shift;;
    --help|-h)
        if [[ "$2" = "f" || "$2" = "font" ]]
        then
            convert -list font
            printf "\nCMD: convert -list font\n"
        fi
        usage
        exit 1
        ;;
    --font_size|-z)font_size=$2; shift;;
    --pad|-p)pad=$2; shift;;
    --)shift; break;;
    *)break;;
    esac
    shift
done

# Shallow Next option check (is it a file at all?)
! [[ -f "$1" ]] && printf "Not a file: %s\n" "$1" && exit 2

printf "Processing %d files ...\n" "$#"

txt_w=0
txt_h=0
tmp_dir=$(mktemp -d)

printf "tmp dir : %s\n" "$tmp_dir"

images=("$@")

printf "Calculating max width / height ..."

# Find max width from file names using font + font-size
txt_w=$(convert \
    -debug annotate  xc: \
    -font "$font" \
    -pointsize "$font_size" \
    -annotate 0 "\"$(printf "%s\n" "${images[@]}")\"" null: 2>&1 | \
    sed -n 's/.*Metrics:.* width: \([^.;]*\)[;.].*/\1/p' | sort -n | tail -n1)

# Find max height from file names using font + font-size
txt_h=$(convert \
    -debug annotate  xc: \
    -font "$font" \
    -pointsize "$font_size" \
    -annotate 0 "\"$(printf "%s" "${images[@]}")\"" null: 2>&1 | \
    sed -n 's/.*Metrics:.* height: \([^.;]*\)[;.].*/\1/p')

printf "\r\033[KWidth   : %d\n" "$txt_w"
printf "Height  : %d\n" "$txt_h"

# Add padding pixels
(( txt_w+=pad*2 ))

# Create the labeled images
for img in "${images[@]}"
do
    printf "\r\033[KCreating label for \`%s'" "$img"
    convert "$img" \
        -splice ${txt_w}x$txt_h \
        -gravity west \
        -font "$font" \
        -pointsize $font_size \
        -annotate +$pad+0 '%f' \
        "$tmp_dir"/"$(basename "$img")"
done

printf "\r\033[KMontage ...\n"

# Combine / Collate / Montage
montage -mode concatenate -tile 1x $(printf "$tmp_dir/%s " "${images[@]}") out.png

printf "Done!\n"

# Clean up
rm -r "$tmp_dir"

関連情報