resize-images.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # file and image manipulation
  2. import sys, os, time
  3. from PIL import Image, UnidentifiedImageError
  4. # index and access image by hash
  5. import io, hashlib, hmac
  6. # configuration file
  7. import toml
  8. class ImageData:
  9. def __init__(self, fn, path):
  10. self.filename = fn
  11. self.lastmodified = os.path.getmtime(path)
  12. pass
  13. def get_timestamp(self):
  14. timestamp = time.strftime(
  15. '%Y-%m-%d %H:%M:%S', time.localtime(self.lastmodified))
  16. return timestamp
  17. def show(self, f=sys.stderr):
  18. print(self.filename, file=f)
  19. print(self.get_timestamp(), file=f)
  20. pass
  21. pass
  22. def get_images(folder):
  23. images = []
  24. # check if files are images
  25. unverified = 0
  26. files = os.listdir(folder)
  27. for fi in files:
  28. path = "%s/%s" % (folder,fi)
  29. try:
  30. im = Image.open(path)
  31. im.verify()
  32. im.close()
  33. images.append(ImageData(fi, path))
  34. except FileNotFoundError:
  35. print(fi, " not found in directory ", folder)
  36. except: # UnidentifiedImageError or anything
  37. unverified += 1
  38. # report number of files that are not verified to be images?
  39. return images
  40. def get_shasum(imagefile):
  41. sm = str()
  42. with open(imagefile, "rb") as f:
  43. b = f.read()
  44. sm = hashlib.sha1(b).hexdigest()
  45. return sm
  46. def get_config(filename):
  47. config = dict()
  48. try:
  49. with open(filename, 'r') as f:
  50. config = toml.loads(f.read())
  51. except:
  52. pass
  53. return config
  54. # makes thumbnails
  55. # use PIL.Image.LANCZOS ( for downsampling?
  56. def downscale(image, source, config):
  57. if "height" not in config and "width" not in config:
  58. print("skipping - no thumnail sizes defined", file=sys.stserr)
  59. return
  60. if "folder" not in config:
  61. print("skipping - no folder destination", file=sys.stderr)
  62. dest = config["folder"]
  63. if os.path.exists("%s/%s" % (dest, image)):
  64. print("skipping - thumb already exists for ", image, file=sys.stderr)
  65. return
  66. width = 0
  67. height = 0
  68. im = None
  69. try:
  70. im = Image.open("%s/%s" % (source, image))
  71. width, height = im.size
  72. except:
  73. pass
  74. return
  75. if width == 0 or height == 0:
  76. print("error - image dimensions not initialized", file=ssys.stderr)
  77. return
  78. if "height" in config and "width" not in config:
  79. newheight = config["height"]
  80. ratio = newheight/height
  81. if ratio > 1:
  82. print("skipping - image will be smaller than thumbnail", file=sys.stderr)
  83. return
  84. newwidth = math.floor(width * ratio)
  85. print(newwidth, config["height"], file=sys.stderr)
  86. thumb = im.resize((newwidth,newheight))
  87. thumb.save("%s/%s" % (dest, image))
  88. elif "height" not in config and "width" in config:
  89. pass
  90. elif "height" in config and "width" in config:
  91. pass
  92. pass
  93. if __name__ == "__main__":
  94. cfg = get_config("config.toml")
  95. source = cfg["source"]
  96. images = get_images(source)
  97. gallery = dict()
  98. content = []
  99. elements = []
  100. # oldest to newest
  101. images.sort(key= lambda i: i.lastmodified, reverse=True)
  102. for image in reversed(images):
  103. image = i.filename
  104. path = "%s/%s" % (source, image)
  105. downscale(image, source, cfg["thumbnail"])
  106. pass