Isotropic resize with padding

From wenmin-wu/ds-skills · View interactive version

Isotropic resize with padding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Resize images preserving aspect ratio then zero-pad to a square to avoid distortion artifacts in face crops or object detection inputs

How to use this skill

Skill file

Isotropic Resize with Padding

Overview

Naively resizing a rectangular image to a square distorts aspect ratio, creating artifacts that confuse classifiers (especially for faces). Isotropic resize scales the image so the longer side matches the target size, then zero-pads the shorter side. This preserves proportions while producing a fixed-size square input for CNNs.

Quick Start

import cv2
import numpy as np

def isotropic_resize(img, size, interpolation=cv2.INTER_AREA):
    h, w = img.shape[:2]
    if w > h:
        new_w = size
        new_h = int(h * size / w)
    else:
        new_h = size
        new_w = int(w * size / h)
    resized = cv2.resize(img, (new_w, new_h), interpolation=interpolation)
    # Zero-pad to square
    canvas = np.zeros((size, size, 3), dtype=np.uint8)
    canvas[:new_h, :new_w] = resized
    return canvas

face_crop = isotropic_resize(face_crop, 224)

Workflow

  1. Compute the scaling factor from the longer side to the target size
  2. Resize both dimensions by this factor (shorter side will be < target)
  3. Create a zero-filled canvas of target size
  4. Place the resized image in the top-left corner
  5. Feed the padded square to the CNN

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.