Reference library

Automation & scripting

CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.

4 matches
Automation & scripting easy

How to Batch Resize Images in Python with pathlib and Pillow

Batch resize all JPG images from a source folder and save to a destination folder using pathlib and Pillow.

pathlib pillow image-processing
Python
from pathlib import Path
from PIL import Image

def batch_resize_images(src_dir: str, dest_dir: str, size: tuple[int, int] = (800, 600)) -> None:
    src_path = Path(src_dir)
    dest_path = Path(dest_dir)
    dest_path.mkdir(parents=True, exist_ok=True)
    
    for img_path in src_path.glob("*.jpg"):
        if not …
12 0 Open
Automation & scripting easy

How to Generate Thumbnails While Maintaining Aspect Ratio in Python

Resize images to fit within maximum dimensions while preserving the original aspect ratio using Pillow (PIL).

pillow image-processing thumbnail
Python
from PIL import Image

def thumbnail_with_aspect_ratio(image_path, output_path, max_width, max_height):
    with Image.open(image_path) as img:
        # Get original dimensions
        width, height = img.size

        # Calculate scaling ratio to fit within max dimensions
        ratio = min(max_width / width, max_h…
13 0 Open
Automation & scripting easy

How to Resize Hundreds of Images in Batch with Python

Resize every image in a folder to a target size using Pillow, creating a new subfolder for processed files.

image processing batch processing pillow
Python
import os
from PIL import Image

def resize_images_in_batch(directory, output_size=(800, 600)):
    if not os.path.exists(directory):
        print(f"Directory {directory} does not exist.")
        return
    output_dir = os.path.join(directory, "resized")
    os.makedirs(output_dir, exist_ok=True)
    for filename in…
41 0 Open
Automation & scripting easy

Resize Disk Partitions in Python (Mock Script)

A mock disk partition resize script that uses dataclasses to model partitions, validate new sizes, and output the updated layout as JSON.

disk partition dataclass
Python
#!/usr/bin/env python3
"""Mock script to demonstrate disk partition resize logic."""
import json
from dataclasses import dataclass
from typing import Dict


@dataclass
class Partition:
    name: str
    size_gb: int
    mount_point: str

    def to_dict(self) -> Dict[str, object]:
        return {
            "name": …
16 0 Open

Browse by section

Each section groups closely related Python snippets.

Automation & scripting — Python code examples

What you will find here

This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.