This article provides the solution to a question
posed here at Experts Exchange. The asker of the question has many JPG
images in many folders, and all of the folders are subfolders of one root folder. For each folder, the asker wants to create a PDF
file containing thumbnail images (sized to 240 pixels wide) of every JPG in that folder. This is commonly known as a contact sheet
Let's begin the article with an illustration showing the results of this solution. Here's a Windows/File Explorer screenshot displaying a folder with four JPGs, which were the input files in a test run, and a single PDF, which was the resulting output file from the test run:
In that test run, all of the JPGs in the folder fit well on a single page in the output PDF file. But if there are too many images to fit on one page, the asker wants the resulting file to be a multi-page PDF. Also, the file name of each JPG should be shown below its thumbnail. All of this needs to be accomplished with a fully automated, unattended method, capable of being run by the Task Scheduler without user intervention.
One of the experts (Eirman
) participating in that question recommended a terrific (and free!) utility (Snap2IMG
) that can produce such contact sheets, but as a JPG file, not a PDF. Also, the utility presents a GUI
, which (Eirman noted) could likely be automated with a scripting language like AutoHotkey
, but it does not support command line
calls (except to provide the root folder).
So I decided to take the approach of using a product that was designed to be run from the command line, making it more amenable to an automated, batch file solution. The product is the excellent (and free!) GraphicsMagick
. I've already published an article at EE which explains how to download and install GraphicsMagick, so rather than repeating those instructions here, I refer you to that article:
Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method
After installing GraphicsMagick using the instructions in that article, the next step is to write the batch file that makes the appropriate command line call in order to create the multi-page PDF file with the thumbnail images — what GraphicsMagick calls a montage
. Borrowing some from my previous article, here's the batch file that accomplishes the task:
SET RootFolder="c:\root folder"
FOR /R %RootFolder% %%G in (.) DO (
gm.exe montage -compress JPEG -font Arial -pointsize %FilenameFontSize% -tile %ColumnsByRows% -frame 6 -shadow -label %%t -geometry %ThumbnailPixelSize% *.jpg %OutputFile%
Simply copy those nine lines of code into a batch (.BAT
) file and schedule it via Task Scheduler
(or, of course, run it manually or put it in the Startup
program group). Notes:
(1) There is no error check on the value of RootFolder — make sure it is valid and exists. (2) If a folder has no JPG files in it, the call to gm.exe
will fail, but the batch file will continue to run fine (recursing into all subfolders), allowing for unattended operation with no user intervention. (3) This solution will overwrite an existing output (montage) PDF file with no warning.
I created local variable names in the batch file that are largely self-documenting and should make it easy for anyone using this solution to configure the parameters: the root folder, the name of the output (montage) PDF file, the size in pixels of each thumbnail (and the spacing), the font size to use for the file name that is placed under each thumbnail, and the number of columns and rows of thumbnails appearing on each page of the PDF file.
One variable that deserves some explanation is ThumbnailPixelSize
. The four values (in pixels) are width of thumbnail, height of thumbnail, horizontal spacing between thumbnails, and vertical spacing between thumbnails (note:
the greater-than sign at the end of the four values is important — be careful to leave it if you change values). Thus, the initial value in the code above means that each thumbnail will be sized to 240 pixels wide and 320 pixels high (but the aspect ratio is maintained), while there will be four pixels between each column of thumbnails and eight pixels between each row of thumbnails. Here's what a montage of them looks like:
That's a screenshot of a page from the PDF file created by this solution.
The run that created it used the ThumbnailPixelSize parameter shown in the code posted above, that is, "240x320+4+8>
", and with the -shadow
option (discussed below). Likewise, here's a screenshot from a run against the same folder with the same set of JPGs, but with the ThumbnailPixelSize parameter set to "240x240+0+0>
", and without the -shadow
The quality of the actual PDF files is better than the screenshots shown above, and I have attached the two-page PDF from each run to corroborate that (the subfolder for this particular montage has 40 JPGs in it, so the 4-column by 5-row run created a two-page PDF).
In addition to the parameters discussed above, I chose some other parameters for the GraphicsMagick call: compress
the PDF file with JPEG compression, the font
of Arial, a frame
of six pixels (surrounds each thumbnail with an ornamental border), and a shadow
(adds a nice visual effect to each thumbnail). The JPEG compression results in a significant reduction in the size of the PDF file. Here are some test results:
PDF size in bytes without PDF size in bytes with the Compression
the -compress option -compress option set to JPEG Ratio
38,507 16,595 57%
741,142 208,090 72%
916,381 272,355 70%
1,556,917 307,080 80%
2,931,450 616,611 79%
5,255,587 1,235,601 76%
There are many other options available in the montage sub-command
— enjoy experimenting with them!
If you find this article to be helpful, please click the thumbs-up
icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe