首页 > 解决方案 > 以文件大小为条件的 Imagemagick 批处理操作 - 如何?

问题描述

Imagemagick在 Windows 10 的命令行 Ubuntu 终端上运行 - 使用 Windows 10 中的内置工具 - Ubuntu App。

我是一个完整的linux新手,但已经安装imagemagick在上述环境中。

我的任务 - 自动移除黑色(ish)边框并歪斜数千张扫描的 35 毫米幻灯片的图像。

我可以成功运行诸如

mogrify -fuzz 35% -deskew 80% -trim +repage *.tif

问题是:-

所以我想要做的是有两次通过,不同的模糊百分比,原因如下: -

这样,几乎没有错误,所有图像都将被正确修剪。

所以问题 - 我如何调整命令行以进行 2 次传递并在第二次传递时忽略较小的文件大小?

我有一种可怕的感觉,答案将是一个脚本。我不知道如何构建或设置 Ubuntu 来运行它,所以如果是这样,请你也指点我帮忙!!

标签: bashimagemagickmogrify

解决方案


在 ImageMagick 中,您可以执行以下操作:

获取输入文件大小

Use convert to deskew and trim. 

Then find the new file 

Then compare the new to the old to compute the percentdifference to some percent threshold

If the percent difference is less than some threshold, then the processing did not trim enough 

So reprocess with a higher fuzz value and write over the input; otherwise keep the first one only and do not write over the old one.


Unix 语法。

选择两个模糊值

选择百分比变化阈值

创建一个新的空目录来保存输出(结果)

cd
cd desktop/Originals
fuzz1=20
fuzz2=40
threshpct=10
list=`ls`
for img in $list; do
filesize=`convert -ping $img -precision 16 -format "%b" info: | sed 's/[B]*$//'`
echo "filesize=$filesize"
convert $img -background black -deskew 40% -fuzz $fuzz1% ../results/$img
newfilesize=`convert -ping ../results/$img -precision 16 -format "%b" info: | sed 's/[B]*$//'`
test=`convert xc: -format "%[fx:100*($filesize-$newfilesize)/$filesize<$threshpct?1:0]" info:`
echo "newfilesize=$newfilesize; test=$test;"
[ $test -eq 1 ] && convert $img -background black -deskew 40% -fuzz $fuzz2% ../results/$img
done


问题是您需要确保将输出的 TIFF 压缩设置为与输入相同,以便文件大小相等,并且可能新大小不会像 JPG 那样大于旧大小。

请注意,sed 用于从文件大小中删除字母 B(字节),因此可以将它们作为数字而不是字符串进行比较。-precision 16 强制“%b”报告为 B 而不是 KB 或 MB。


推荐阅读