Preparing Images

I have more than 5000 pictures taken with several different cameras. They do not have the same resolutions, the same naming scheme and the pictures are too large to be used as is in a gallery.

The following code will make the images more uniform:

  • read all image files from a given directory
  • transpose the image according to the EXIF tag
  • extract the DateTime from the EXIF info to construct a new filename. This only works if the pictures were taken at least 1s apart!
  • create a new image with the data, dropping all EXIF infos
  • resize the image so that the largest dimension will be 1280
  • save the image in the webp format

These images will then be used to make a gallery with gallerydeluxe.

#! /usr/bin/env python

import pathlib
from PIL import Image, ImageOps
from PIL.ExifTags import Base

dirs = ["src"]
dst = "all"

for dir in dirs:
    p = pathlib.Path(dir)
    for fname in p.iterdir():
        img = Image.open(fname)
        img = ImageOps.exif_transpose(img)
        date = img.getexif()[Base.DateTime]
        newfname = f"{dst}/{date.replace(':', '').replace(' ', '')}_{dir}.webp"
        res = Image.new(img.mode, img.size)
        res.putdata(list(img.getdata()))
        x, y = img.size
        if x > y:
            f = x/1280.0
            x = 1280
            y = int(y/f)
        else:
            f = y/1280.0
            y = 1280
            x = int(x/f)
        res = res.resize((x, y), Image.LANCZOS)
        res.save(newfname, 'webp')

2024-12-26


copyright 2025 mobilarte