image-processing - 通过分层源图像自动生成随机构图**无需创建重复项**
问题描述
这是ign提出的这个问题的后续, Marc Setchell专业地回答了这个问题
它工作得很好,尽管我希望找到一种方法来避免在随机化过程中形成重复。我将对一堆层进行数百种变化,但存在细微的差异,所以我不能真正进入并以故障安全的方式发现重复项。
这是我正在使用的代码,如上所述 - 归功于 Marc Setchell!
#!/bin/bash
# Number of output files - edit freely :-)
NFILES=10
# Build arrays of filenames in each layer, assume directories are "Layer0", "Layer1" etc
IFS=$'\n' L0files=($(find "Layer 0" -name "*.png"))
IFS=$'\n' L1files=($(find "Layer 1" -name "*.png"))
IFS=$'\n' L2files=($(find "Layer 2" -name "*.png"))
IFS=$'\n' L3files=($(find "Layer 3" -name "*.png"))
# Produce NFILES output files
for i in `seq 1 $NFILES`; do
# Choose random index into each array of filenames
index0=$( jot -r 1 0 $((${#L0files[@]} - 1)) )
index1=$( jot -r 1 0 $((${#L1files[@]} - 1)) )
index2=$( jot -r 1 0 $((${#L2files[@]} - 1)) )
index3=$( jot -r 1 0 $((${#L3files[@]} - 1)) )
# Pick up files as specified by the random index
f0=${L0files[index0]}
f1=${L1files[index1]}
f2=${L2files[index2]}
f3=${L3files[index3]}
# Generate output filename, "output-nnn.png"
# ... where nnn starts at 0 and goes up till no clash
i=0
while :; do
out="output-$i.png"
[ ! -f "$out" ] && break
((i++))
done
echo $f0, $f1, $f2, $f3 "=> $out"
convert "$f0" "$f1" -composite "$f2" -composite "$f3" -composite "$out"
done
解决方案
恕我直言,与 IM 或 PS 无关。只需命名您的文件,以便它们包含您用于创建它们的图层的索引,如果您发现您正在再次创建相同的文件,请跳过。
out="output-$index0-$index1-$index2-$index3.png"
[[ ! -f "$out" ]] && continue
((i++))
这还要求您不要使用seq 1 $NFILES
迭代文件,因为您只想在不跳过(*)时增加计数器。
完成后,如有必要,您可以将文件重命名为序列。
bash 中的另一种方法是声明一个关联数组,并在其中为您创建的每个文件放入一个元素:
# declare associative array
declare -A doneFiles
# when creating a file:
combination=$index0-$index1-$index2-$index3
donefiles[$combination]="done" # or any info
# testing:
[[ -n ${doneFiles[$combination]} ]] && continue # already done
(*) 顺便说一句i
,即使您不明确使用外部变量,同时使用外部和内部循环也是疯狂的。但是内部循环是糟糕的代码,因为您可以使用外部i
.
推荐阅读
- android - Kotlin GoogleMaps ClusterManager 返回的集群项目大小小于最初传递给它的项目大小
- csv - 使用 Overwrite 将 PySpark Dataframe 写入 GCS
- vue.js - 发布新的Vue应用程序版本时如何强制清除chrome中的缓存
- flutter - 如何转换为未知的通用运行时类型(C# ChangeType 等效项)
- python - beautifulsoup for loop 仅提取第一页数据
- infinispan - 为开发或测试环境切换 infinispan 缓存
- python - 如何为烧瓶 azure Web 应用程序设置启动命令
- linux - 通过 SSH 进行 Rsync 时的分叉进程
- angular - 无法绑定“ngModel”也无法在角度材料角度 6 中渲染 mat-select
- python - 从 CSV 文件读取并上传到 Google Data Store Python