我正在嘗試撰寫代碼來執行以下操作。
1:從輸入行獲取所有目錄名稱到一個字串中(目錄輸入的數量可能會有所不同)
2:對于這些目錄中的所有 tiff 檔案,使用相同名稱的影像(使用 imagemagick)蒙太奇并將其保存在主檔案夾或給定檔案夾中。
我想組合兩個或多個影像(取決于命令列輸入中的目錄數)。每個目錄都有與另一個目錄同名的影像輸出。我想合并給定目錄中具有相同名稱的檔案
我寫的代碼在下面,但它沒有將 $name 作為變數傳遞給 montage 命令。我在這里做錯了什么?任何幫助將不勝感激。
for arg in "$@"; do #
n1=$arg
fname =$n1"\${name} " #Get all the directory names in a string with $name at the end. for eg: Baseline/$name
done
echo $fname
for n in $arg/*.tif; do
name="$(basename "$n")"
name1=$(echo "$name" | cut -f 1 -d '.')
montage -tile 3x3 $fname name1.png
exit
done
更多細節
uj5u.com熱心網友回復:
更新答案
感謝您的澄清,如果您將以下內容另存為monty.sh并使其可執行,您可以做您想做的事情:
chmod x monty.sh
這是代碼:
#!/bin/bash
# Goto first subdirectory and take it off args list
cd "$1"
shift
# Grab remaining directory names into array
friends=( "$@" )
>$2 echo "DEBUG: friends=${friends[@]}"
# Iterate over files in first directory
for this in *.tiff ; do
>&2 echo "DEBUG: this=$this"
# Generate list of files to montage
{
# ... starting with this one...
echo "$this"
# ... and adding in his friends
for friend in "${friends[@]}" ; do
next="../${friend}/${this}"
>&2 echo "DEBUG: next=$next"
if [ -f "$next" ] ; then
echo "$next"
else
>&2 echo "ERROR: file $next is missing"
fi
done
} | magick montage -tile 3x @- ../"${this/tiff/png}"
done
那么你的命令將是:
./monty.sh Output1 Output2 Output3
或者,更簡潔地說:
./monty.sh Output{1,2,3}
如果您不熟悉bash語法,for回圈中間的代碼本質上是這樣做的:
...
...
{
echo first filename to montage onto stdout
echo second filename to montage onto stdout
echo third filename to montage onto stdout
} | magick montage <ALL FILENAMES ON STDIN> result.png
因此,重要的是將內部的所有錯誤訊息{...}發送到stderr其他錯誤訊息將轉到montage將它們解釋為檔案名的命令。這就是為什么所有除錯陳述句都以 開頭的>&2 echo ...原因,因為這會將它們定向到stderr這樣它們就不會與stdout.
原答案
I don't understand what you are trying to do, but if you can write all the filenames into ImageMagick's stdin, you can make it montage them like this:
find . -name "*.tif" -print | magick montage -geometry 0 0 -tile x3 @- result.png
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350266.html
標籤:linux 猛击 图像魔术师 imagemagick 转换
上一篇:使用bash-c逐行決議
