#!/usr/bin/python3
"""
===========================================================================
thumbspage.py, version 1.4, Mar-4-2018
Synopsis: Make an HTML thumbnail links page for an images folder.
Requires: Any Python 3.X, plus the Pillow (PIL) image library
available at https://pypi.python.org/pypi/Pillow.
Launch: Run thumbspage.py, input run parameters in console.
See also option setting variables at start of code.
Examples: See learning-python.com/trnpix, and /examples here.
License: Provided freely but with no warranties of any kind.
Author: Copyright M. Lutz (learning-python.com), 2016-2018.
Overview:
Given a folder of image files, this script generates a basic index
page with thumbnail links for each image file in the folder. It skips
non-image files, uses optional header and footer HTML inserts, makes
an automatic bullet list for any subfolders in the images folder (not
named with a leading "_"), and creates the output page in the images
folder itself. As of 1.3, non-ASCII Unicode filenames and content are
supported. As of 1.4, its output page is more mobile-friendly.
To view results, open the output "index.html" index page created in
your images folder. To publish the page, upload the entire images
folder, including its generated "thumbs" subfolder and "index.html"
file, to the folder representing your page on your site's web-server
host; zip first for convenience. The names of the generated index
page and thumbs folder can both be changed in settings below.
For more custom behavior, add custom HTML code to the top and bottom
of the output page by placing it in files in the images folder named
"HEADER.html" and "FOOTER.html" respectively (both are optional).
If these files are not present, generic HTML and text is generated
in the output page around the thumbs table.
GUI mode:
You can also view the thumbs and images in GUI mode (i.e., without a
web browser) by using the PP4E book example included and used here
on Windows or Unix (Mac OS and Linux):
c:\...\thumbspage> py -3.3 viewer_thumbs.py examples\test\images
/.../thumbspage> python3 viewer_thumbs.py examples/test/images
This is a very basic GUI view, however; the book's later PyPhoto
example adds scroll bars to both the thumbs index and open images.
Update: PyPhoto is now available upgraded and standalone, at
learning-python.com/pygadgets.html.
===========================================================================
Version history:
1.4, Mar-4-18: restyle for mobile and improved table display.
This script's output page now better supports browsers on
smaller screens, and looks nicer in general. Its new CSS:
- Autoscrolls the thumbs table to appease mobile browsers;
- Adds padding to thumb cells to reduce run-together;
- Center-aligns thumbs images for a more even look; this
helps overall, but especially for narrow/portrait images;
- Uses nowrap paragraphs for image labels; the former
+wrap
scheme looked jumbled, and made Chrome (only!) arrange thumb
table columns unevenly in small windows (left was narrower).
This version also adds a mobile-friendly viewport tag
to default headers if "useViewPort." This may impact other
page components; use a custom HEADER.html for more options.
Tip: because filenames used on labels are now not wrapped,
their width largely determines column spacing in the index
page; use shorter filenames for less space between columns.
1.3, Aug-8-16: support non-ASCII Unicode filenames and content:
a) HTML-escape all added text - image, folder, subfolder names
b) URL-escape all added links - to thumbs, images, subfolders
c) Output the index file in UTF8 Unicode encoding by default,
with a content-type tag; ASCII content is unchanged,
as it is a subset of UTF8; other encodings may be used for
the output file via setting outputEncoding in the code
d) Load any header and footer inserts per UTF8 Unicode encoding
by default, as it is general and supports ASCII directly;
other encodings, including the platform's default, may be
used for inserts via setting insertEncoding in the code
e) Assume any inserts are both HTML-safe and UTF8-compatible
See examples\test\escapes\images for a Unicode test case.
1.2, Aug-1-16: add table styling options, per settings in code:
a) Uniform-width columns, not per content (_on_ by default)
b) Stretch thumbs table to fill whole window (_off_ by default)
c) Scrollbar if window too small? (skipped in 1.2, added in 1.4)
1.1, Jul-27-16: add auto subfolder-links list, per setting in code.
1.0, Jul-24-16: initial release.
===========================================================================
Notes:
1) (all) SCOPE: Apart from header/footer inserts, this script makes
fairly simplistic image and subfolder links only; edit either
the output HTML page or the code here that creates it as desired.
2) (1.1) SUBFOLDERS: Makes a bullet list for any subfolders in the
images folder (else they cannot be navigated to), but skips all
other non-image content; put any doctype, readme, font, CSS code,
etc., in header or footer. Run on each image tree subfolder manually;
automating this and other content seems too clever and complex.
3) (1.2) STYLING: Stretching the thumbs table to fill the whole window
is off by default, as it can appear too spread out for short names in
large windows. Enable this via the code setting below if desired.
4) (1.2) STYLING: A table overflow scrollbar was skipped, as it
appears only at table bottom, which can be very far away on large
pages. Instead, use the browser's automatic general window scroll.
==> changed in 1.4: now uses CSS autoscroll for thumbs table,
which is more mobile-friendly, and shows scroll iff needed.
5) (1.3) UNICODE: The script now fully supports images with non-ASCII
names, but getting them onto your web server may take special steps.
On my Linux server, an "unzip" of the WinZip zip package failed on the
Unicode test images, but GoDaddy's browser-based file upload worked.
See examples/test/escapes/readme.txt for more on uploading files.
See also learning-python.com/ziptools for a Python zip alternative.
6) (1.3) UNICODE: the insertEncoding setting now defaults to UTF-8,
as this is a general scheme which also works for ASCII files. To use
your platform's default encoding instead, set this variable to None,
or save your header/footer inserts in UTF-8 format.
7) (1.3) UNICODE: If (but only if) file or folder names are non-ASCII,
custom HEADER.html files must include a content-type tag with
a charset matching the outputEncoding (which is UTF-8 by default).
Default headers include this tag automatically (see the code).
8) (all) RELATED: See also pixindex, a related tool that adds images
to a zip file and FTPs them to a sever: learning-python.com/pixindex.
===========================================================================
Caveats and TBDs:
1) Python 3.X assumes local platform's Unicode encoding default
for header/footer inserts and the output page: change as needed.
==> addressed and resolved in 1.3, per version notes above
2) This script would probably run on Python 2.X too if the
viewer_thumbs module didn't import "tkinter" (a book legacy).
==> but no longer as is, as of 1.3's Unicode enhancements
3) Styling of the table is open ended. For example, a
%s %s | ' % (colstyle, link, labstyle, escname))
print('
This page was generated by ' 'thumbspage.py' '
') print('') sys.stdout.close()