123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- # file and image manipulation
- import sys, os, time
- from PIL import Image, UnidentifiedImageError
- # index and access image by hash
- import io, hashlib, hmac
- # configuration file
- import toml
- class ImageData:
- def __init__(self, fn, path):
- self.filename = fn
- self.lastmodified = os.path.getmtime(path)
- pass
- def get_timestamp(self):
- timestamp = time.strftime(
- '%Y-%m-%d %H:%M:%S', time.localtime(self.lastmodified))
- return timestamp
- def show(self, f=sys.stderr):
- print(self.filename, file=f)
- print(self.get_timestamp(), file=f)
- pass
- pass
- def get_images(folder):
- images = []
- # check if files are images
- unverified = 0
- files = os.listdir(folder)
- for fi in files:
- path = "%s/%s" % (folder,fi)
- try:
- im = Image.open(path)
- im.verify()
- im.close()
- images.append(ImageData(fi, path))
- except FileNotFoundError:
- print(fi, " not found in directory ", folder)
- except: # UnidentifiedImageError or anything
- unverified += 1
- # report number of files that are not verified to be images?
- return images
- def get_shasum(imagefile):
- sm = str()
- with open(imagefile, "rb") as f:
- b = f.read()
- sm = hashlib.sha1(b).hexdigest()
- return sm
- def get_config(filename):
- config = dict()
- try:
- with open(filename, 'r') as f:
- config = toml.loads(f.read())
- except:
- pass
- return config
- # makes thumbnails
- # use PIL.Image.LANCZOS ( for downsampling?
- def downscale(image, source, config):
- if "height" not in config and "width" not in config:
- print("skipping - no thumnail sizes defined", file=sys.stserr)
- return
- if "folder" not in config:
- print("skipping - no folder destination", file=sys.stderr)
- dest = config["folder"]
- if os.path.exists("%s/%s" % (dest, image)):
- print("skipping - thumb already exists for ", image, file=sys.stderr)
- return
- width = 0
- height = 0
- im = None
- try:
- im = Image.open("%s/%s" % (source, image))
- width, height = im.size
- except:
- pass
- return
- if width == 0 or height == 0:
- print("error - image dimensions not initialized", file=ssys.stderr)
- return
- if "height" in config and "width" not in config:
- newheight = config["height"]
- ratio = newheight/height
- if ratio > 1:
- print("skipping - image will be smaller than thumbnail", file=sys.stderr)
- return
- newwidth = math.floor(width * ratio)
- print(newwidth, config["height"], file=sys.stderr)
- thumb = im.resize((newwidth,newheight))
- thumb.save("%s/%s" % (dest, image))
- elif "height" not in config and "width" in config:
- pass
- elif "height" in config and "width" in config:
- pass
- pass
- if __name__ == "__main__":
- cfg = get_config("config.toml")
- source = cfg["source"]
- images = get_images(source)
- gallery = dict()
- content = []
- elements = []
- # oldest to newest
- images.sort(key= lambda i: i.lastmodified, reverse=True)
- for image in reversed(images):
- image = i.filename
- path = "%s/%s" % (source, image)
- downscale(image, source, cfg["thumbnail"])
- pass
|