August 15, 2021
I spoke too soon.
That self-creating art gallery page was great, but the amount of images, and the default sizes are CRAZY big.
Therefore, I had to incorporate creating thumbnails into the logical process as well.
I tried many, many, many different attempts to resize the GIF’s without success.
I have no idea how to resize the GIF’s. I tried the ‘resize2gif’ library, and even tried to follow two vague Stackoverflow posts to manually hack the module’s code to work with Python 3 without matrix array errors. Ultimately, its not worth the hassle.
I’ll just have to figure out the GIF’s portion section another time.
Most likely, I will have to take the first frame of a given GIF, save that as an image, and then repeat the process of creating thumbnails from those images.
Gallery page found here:
Latest code attempt here:
import os
from pathlib import Path
from pathlib import PurePath
from pathlib import PosixPath
import pprint
import itertools
from wand.image import Image as wand_image
import wand
def create_thumbnails():
print('CALLING create_thumbnails() FUNCTION...')
art_gallery_path = '/var/www/musimatic/images/ArtGallery'
os.chdir(art_gallery_path)
picture_directories = list(filter(os.path.isdir, os.listdir(art_gallery_path)))
for directory in picture_directories:
print('Checking for thumbnails directory')
thumbs_path = str('/var/www/musimatic/images/ArtGallery/' + str(directory) + '/thumbs')
print('thumbs_path: ' + str(thumbs_path))
# Check if a thumbnails directory exist
thumbs_path_exists = Path(thumbs_path).exists()
if thumbs_path_exists:
print('thumbs_path_exists is true: thumbnail directory exists')
# if not thumbails directory:
if not thumbs_path_exists:
print('thumbs_path_exists is false: thumbnail directory does NOT exist')
# mkdir thumbnails
# https://csatlas.com/python-create-directory/
Path(thumbs_path).mkdir()
# Create globs for each file type
picture_paths_jpg = (x.resolve() for x in Path(directory).glob("*.jpg"))
picture_paths_png = (x.resolve() for x in Path(directory).glob("*.png"))
picture_paths = itertools.chain(picture_paths_jpg, picture_paths_png)
picture_paths_strings = [str(p) for p in picture_paths]
# Cycle through each picture_path string
print('Cycling through each picture_path string')
for picture_path in picture_paths_strings:
# Use PosixPath() to split path parts accordingly
current_filename = PosixPath(picture_path).name
current_stem = PosixPath(picture_path).stem
current_parent = PosixPath(picture_path).parent
print('current_filename: ' + str(current_filename))
print('current_stem: ' + str(current_stem))
print('current_parent: ' + str(current_parent))
thumb_image_version = str(str(current_parent) + '/thumbs/thumb_' + current_filename)
# https://www.geeksforgeeks.org/python-check-if-a-file-or-directory-exists/
thumb_image_version_exists = Path(thumb_image_version).exists()
print('thumb_image_version: ' + str(thumb_image_version))
print('thumb_image_version_exists: ' + str(thumb_image_version_exists))
# if not thumbnails/image.ext:
if not thumb_image_version_exists:
print('Creating new thumbnail image...')
# create_thumbnail(path_to_image, thumbnail_path)
# with Image(filename = picture_path) as image:
# https://www.geeksforgeeks.org/wand-thumbnail-function-python/
with wand_image(filename = picture_path) as image:
with image.clone() as thumbnail:
thumbnail.thumbnail(50, 50)
thumbnail.save(filename=thumb_image_version)
def create_thumbnails_gifs():
print('CALLING create_thumbnails() FUNCTION...')
art_gallery_path = '/var/www/musimatic/images/ArtGallery'
os.chdir(art_gallery_path)
picture_directories = list(filter(os.path.isdir, os.listdir(art_gallery_path)))
for directory in picture_directories:
print('Checking for thumbnails directory')
thumbs_path = str('/var/www/musimatic/images/ArtGallery/' + str(directory) + '/thumbs')
print('thumbs_path: ' + str(thumbs_path))
# Check if a thumbnails directory exist
thumbs_path_exists = Path(thumbs_path).exists()
if thumbs_path_exists:
print('thumbs_path_exists is true: thumbnail directory exists')
# if not thumbails directory:
if not thumbs_path_exists:
print('thumbs_path_exists is false: thumbnail directory does NOT exist')
# mkdir thumbnails
Path(thumbs_path).mkdir()
# Create globs for each file type
picture_paths_gif = (x.resolve() for x in Path(directory).glob("*.gif"))
picture_paths = itertools.chain(picture_paths_gif)
picture_paths_strings = [str(p) for p in picture_paths]
# Cycle through each picture_path string
print('Cycling through each picture_path string')
for picture_path in picture_paths_strings:
# Use PosixPath() to split path parts accordingly
current_filename = PosixPath(picture_path).name
current_stem = PosixPath(picture_path).stem
current_parent = PosixPath(picture_path).parent
print('current_filename: ' + str(current_filename))
print('current_stem: ' + str(current_stem))
print('current_parent: ' + str(current_parent))
thumb_image_version = str(str(current_parent) + '/thumbs/thumb_' + current_filename)
thumb_image_version_exists = Path(thumb_image_version).exists()
print('thumb_image_version: ' + str(thumb_image_version))
print('thumb_image_version_exists: ' + str(thumb_image_version_exists))
# if not thumbnails/image.ext:
if not thumb_image_version_exists:
print('Creating new thumbnail gif image...')
# Taken from this SO post:
# https://stackoverflow.com/questions/9988517/resize-gif-animation-pil-imagemagick-python
frames = images2gif.readGif(picture_path,False)
for frame in frames:
frame.thumbnail((100,100), Image.ANTIALIAS)
images2gif.writeGif(thumb_image_version, frames)
def main():
print('CALLING main() FUNCTION...')
with open('/var/www/musimatic/pythonprojectwebsites/ArtGallery/artgallery.html', 'w') as f:
f.write('<!DOCTYPE html>')
f.write('<html>')
f.write('<head>')
f.write('<title>Art Gallery</title>')
f.write('<meta charset="utf-8"/>')
f.write('<link rel="stylesheet" href="https://musimatic.xyz/css/artgallery.css" type="text/css"/>')
f.write('</head>')
f.write('<body>')
art_gallery_path = '/var/www/musimatic/images/ArtGallery'
os.chdir(art_gallery_path)
picture_directories = list(filter(os.path.isdir, os.listdir(art_gallery_path)))
for directory in picture_directories:
picture_directory_anchor = str('<a href="#' + str(directory) + '">' + str(directory) + '</a>')
f.write(picture_directory_anchor)
f.write('<br />')
print('WORKING ON CREATING IMG TAGS')
for directory in picture_directories:
picture_directory_header = str('<h1 id="' + str(directory) + '">' + str(directory) + '</h1>')
f.write(picture_directory_header)
f.write('<br />')
# SO Post on Globs:
# https://stackoverflow.com/questions/4568580/python-glob-multiple-filetypes
picture_paths_jpg = (x.resolve() for x in Path(directory).glob("*.jpg"))
picture_paths_png = (x.resolve() for x in Path(directory).glob("*.png"))
# TODO: Once I fix the 'create_thumbnails_gifs()' function, return to this:
# picture_paths_gif = (x.resolve() for x in Path(directory).glob("*.gif"))
# picture_paths = itertools.chain(picture_paths_jpg, picture_paths_png, picture_paths_gif)
picture_paths = itertools.chain(picture_paths_jpg, picture_paths_png)
# SO Post on string replacement:
# https://stackoverflow.com/questions/9452108/how-to-use-string-replace-in-python-3-x
# picture_paths_strings = [str(p).replace('/var/www/musimatic/', 'https://musimatic.xyz/') for p in picture_paths]
picture_paths_strings = [str(p) for p in picture_paths]
# pprint.pprint(picture_paths_strings)
for picture_path in picture_paths_strings:
current_filename = PosixPath(picture_path).name
current_stem = PosixPath(picture_path).stem
current_parent = PosixPath(picture_path).parent
regular_image_version = str(picture_path).replace('/var/www/musimatic/', 'https://musimatic.xyz/')
thumb_image_version = str(str(current_parent) + '/thumbs/thumb_' + current_filename)
thumb_image_version = str(thumb_image_version).replace('/var/www/musimatic/', 'https://musimatic.xyz/')
print('thumb_image_version: ' + str(thumb_image_version))
picture_img_tag = str('<a target="_blank" href="' + str(regular_image_version) + '"><img src="' + str(thumb_image_version) + '"/></a>')
f.write(picture_img_tag)
f.write('</body>')
f.write('</html>')
print('ART GALLERY COMPLETE!')
if __name__ == '__main__':
create_thumbnails()
# create_thumbnails_gifs()
main()
~ Sam