0

Can someone help me look into this? I'm trying to use Imagemagick to merge two images. One is a plain text, while the other one has a nice background. I have more than 12,000 plain text and 82 background images, so I have to construct two FOR loop to be able to pick both images randomly. But unfortunately, the second loop is not working for the background images. It only picks one and merge it with all plain text instead of random. The following is how I am implementing it.

for i in $(find /home/user/png -name "*.png"); do 
    for o in $(find /home/user/jpg -name "*.jpg"); do 
       composite -gravity Center $i $o "${i%.*}.jpg"; 
    done
done
1

2 Answers 2

1

For the fact that i have more than 12,000 plain text an 82 Background so i have to construct two FOR loop for to be able to pick both image randomly.

If the background and image file paths don't contain whitespace characters, then the following technique should work. Replace all the find ... appropriately.


You could put the 82 background files into an array:

backgrounds=($(find ...))

Define a function to pick a random background:

randombg() {
    local index
    ((index = RANDOM % ${#backgrounds[@]}))
    echo ${backgrounds[index]}
}

And then loop over the image files, and for each file, pick a random background:

find ... | while read image; do
   bg=$(randombg)
   composite -gravity Center $image $bg "${image%.*}.jpg"
done
Sign up to request clarification or add additional context in comments.

1 Comment

I can't thank you enough for helping me. This works like charm.
1

Using the shuf command to create an endless stream of shuffled *.jpg files, which within the for loop are input with read o:

shuf -r -e $(find /home/user/jpg -name "*.jpg") | 
for i in   $(find /home/user/png -name "*.png") ; do 
    read o
    composite -gravity Center "$i" "$o" "${i%.*}.jpg"
done 

Since there are no bash arrays, the above code is POSIX by default.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.