Collecting my attempts to improve at tech, art, and life

2025-10-12

Does any of this enlighten, entertain, or otherwise please you? Please consider a Tip. Every little bit helps!

[2025-10-12 Sun 15:01]

Knocking 1GB off my note vault disk usage by resizing all images to fit in a 1600x900 container.

Used Python and Pillow for the task. Are there faster solutions? You betcha. After the first run applied to this vault, will it matter? Not really.

Ten minutes to write it—after an hour in which Ruby + libvips gave me confusing errors—two minutes or so for the first run, 0-5 seconds for every run from here on.

Choose your battles.

"""Ensure image assets in my primary vault fit in a 1600x900 container."""

import logging
import pathlib

import rich.logging
from PIL import Image, ImageOps

IMAGE_DIR = "~/doc/random-geekery/assets/img"
IMAGE_EXTENSIONS = (".png", ".jpeg", ".jpg", ".JPG")
MAX_WIDTH = 1600
MAX_HEIGHT = 900
CONTAINER_SIZE = (MAX_WIDTH, MAX_HEIGHT)

logging.basicConfig(level=logging.INFO, handlers=[rich.logging.RichHandler()])
logger = logging.getLogger(__name__)


def main():
    """Application entry point."""
    img_dir = pathlib.Path(IMAGE_DIR).expanduser()
    logger.info("scanning images: folder='%s'", img_dir)

    for path in img_dir.glob("**/*.*"):
        if path.suffix not in IMAGE_EXTENSIONS:
            continue

        img = Image.open(path)
        width, height = img.size

        if width > MAX_WIDTH or height > MAX_HEIGHT:
            logger.warning(
                "Resizing to container: name='%s', size=%s; container=%s",
                path.name,
                img.size,
                CONTAINER_SIZE,
            )
            ImageOps.contain(img, CONTAINER_SIZE).save(path)


if __name__ == "__main__":
    main()