如何通过在 Windows 中随机分层源图像来自动生成合成图像?

How can I automatically generate composite images by layering source images randomly in Windows?

您好,我很想知道如何从一组不同的文件夹中随机对几张图像进行图像合成。我认为 Mark Setchell in this thread 的回答回答了我的问题,但我在 windows 上,我不明白如何在 Windows 的 BAT 文件中使用这个脚本。我不知道 unix 代码中的 $ 或者我是否正确地提到了它。

提前致谢。

更新:

我终于能够完成这项工作了。也感谢其他线程解决了我的 bat 命令脚本问题。

echo OFF
cls
setlocal enableDelayedExpansion

set /a folder1=1
for %%A in (./bg/*.png) do (set fol1[!folder1!]=%%~A
set /a folder1+=1 )

set /a folder2=1
for %%B in (./frame/*.png) do (set fol2[!folder2!]=%%~B
set /a folder2+=1 )

set /a folder3=1
for %%C in (./people/*.png) do (set fol3[!folder3!]=%%~C
set /a folder3+=1 )

set /a folder4=1
for %%D in (./box/*.png) do (set fol4[!folder4!]=%%~D
set /a folder4+=1 )


set totaloutput=10

for /l %%x in (1, 1, %totaloutput%) do (

    :: Assigning random number to pick photos from array
    set /a "_rand1=(!RANDOM!* (%folder1%-1) /32768)+1"
    set /a "_rand2=(!RANDOM! * (%folder2%-1) /32768)+1"
    set /a "_rand3=(!RANDOM! * (%folder3%-1) /32768)+1"
    set /a "_rand4=(!RANDOM! * (%folder4%-1) /32768)+1"
    
    :: Assigning filename with path to the picked photos
    FOR %%A IN ("!_rand1!") DO set "file1=.\bg\!fol1[%%~A]!"
    FOR %%B IN ("!_rand2!") DO set "file2=.\frame\!fol2[%%~B]!"
    FOR %%C IN ("!_rand3!") DO set "file3=.\people\!fol3[%%~C]!"
    FOR %%D IN ("!_rand4!") DO set "file4=.\box\!fol4[%%~D]!"
    
    :: Assign output file numbering based on FOR iteration number
    set "fileoutput=output-%%~x.png"
    
    :: Final Magic
    magick convert !file1! !file2! -composite !file3! -composite !file4! -composite !fileoutput!
    
)

输出样本:

sample output of files

我不会说 "Windows",但可以给你半个答案,你或其他人可以将其扩展成有用的东西。

您需要 运行 的基本 ImageMagick 命令是:

magick BACKGROUND.PNG OVERLAY1.PNG OVERLAY2.PNG -compose over -composite RESULT.PNG

如果要在叠加之前将它们全部调整为相同大小,请使用:

magick BACKGROUND.PNG OVERLAY1.PNG OVERLAY2.PNG -resize 800x600! -compose over -composite RESULT.PNG

如果您想在叠加之前单独调整它们的大小,请使用:

magick BACKGROUND.PNG ( OVERLAY1.PNG -resize 400x300 ) ( OVERLAY2.PNG -resize 100x100 ) -compose over -composite RESULT.PNG

所以用你的一些图像试试看,看看你得到了什么。使用您的代码和代表性示例图像更新您的问题。

作为答案重新发布:

echo OFF
cls
setlocal enableDelayedExpansion

set /a folder1=1
for %%A in (./bg/*.png) do (set fol1[!folder1!]=%%~A
set /a folder1+=1 )

set /a folder2=1
for %%B in (./frame/*.png) do (set fol2[!folder2!]=%%~B
set /a folder2+=1 )

set /a folder3=1
for %%C in (./people/*.png) do (set fol3[!folder3!]=%%~C
set /a folder3+=1 )

set /a folder4=1
for %%D in (./box/*.png) do (set fol4[!folder4!]=%%~D
set /a folder4+=1 )


set totaloutput=10

for /l %%x in (1, 1, %totaloutput%) do (

    :: Assigning random number to pick photos from array
    set /a "_rand1=(!RANDOM!* (%folder1%-1) /32768)+1"
    set /a "_rand2=(!RANDOM! * (%folder2%-1) /32768)+1"
    set /a "_rand3=(!RANDOM! * (%folder3%-1) /32768)+1"
    set /a "_rand4=(!RANDOM! * (%folder4%-1) /32768)+1"
    
    :: Assigning filename with path to the picked photos
    FOR %%A IN ("!_rand1!") DO set "file1=.\bg\!fol1[%%~A]!"
    FOR %%B IN ("!_rand2!") DO set "file2=.\frame\!fol2[%%~B]!"
    FOR %%C IN ("!_rand3!") DO set "file3=.\people\!fol3[%%~C]!"
    FOR %%D IN ("!_rand4!") DO set "file4=.\box\!fol4[%%~D]!"
    
    :: Assign output file numbering based on FOR iteration number
    set "fileoutput=output-%%~x.png"
    
    :: Final Magic
    magick convert !file1! !file2! -composite !file3! -composite !file4! -composite !fileoutput!
    
)