Help with using imagemagick's montage in a bash script

56 views
Skip to first unread message

Michelle Rothrock

unread,
May 8, 2024, 1:51:16 PMMay 8
to Digital Curation
Hi all—

Hoping for some help with part of a bash script I'm trying to put together!

The script searches for video files recursively in a directory, creates thumbnails using ffmpeg, and then ideally would use those thumbnails to create a simple contact sheet. I'm not an expert at bash, so I've relied heavily on finding and repurposing bits of script from others.

I'm having two problems with imagemagick:
- One, passing it a list of files to compile into a contact sheet has been a little weird; I currently have a workaround which puts the thumbnail filenames into a .txt file (and then later deletes the .txt file). I feel sure there's some more straightforward way to do this
- Two, the -label '%f' option isn't working.

I'm copying the relevant portion of the script below. If anyone has any ideas—or if there's an existing solution to this entire thing I'm trying to do 🙈—I'd really appreciate it.

find "$video_dir" -type f -iname *-thumb.png > $video_dir/thumb_list.txt
read num_imgs <<< $(sed -n '$=' thumb_list.txt)
let num_rows=num_imgs/8
let lefto=num_imgs%8
if [ $lefto -gt 0 ]
then
   let num_rows=num_rows+1
fi
montage @$video_dir/thumb_list.txt -geometry 200x150 -label '%f' -tile 8x$nrow $video_dir/contact_sheet.png
rm $video_dir/thumb_list.txt

Esmé Cowles

unread,
May 8, 2024, 2:07:08 PMMay 8
to digital-...@googlegroups.com
Michelle-

You should be able to put the output of the find command into a variable with $( ), and the output of math operations with $(( )) which would look like:

thumbs=$( find "$video_dir" -type f -iname *-thumb.png )

num_imgs=$( echo $THUMBS | wc -w ) 

num_rows=$(( $num_imgs / 8 ))

lefto=$(( $num_imgs % 8 ))

if [ $lefto -gt 0 ]; then

   num_rows=$(( $num_rows + 1 ))

fi

montage $thumbs -geometry 200x150 -label '%f' -tile 8x$num_rows $video_dir/contact_sheet.png


That probably depends on not having spaces in the video filenames, though. If you do have spaces in the filenames, then writing to a file and counting the lines like you were doing is probably easier than getting the quoting to work (in my experience).

Hope this helps!

-Esmé
--
Esmé Cowles <esco...@princeton.edu>
Asst. Director, Library Software Engineering
Princeton University Library

--
You received this message because you are subscribed to the Google Groups "Digital Curation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to digital-curati...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/digital-curation/28a9bb4a-f988-401e-9da0-73fec499bc99n%40googlegroups.com.

Kam Woods

unread,
May 8, 2024, 2:33:44 PMMay 8
to digital-...@googlegroups.com
Re the issue with labels not appearing - the label flag must appear before the filenames, so

montage @$video_dir/thumb_list.txt -geometry 200x150 -label '%f' -tile 8x$nrow $video_dir/contact_sheet.png

becomes:

montage -label '%f' @$video_dir/thumb_list.txt -geometry 200x150 -tile 8x$nrow $video_dir/contact_sheet.png

--

Alex Chan

unread,
May 9, 2024, 7:19:14 AMMay 9
to Digital Curation
On Wednesday 8 May 2024 at 18:51:16 UTC+1 michelle...@gmail.com wrote:

One, passing it a list of files to compile into a contact sheet has been a little weird; I currently have a workaround which puts the thumbnail filenames into a .txt file (and then later deletes the .txt file). I feel sure there's some more straightforward way to do this

IMO putting the thumbnail filenames into a .txt file is fine.

I'd prioritise writing code you understand over something that's more "technically correct" but which you'll find harder to read later.
 
Two, the -label '%f' option isn't working.

Put the `-label` before the filenames:

montage -label '%f' @$video_dir/thumb_list.txt -geometry 200x150 -tile 8x$nrow $video_dir/contact_sheet.png

I'm copying the relevant portion of the script below. If anyone has any ideas—or if there's an existing solution to this entire thing I'm trying to do 🙈—I'd really appreciate it.

find "$video_dir" -type f -iname *-thumb.png > $video_dir/thumb_list.txt
read num_imgs <<< $(sed -n '$=' thumb_list.txt)
let num_rows=num_imgs/8
let lefto=num_imgs%8
if [ $lefto -gt 0 ]
then
   let num_rows=num_rows+1
fi
montage @$video_dir/thumb_list.txt -geometry 200x150 -label '%f' -tile 8x$nrow $video_dir/contact_sheet.png
rm $video_dir/thumb_list.txt

You don't need to work out the row count yourself. If you just pass `tile 8x`, then montage will work out the correct number of rows.

This allows you to simplify your code:

find "$video_dir" -type f -iname *-thumb.png > "$video_dir/thumb_list.txt"
montage -label '%f' "@$video_dir/thumb_list.txt" -geometry 200x150 -tile 8x "$video_dir/contact_sheet.png"
rm "$video_dir/thumb_list.txt"

You can see some more examples here: https://legacy.imagemagick.org/Usage/montage/

~ Alex

Alex Chan

unread,
May 9, 2024, 7:24:23 AMMay 9
to digital-...@googlegroups.com
(Sorry, I hit "send" on that hastily, and hadn't edited for tone!)

1. The thing about working out the row count is meant as a "hey here's
a thing that might be useful", not something you absolutely have to
do. Sorry if it read a bit curt or strong.
2. The specific section I meant to link in the montage docs is "Tile
Layout Controls", which has examples of the -tile flag:
https://legacy.imagemagick.org/Usage/montage/#tile
3. I didn't know about montage, but now I do and it looks like a fun
tool to add to my toolbox. Thanks Michelle, for letting me know about
it!

~ Alex
> --
> You received this message because you are subscribed to a topic in the Google Groups "Digital Curation" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/digital-curation/xuXjl0sxcnY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to digital-curati...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/digital-curation/d70deeee-cca6-47e0-8eb5-1a55c31b992fn%40googlegroups.com.

Michelle Rothrock

unread,
May 10, 2024, 10:15:22 AMMay 10
to digital-...@googlegroups.com
Thanks everyone for your help! Very much improved my code—still tweaking it, but it's close :)

With appreciation—
michelle



--
michelle rothrock

- - -  - - –  - – – -

Reply all
Reply to author
Forward
0 new messages