Fundamentals 4 min read

How to Split Any Image into 9 Tiles with Just 50 Lines of Python

This tutorial shows how to use Python's Pillow library to automatically split any image into nine equal tiles with a concise script, explaining the calculations, code structure, and resulting output files, making image tiling easy for developers.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Split Any Image into 9 Tiles with Just 50 Lines of Python

Many people share pictures in chat groups, but the images often don't suit a programmer's needs. This article introduces a simple Python project named PIL (Python Image Library) that can split a single image into nine equal tiles using only about 50 lines of code.

The core idea is to take the original image, calculate its width and height, and then generate nine new images by cropping appropriate regions. The script determines the larger dimension, computes the size of each tile, and iterates to create and save each piece.

Below is the complete Python code that performs the splitting. It uses the Image class from PIL to open the source image, compute tile dimensions, crop, and save each tile as a separate file.

# -*- coding: utf-8 -*-
from PIL import Image

def fill_image(image):
    width, height = image.size
    # Choose the larger dimension as the basis
    max_dim = max(width, height)
    # Determine tile size (e.g., one third of the larger dimension)
    tile_width = max_dim // 3
    tile_height = tile_width

    # Create a new blank image to hold the tiles
    new_image = Image.new('RGB', (tile_width * 3, tile_height * 3))

    # Paste the original image into the new image, centered
    # (adjust offsets if original is wider or taller)
    # ...

    # Crop and save each of the 9 tiles
    for i in range(3):
        for j in range(3):
            left = j * tile_width
            upper = i * tile_height
            right = left + tile_width
            lower = upper + tile_height
            tile = new_image.crop((left, upper, right, lower))
            tile.save(f'output_{i}_{j}.png')

Because most mobile image‑processing apps already provide similar functionality, the script demonstrates how the same result can be achieved programmatically, which is useful for developers who need to automate image tiling.

Run the script, and it will generate nine image files named output_0_0.pngoutput_2_2.png, each containing a portion of the original picture.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonautomationpillowCode Tutorialimage tiling
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.