对多张照片使用 Imagemagick 脚本

发布于 2024-12-26 00:39:06 字数 581 浏览 0 评论 0原文

我有 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 文件上使用此脚本。我不确定如何自动执行此操作,我非常感谢任何帮助。

谢谢!

I have imagemagick sequence of commands that cleanup a single image step by step:

#! /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

I'd like to use this script on a couple hundred tif files in a particular folder. I'm not sure how to automate this, I'd really appreciate any help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2025-01-02 00:39:06

将转换命令包装在 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

Wrap your convert commands in a for loop.

#!/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
心在旅行 2025-01-02 00:39:06

类似这样的事情可能会发生:

#!/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

Something like this may do:

#!/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

Name this script "convert_one.sh" and invoke with:

for i in *.jpg; do sh convert_one.sh $i;done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文