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

Posted

技术标签:

【中文标题】如何在 Windows 中通过随机分层源图像来自动生成合成图像?【英文标题】:How can I automatically generate composite images by layering source images randomly in Windows? 【发布时间】:2021-11-05 00:29:29 【问题描述】:

您好,我很想知道如何从一组不同的文件夹中随机合成几张图片。我认为这是Mark Setchell 在此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

【问题讨论】:

【参考方案1】:

作为答案转发:

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!
    
)

【讨论】:

【参考方案2】:

我不会说“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

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

【讨论】:

感谢您用提示回复的手势,即使您不会说“Windows”。我的问题主要是 bat 命令脚本

以上是关于如何在 Windows 中通过随机分层源图像来自动生成合成图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Python 从文件夹中通过电子邮件发送随机 pdf 文件?

在 Java 中通过 XSLT 进行 XML 粉碎

如何在Windows中通过Cygwin安装和运行Nmap程序?

如何在Windows中通过命令行创建快捷方式?

如何在 Yii 中通过 FPDF 为 POS 软件设置自动页面高度

如何在 Windows Store App 中通过 Web API(在线服务)与 xaml UI 绑定 JSON 数据?