蒙太奇不会使用文本文件中表示的字符串列表标记图像

时间:2016-09-07 21:02:23

标签: image terminal imagemagick

我有一个目录( <system.serviceModel> <services> <service name="Portal.EP1" behaviorConfiguration="debug"></service> <service name="Portal.EP2" behaviorConfiguration="debug"></service> </services> <behaviors> <serviceBehaviors> <behavior name="debug"> <!-- To avoid disclosing metadata information, set the value below to false before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> ),里面有大量图片。我想用每个文件的特定标签来剪辑它们。我有一个文本文件(./img/),其中包含标签列表:

  

label1的
  LABEL2
  LABEL3
  label4
  ...

我使用这个命令:

label.txt

但结果是,所有图像都被montage -label @label.txt -size 512x512 "./img/*.*[120x90]" -geometry +5+5 photo.png 的所有行标记!我如何将它们分开,因此每个图像都会被label.txt的相关行标记?

2 个答案:

答案 0 :(得分:1)

ImageMagick通过解析命令行并从左到右执行参数来工作。

假设我有以下图片:

eh-banner-201606.png
eh-banner-201607.png
eh-banner-201608.png
eh-banner-201609.png

(这些是网站的一些横幅,我手头上最快的图像)

现在,如果我想构建一个具有不同标签的蒙太奇,我必须这样做:

montage -font /usr/share/fonts/TTF/DejaVuSerif.ttf -size 512x512 \
        -label 'first image' eh-banner-201606.png \
        -label 'second image' eh-banner-201607.png \
        -label 'third image' eh-banner-201608.png \
        -label 'fourth image' eh-banner-201609.png \
        -geometry +5+5 out.png

-font参数不是严格需要的,但我把它包含在迂腐中)

这适用于@labelfile

montage -font /usr/share/fonts/TTF/DejaVuSerif.ttf -size 512x512 \
        -label '@first.txt' eh-banner-201606.png \
        -label '@second.txt' eh-banner-201607.png \
        -label '@third.txt' eh-banner-201608.png \
        -label '@fourth.txt' eh-banner-201609.png \
        -geometry +5+5 out.png

每个文件将包含一行(或多行),这些行将成为命令行中下一个图像的标题。

因此,您需要一个脚本来执行您的操作,例如:

#!/bin/bash

LABELS=${1:-./labels.txt}
IMAGES=""

readarray labels < "$LABELS"
i=0
for img in "$@"; do
    IMAGES+=" -label '${labels[i]}' $img"
    ((i++))
done

echo montage -size 512x512 $IMAGES -geometry +5+5 out.png
montage -size 512x512 $IMAGES -geometry +5+5 out.png

您可以做(假设脚本名称为script.sh):

./script.sh label.txt ./img/*.*120x90

警告:图像文件不能包含空格或特殊的shell字符(这需要第三级引用,这非常容易出错)。请注意,[]是特殊的shell字符,具体取决于上下文。如果您需要更复杂的处理,使用Perl或Python可能更明智。

答案 1 :(得分:1)

我更改了sample code所代表的@grochmal,并且有效:

#!/bin/bash

LABELS=${1:-./labels.txt}
IMAGES=""

readarray labels < "$LABELS"

i=0
echo @ is $@

#copy input argument array in InArg array:
for img in "$@"; do
InArg[$i]=$img
    ((i++))
done

#get dimensions of InArg array:
arraylength=${#InArg[@]}

#extract Input Image array from InArg array:
for (( i=1; i<${arraylength}; i++ ));
do
ImageArray[$i]=${InArg[$i]}
done

#create IMAGES from some elements of InArg array:
for (( i=1; i<${arraylength}; i++ ));
do
IMAGES+=" -label ${labels[i-1]} ${ImageArray[$i]}"
done
echo  "IMAGES is $IMAGES"

echo montage -size 512x512 $IMAGES -font FreeMono-Bold-Oblique -pointsize 30 -geometry '120x90+5+5>' -tile 5x  -frame 5  -shadow out.html
montage -size 512x512 $IMAGES -font FreeMono-Bold-Oblique -pointsize 30 -geometry '120x90+5+5>' -tile 5x  -frame 5  -shadow out.html
相关问题