使用Imagemagick脚本处理多张照片

3

我有一系列使用imagemagick的命令,逐步清理单个图像:

#! /bin/bash

convert -unsharp 5 "base.jpg" "base.ppm"

convert -opaque white -fill white -fuzz 10% "base.ppm" "image1_step1.tif"

convert -fuzz 5% -fill "#816c51" -opaque "#816c51" "image1_step1.tif" "image1_step2.tif"
convert -fuzz 1.5% -fill "#816c51" -opaque "#5a4a3b" "image1_step2.tif" "image1_step3.tif"

convert -fuzz 12% -fill "black" -opaque "#1c110f" "image1_step3.tif" "image1_step4.tif"

convert image1_step4.tif image1_cleaned.jpg

我希望能在一个特定的文件夹中使用这个脚本来处理几百个tif文件。我不知道如何自动化这个过程,非常感谢您的帮助。

谢谢!

2个回答

2

将您的转换命令包装在一个 for 循环中。

#!/bin/bash
dir="/path/to/dir"
cd "$dir"
for file in *.jpg
do
    base=${file%*.jpg}
    convert ... "$base" "$base.ppm"
    convert ... "$base.ppm" "${base}_step1.tif"
    # etc
done

@Nimbuz:我觉得看起来不错,但最后一个转换似乎没有操作。也许你的意思是第二个参数应该是 "${base}_final.jpg",这与你问题中的代码是一致的。 - Dennis Williamson

0

可能是这样的:

#!/bin/bash

VICTIM="$1"

if [ -z "$VICTIM" ]; then
    echo >&2 "Syntax: $0 <imagefile>"
    exit 1
fi

if [ ! -f "$VICTIM" ]; then
    echo >&2 "$VICTIM is not a regular file"
    exit 1
fi

VICTIM=$(readlink -f "$VICTIM")

DSTDIR=$(dirname "$VICTIM")

WORKDIR=$(mktemp -d /tmp/.workXXXXXX)

if [ "$?" != "0" ]; then
    echo >&2 "Aiie, failed to create temporary directory"
    exit 1
fi

export WORKDIR

trap "rm -rf $WORKDIR" exit INT QUIT TERM

(
    cd $WORKDIR
    convert -unsharp 5 "$VICTIM" step1.ppm && \
    convert -opaque white -fill white -fuzz 10% step1.ppm step2.tif && \
    convert -fuzz 1.5% -fill "#816c51" -opaque "#5a4a3b" step2.tif step3.tif && \
    convert -fuzz 12% -fill "black" -opaque "#1c110f" step3.tif step4.tif
)

if [ "$?" != "0" ]; then
    echo >&2 "Aiie, image processing failed"
    exit 1
fi

convert $WORKDIR/step4.tif $DSTDIR/CLEANED-$VICTIM

RC=$?
if [ "$RC" != "0" ]; then
   echo >&2 "Aiie, final conversion failed"
fi

exit $RC

将此脚本命名为"convert_one.sh"并使用以下方式调用:

for i in *.jpg; do sh convert_one.sh $i;done

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接