<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Create a PDF file with Contact Sheets (montage of thumbnails) for all JPG files in a folder and each of its subfolders using an automated, batch method

Published on
18,851 Points
2,751 Views
6 Endorsements
Last Modified:
Joe Winograd, EE MVE 2015&2016
50+ yrs in computer industry. Everything from programming to sales. OS kernel dev on mainframes. CIO. Document imaging. EE MVE 2015 & 2016.
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 or montage.

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:

Create-Montage-PDF-from-JPGs.jpg
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"
SET OutputFile="Montage.pdf"
SET ThumbnailPixelSize="240x320+4+8>"
SET FilenameFontSize=12
SET ColumnsByRows=4x5
FOR /R %RootFolder% %%G in (.) DO (
  PUSHD %%G
  gm.exe montage -compress JPEG -font Arial -pointsize %FilenameFontSize% -tile %ColumnsByRows% -frame 6 -shadow -label %%t -geometry %ThumbnailPixelSize% *.jpg %OutputFile%
  POPD )

Open in new window



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:

Montage-PDF-page-240-320-4-8-shadow.jpg
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 option:

Montage-PDF-page-240-240-0-0-no-shadow.j
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%

Open in new window


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

Montage-240-320-4-8-shadow.pdf
Montage-240-240-0-0-no-shadow.pdf

 
6
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Join & Write a Comment

This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month