如何通过对源图像进行随机分层来自动生成合成图像? [英] How can I automatically generate composite images by layering source images randomly?

查看:31
本文介绍了如何通过对源图像进行随机分层来自动生成合成图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个个人创意项目,需要帮助寻找或创建一个脚本,将从4个不同的文件夹随机选择4个图像,堆叠它们,并创建一系列的PNG合成图像作为输出。如果每个文件夹的图像可以分配到固定图层,则会有额外的点数(例如,"Background"文件夹中的图像将始终显示在背面)。 注意:我不知道如何编码。

推荐答案

更新

好的,我已经按如下方式更新了脚本:

  1. 它会生成10个输出文件-您可以通过在开始时更改NFILES来更改它
  2. 两次运行之间输出文件不会互相覆盖,下一个自由名称将以output-0.png开始,然后以output-1.png开始,依此类推。
  3. 我已更正Alpha合成以匹配您的文件。

脚本跟在后面.

#!/bin/bash

# Number of output files - edit freely :-)
NFILES=10

# Build arrays of filenames in each layer, assume directories are "Layer0", "Layer1" etc
IFS=$'
' L0files=($(find "Layer 0" -name "*.png"))
IFS=$'
' L1files=($(find "Layer 1" -name "*.png"))
IFS=$'
' L2files=($(find "Layer 2" -name "*.png"))
IFS=$'
' 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

从Layer[0-3]目录中的以下图像开始:

第0层

第1层

第2层

第3层

生成如下输出文件:

原始答案

我会安装ImageMagick来实现这一点-如果您先转到here来安装homebrew,那么它在OSX上是免费且容易安装的。然后在终端中键入以下内容。

brew install imagemagick

然后脚本将如下所示:

#!/bin/bash
# Build arrays of filenames in each layer, assume directories are "Layer0", "Layer1" etc
IFS=$'
' L0files=($(find "Layer0" -name "*.png"))
IFS=$'
' L1files=($(find "Layer1" -name "*.png"))
IFS=$'
' L2files=($(find "Layer2" -name "*.png"))
IFS=$'
' L3files=($(find "Layer3" -name "*.png"))

# 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]}

echo Overlaying $f0, $f1, $f2, $f3 to produce "result.png"
convert "$f0" "$f1" "$f2" "$f3" -compose over -composite result.png

因此,您可以将上述内容另存为generate,然后转到终端并执行以下一次操作以使其可执行

chmod +x generate

然后,您可以通过在Finder中双击其图标或键入:

来运行它
./generate

在终端。然后,它将随机生成4幅图像,每个文件夹各一幅,并将结果另存为result.png

您需要根据不同图层的图像所在位置编辑前4行-基本上,我假设图像位于名为Layer0-4的目录中,但您的图像可能位于/Users/FreddyFrog/pictures/backgrounds或类似目录中,在这种情况下,您将编辑

    IFS=$'
' L0files=($(find "/Users/FreddyFrog/pictures/backgrounds" -name "*.png"))

当我在我的Mac上运行它几次时,我得到:

Overlaying Layer0/l0-5.png, Layer1/l1-0.png, Layer2/l2-3.png, Layer3/l3-3.png to produce result.png
Overlaying Layer0/l0-3.png, Layer1/l1-4.png, Layer2/l2-3.png, Layer3/l3-3.png to produce result.png

这篇关于如何通过对源图像进行随机分层来自动生成合成图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆