I have 2 folders which contain different number of image files, I want photoshop to combine them randomly using batch.

The first folder is Backgrounds which holds all the backgrounds, and the second folder is Data which holds all the images, I want photoshop to run over all the images inside Data and paste them over a random background image from Backgrounds folder.

The Backgrounds folder holds only 20 images of background and the Data folder holds over 2000 images.

How can I do that?

  • 1
    I believe you'll need a script for that: In Photoshop it's going to be quite simple one but you need to know the basics of scripting for PS. I believe it's also possible to do with imagemagick Commented Feb 18, 2021 at 9:49
  • Maybe you could use data sets and randomize the rows or cells in a spreadsheet application? Of course if you need to run it multiple times and needs to be randomized on every run, it could be too much work... or not?
    – Joonas
    Commented Feb 18, 2021 at 10:06
  • I knew randomizing excel rows was going to be easy but I wasn't so sure about just randomizing the rows of a single column. It turns out that is also pretty simple. You can follow this guide up to step 3 and instead select the column you want to randomize + the rand() column then choose Custom sort from the list. In the window choose to sort by the rand() column.
    – Joonas
    Commented Feb 18, 2021 at 10:19
  • the question is simple: what have you try? post some code please, we could help you better than now
    – Les Go
    Commented Feb 18, 2021 at 11:37

2 Answers 2


I tried to make your idea:

It is not fluid and you will have to improve it a little (and change all directories) but this is the idea I think:

echo OFF

: creating 2 directory where I will put the images mixed

md C:\Users\alessandro.golin\Desktop\union >nul 2>&1 
md C:\Users\alessandro.golin\Desktop\union\result >nul 2>&1 

    setlocal EnableDelayedExpansion
    cd C:\Users\alessandro.golin\Desktop\back

    : now create a loop to control files and save them in a vector
    set n=1
    for %%f in (*.*) do (
       set /A n+=1
       set "file[!n!]=%%f"

    : randomic function to decide what file it will take
    set /a dir=(%random%*13)/32768+1
    : put the file in the new directory 
    copy "!file[%dir%]!" C:\Users\alessandro.golin\Desktop\union

    : coping name and path (brutally)
    set srcback=!file[%dir%]!
    set backpath=C:\Users\alessandro.golin\Desktop\back\%srcback%
    : NOW, going to the other folder, same passages  
    cd C:\Users\alessandro.golin\Desktop\datah
    set n=1
    for %%f in (*.*) do (
       set /A n+=1
       set "file[!n!]=%%f"
    : modify the 8 with the number of your img
    set /a num=(%random%*8)/32768+1
    copy "!file[%num%]!" C:\Users\alessandro.golin\Desktop\union
    set srcdata=!file[%num%]!
    set datapath=C:\Users\alessandro.golin\Desktop\datah\%srcdata%
: -Then the unions of the images with imageMagick- 
: (In my opinion, it is the most useful kit for doing this type of automatic operation) 

magick convert %backpath% %datapath% -gravity Center -geometry 256x256+30+5 -composite -resize 512x512 

: if you give to your program a cute name and you want to display it like if it is a chat,
: add %~n0: (it is not the real use of %~n0: but a sort of :|)
echo %~n0: bye :)

For uses of imageMagick see the composite command paragraph
Hope you like it :)

P.S: Someone know how to add the batch language identifier?


So I found easier solution in node.js using pic-generator:

const fs = require('fs')
const { createCanvas, loadImage } = require('canvas')
const { forEach } = require('lodash');
const imagesDir = './pics/images/';
const bgDir = './pics/backgrounds/';

const width = 500
const height = 500

let bgs = [];
let images = [];

fs.readdir(imagesDir, (err, files) => {
    images = files;
    fs.readdir(bgDir, (err, files2) => {
        bgs = files2;

        let bgCount = 0;

        forEach(images, (img) => {
            const canvas = createCanvas(width, height)
            const context = canvas.getContext('2d');
            context.fillStyle = '#fff'
            context.fillRect(0, 0, width, height);

            const imageToEdit = `${imagesDir}${img}`
            const bgIdx = bgCount % bgs.length;
            const bgImg = `${bgDir}${bgs[bgIdx]}`

            loadImage(imageToEdit).then(image => {
                loadImage(bgImg).then(bg => {
                    context.drawImage(bg, 0, 0, 500, 500)
                    context.drawImage(image, 0, 0, 500, 500)

                    const buffer = canvas.toBuffer('image/png')
                    fs.writeFileSync(imageToEdit, buffer)




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.