Below is a compact but flexible Pillow‑based script that will take a very tall (“vertical”) image and slice it into a stack of 768‑pixel‑high tiles, each overlapping the previous one by a user‑defined amount.
Key points
• Keeps the full original width (good for comic pages, screenshots, etc.).
• Overlap is adjustable (default 128 px).
• Handles the last slice gracefully: you may either crop it “as‑is” or let the script pad it up to 768 px with any colour you like.
• Pure Python, only needs Pillow (pip install pillow).
#!/usr/bin/env python3
"""
split_vertical.py — slice a tall image into overlapping 768‑px chunks.
Author: <you>
License: MIT
"""
import os
import argparse
from PIL import Image
def split_vertical(img_path: str,
out_dir: str = "chunks",
chunk_h: int = 768,
overlap: int = 128,
pad_color: str | None = None) -> None:
"""
Split a tall image into overlapping vertical chunks.
Parameters
----------
img_path : str
Path to the input image.
out_dir : str
Where to write the chunks.
chunk_h : int
Height of each chunk in pixels.
overlap : int
Vertical overlap between neighbouring chunks.
pad_color : str | None
If set, the last (short) chunk will be padded to `chunk_h`
using this colour; otherwise it is left cropped.
"""
os.makedirs(out_dir, exist_ok=True)
img = Image.open(img_path)
width, height = img.size
stride = chunk_h - overlap # effective step
i = 0
for top in range(0, height, stride):
bottom = top + chunk_h
if bottom <= height: # a “full‑size” chunk
chunk = img.crop((0, top, width, bottom))
else: # final, possibly short chunk
top = max(0, height - chunk_h) # align to bottom
chunk = img.crop((0, top, width, height))
# optionally pad up to `chunk_h`
if pad_color is not None and chunk.size[1] < chunk_h:
padded = Image.new(img.mode, (width, chunk_h), color=pad_color)
padded.paste(chunk, (0, chunk_h - chunk.size[1]))
chunk = padded
out_name = f"{os.path.splitext(os.path.basename(img_path))[0]}_{i:03d}.png"
chunk.save(os.path.join(out_dir, out_name))
print(f"[+] Wrote {out_name}")
i += 1
def main() -> None:
parser = argparse.ArgumentParser(
description="Split a tall image into overlapping vertical chunks.")
parser.add_argument("image",
help="Path to the input image file.")
parser.add_argument("-o", "--outdir",
default="chunks",
help="Output directory (default: ./chunks)")
parser.add_argument("-s", "--size",
type=int, default=768,
help="Height of each chunk in pixels (default: 768)")
parser.add_argument("-ov", "--overlap",
type=int, default=128,
help="Vertical overlap between chunks (default: 128)")
parser.add_argument("--pad-color",
help="Pad colour for the last chunk (e.g. 'white', '#222222'); "
"omit to leave final tile uncropped.")
args = parser.parse_args()
split_vertical(args.image, args.outdir,
chunk_h=args.size,
overlap=args.overlap,
pad_color=args.pad_color)
if __name__ == "__main__":
main()
🍀 Tips & Variations
• You can change chunk_h to 512, 1024, or whatever suits your workflow.
• To slice an image that is wider than needed, replace the crop box with something like (left, top, left+chunk_w, bottom) and loop horizontally as well.
• Use --pad-color transparent on PNGs with an alpha channel if you want see‑through padding.