Search code examples
bashimagemagick

Only cropping/appending a subset of images from an image sequence


I have a numbered image sequence that I need to crop and append, but only certain frame ranges.

Example, sequence of 100 images named as follows:

frame001.jpg
frame002.jpg
frame003.jpg
...

Sometimes might only need to crop and append images 20-30, or other time, 5-75.

How can I specify a range? Simply outputting to a PNG.


Solution

  • For examle, if you want to pick the jpg files in the range of 20-30 and generate a png file appending them, would you please try:

    #!/bin/bash
    
    declare -a input                                        # an array to store jpg filenames
    for i in $(seq 20 30); do                               # loop between 20 and 30
        input+=( "$(printf "frame%03d.jpg" "$i")" )         # append the filename one by one to the array
    done
    echo convert -append "${input[@]}" "output.png"         # generate a png file appending the files
    

    If the output command looks good, drop echo.

    If you are unsure how to run a bash script and prefer a one-liner, please try instead:

    declare -a input; for i in $(seq 20 30); do input+=( "$(printf "frame%03d.jpg" "$i")" ); done; echo convert -append "${input[@]}" "output.png"
    

    [Edit]
    If you want to crop the images with e.g. 720x480+300+200, then please try:

    #!/bin/bash
    
    declare -a input
    for i in $(seq 20 30); do
        input+=( "$(printf "frame%03d.jpg" "$i")" )
    done
    convert "${input[@]}" -crop 720x480+300+200 -append "output.png"
    

    The order of options and filenames doesn't matter here, but I have followed the modern style of ImageMagick usage to place the input filenames first.