bash - 以文件大小为条件的 Imagemagick 批处理操作 - 如何?
问题描述
我Imagemagick
在 Windows 10 的命令行 Ubuntu 终端上运行 - 使用 Windows 10 中的内置工具 - Ubuntu App。
我是一个完整的linux新手,但已经安装imagemagick
在上述环境中。
我的任务 - 自动移除黑色(ish)边框并歪斜数千张扫描的 35 毫米幻灯片的图像。
我可以成功运行诸如
mogrify -fuzz 35% -deskew 80% -trim +repage *.tif
问题是:-
- 边界没有清晰定义,也不是完全黑色,因此 -fuzz。有些图像在某个模糊处被过度修剪,而另一些则修剪得不够。
所以我想要做的是有两次通过,不同的模糊百分比,原因如下: -
- 第一次通过,Fuzz% 低。许多图像根本不会被修剪,但我发现那些容易过度修剪的图像会以低 % 修剪 Ok
- 由于所有图像都以相同的文件大小开始,因此修剪 Ok 的图像将具有较小的文件大小(注意这些是 tifs 而不是 jpgs)
- 所以我需要做的是在更高的 fuzz% 下为第二遍设置文件大小条件,忽略低于某个值的文件大小并且不执行任何操作。
这样,几乎没有错误,所有图像都将被正确修剪。
所以问题 - 我如何调整命令行以进行 2 次传递并在第二次传递时忽略较小的文件大小?
我有一种可怕的感觉,答案将是一个脚本。我不知道如何构建或设置 Ubuntu 来运行它,所以如果是这样,请你也指点我帮忙!!
解决方案
在 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。
推荐阅读
- rxjs - 使用 RxJS 的工作流运行器
- javascript - Google Apps 脚本 - 将函数输出分配给变量并执行 document.write
- coinbase-api - 使用coinbase api将xlm从coinbase发送到恒星地址
- security - 我已在应用程序中订阅了一项服务,但我无法使用相同的凭据登录到我的其他设备
- azure - 如何为具有相同标签集的不同文档布局训练自定义模型?
- r - 为什么我会收到警告“在 edit.data.frame(x) 中:在 'name' 中添加了因子级别”使用 edit(x)
- java - 数组错误在给定条件下有多少奇数和偶数
- python - 如何将数组([1,1,1])与数组([2,3,4])相乘以获得数组[[2,3,4],[2,3,4],[2,3,4] ]]?
- csv - backtrader 如何使用分析器访问数据?
- .net-core - 如何验证使用密钥提交的凭据以在 .net 核心中使用 google 登录?