#!/usr/bin/python3 """ ====================================================================== thumbspage.py, version 1.3, Aug-8-16 Synopsis: Make an HTML thumbnail links page for an images folder. Requires: Any Python 3.X, plus 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 /examples, and learning-python.com/books/trnpix. License: Provided freely but with no warranties of any kind. Author: Copyright M. Lutz (learning-python.com), August, 2016. Given a folder having image files, generates a basic index page with thumbnail links for each image file in the folder. Skips non-image files, uses optional header and footer HTML inserts, makes an automatic bullet list for any subfolders in the images folder, and creates the output page in the images folder itself. As of 1.3, non-ASCII Unicode filenames and content are supported. To view results, open the output "index.html" (or other) index page created in your images folder. To publish the page, upload the entire images folder, including its generated "thumbs" (or other) subfolder, to your web server. Add custom HTML code to the top and bottom of the output file by placing it in files in the images folder named "HEADER.html" and "FOOTER.html" (both are optional). Otherwise, generic HTML and text is generated in the output file around the thumbs. You can also view the thumbs and images in GUI mode (without a web browser) using the PP4E book example included and used: c:\...\thumbspage> py -3.3 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. ====================================================================== Version history: 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: use window scroll) 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. 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. 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) A ".." parent link is not generated in automatic subfolder lists; should it be (the parent may or may not have images)? 4) Creating the index file in the image folder might preclude use of some page-generation tools (see trnpix template copies). 5) Styling of the table is open ended. For example, a
%s %s' % (colstyle, link, escname)) print(' |
This page was generated by ' 'thumbspage.py' '
') print('') sys.stdout.close()