Image processing 如何通过随机分层源图像自动生成合成图像?

Image processing 如何通过随机分层源图像自动生成合成图像?,image-processing,scripting,imagemagick,photoshop,Image Processing,Scripting,Imagemagick,Photoshop,我正在从事一个个人创意项目,需要帮助查找或创建一个脚本,该脚本将从4个不同文件夹中随机选择4个图像,将它们堆叠起来,并创建一系列png合成图像作为输出。如果可以将每个文件夹的图像指定给固定层,则会获得额外的点数(例如,来自“backgrounds”文件夹的图像将始终显示在后面)。 注意:我不知道如何编码。已更新 好的,我更新了脚本如下: 它生成10个输出文件-您可以通过在开始时更改NFILES来更改它 输出文件在运行之间不会相互覆盖,将使用下一个自由名称,从output-0.png开始,然后从o

我正在从事一个个人创意项目,需要帮助查找或创建一个脚本,该脚本将从4个不同文件夹中随机选择4个图像,将它们堆叠起来,并创建一系列png合成图像作为输出。如果可以将每个文件夹的图像指定给固定层,则会获得额外的点数(例如,来自“backgrounds”文件夹的图像将始终显示在后面)。
注意:我不知道如何编码。

已更新

好的,我更新了脚本如下:

  • 它生成10个输出文件-您可以通过在开始时更改NFILES来更改它
  • 输出文件在运行之间不会相互覆盖,将使用下一个自由名称,从
    output-0.png
    开始,然后从
    output-1.png
    开始,依此类推
  • 我已更正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=$'\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
    
    从图层[0-3]目录中的以下图像开始:

    第0层

    第1层

    第2层

    第3层

    生成如下输出文件:

    原始答案

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

    brew install imagemagick
    
    脚本将如下所示:

    #!/bin/bash
    # Build arrays of filenames in each layer, assume directories are "Layer0", "Layer1" etc
    IFS=$'\n' L0files=($(find "Layer0" -name "*.png"))
    IFS=$'\n' L1files=($(find "Layer1" -name "*.png"))
    IFS=$'\n' L2files=($(find "Layer2" -name "*.png"))
    IFS=$'\n' 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
    
    因此,您可以将上述内容保存为
    生成
    ,然后转到终端并执行以下一次,使其可执行

    chmod +x generate
    
    然后,您可以通过双击Finder中的图标或键入以下内容来运行它:

    ./generate
    
    在候机楼。然后,它将生成一个随机覆盖的4幅图像,每个文件夹一幅,并将结果保存为
    result.png

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

        IFS=$'\n' 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
    

    您是否使用Linux或OSX这样的操作系统?如果图像大小不同怎么办-重新缩放到固定大小?谢谢你的回答。我在OSX上工作,我的源图像都是png,alpha通道以最终大小保存,因此无需进行更改。最后的图片应该有4层,我有一套图片,分别放在不同的文件夹中。谢谢你的详细回答@Mark,这似乎解决了主要任务。我对透明胶片的渲染方式有问题,但我一次只能看到两层,而且我的平面颜色背景似乎覆盖了最前面的图像。
    因为我需要运行脚本几次,我怎样才能避免结果被覆盖,怎样才能让它播放呢?比如说,每次我运行它10次吧?我已经有一段时间不在Mac上了。你能发布每一层的图片和你期望的结果吗?或者单击我的个人资料并向他们发送电子邮件,以及结果的外观。我可以在家里解决所有问题。再次感谢。我已经上传了一个4层的示例和一个示例结果图像,我认为这现在可以满足您的所有要求-只要说是否有任何错误或不清楚的地方适合您的需要。@ign:通常推荐的说“谢谢”的方式在StackOverflow上,就是“向上投票”你遇到的、读过的、认为是好的或是教给你新东西的任何答案。你只是“接受”了马克的答案,但你还没有“否决”它(正如我刚才所做的那样)。点击答案左上角的
    ^
    -形图标即可完成向上投票。