How to Overlay Images on a T-Shirt with Python OpenCV: A 2026 Guide

Learn to overlay images on t-shirts using Python OpenCV. This guide covers edge detection, resizing, and preserving shadows for realistic mockups.

How to Overlay Images on a T-Shirt with Python OpenCV: A 2026 Guide

How to Overlay Images on a T-Shirt with Python OpenCV: A 2026 Guide

In this tutorial, you'll learn how to overlay any custom image onto a blank t-shirt using Python's OpenCV library. This technique is useful for digital mockups and virtual fitting rooms, allowing designers and developers to visualize designs before printing them. We will cover essential steps like detecting the t-shirt area, resizing the image, and blending it while preserving shadows and wrinkles to ensure a realistic appearance.

Key Takeaways

  • Learn how to use OpenCV for image processing tasks.
  • Understand edge detection and masking techniques.
  • Preserve t-shirt shadows and wrinkles for realism.
  • Gain experience with image blending and transformations.
  • Apply these skills in digital fashion and design.

OpenCV, a powerful library for computer vision tasks, allows developers to perform complex image manipulations easily. By following this guide, you'll not only enhance your technical skills but also gain insights into practical applications of computer vision in the fashion industry.

Prerequisites

  • Basic understanding of Python programming.
  • Python 3.10 or above installed on your system.
  • OpenCV for Python installed (cv2 module).
  • Familiarity with image processing concepts is beneficial but not required.

Step 1: Install Dependencies

First, ensure that you have Python and OpenCV installed. You can install OpenCV via pip if you haven't already:

pip install opencv-python

Ensure other necessary libraries are installed:

pip install numpy

Step 2: Load and Display the Images

We'll start by loading the t-shirt image and the image you want to overlay:

import cv2
import numpy as np

# Load the t-shirt image
shirt_image = cv2.imread('blank_tshirt.jpg')
# Load the overlay image
overlay_image = cv2.imread('design.png')

# Display the original t-shirt image
cv2.imshow('T-Shirt', shirt_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Step 3: Detect the T-Shirt Area

To overlay the image properly, we need to detect the area of the t-shirt. We'll use edge detection and masking for this:

# Convert t-shirt image to grayscale
gray_shirt = cv2.cvtColor(shirt_image, cv2.COLOR_BGR2GRAY)

# Use Canny edge detection
tshirt_edges = cv2.Canny(gray_shirt, 50, 150)

# Display edges
cv2.imshow('Edges', tshirt_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

Detecting edges helps us identify the t-shirt boundaries, which we will use to create a mask.

Step 4: Create a Mask for the T-Shirt

Using the detected edges, we create a mask to isolate the t-shirt area:

# Find contours based on edges
contours, _ = cv2.findContours(tshirt_edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Assuming the largest contour is the t-shirt
tshirt_contour = max(contours, key=cv2.contourArea)

# Create a mask with the same dimensions as the t-shirt image
mask = np.zeros_like(gray_shirt)

# Draw the contour on the mask
cv2.drawContours(mask, [tshirt_contour], -1, (255), thickness=cv2.FILLED)

# Display mask
cv2.imshow('T-Shirt Mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

Step 5: Resize and Position the Overlay Image

With the mask, we can now resize and position the overlay image to fit the t-shirt:

# Resize overlay to fit within the t-shirt area
x, y, w, h = cv2.boundingRect(tshirt_contour)
resized_overlay = cv2.resize(overlay_image, (w, h))

# Create a region of interest (ROI) on the t-shirt image
roi = shirt_image[y:y+h, x:x+w]

# Apply the resized overlay on the ROI
overlay_gray = cv2.cvtColor(resized_overlay, cv2.COLOR_BGR2GRAY)
_, overlay_mask = cv2.threshold(overlay_gray, 1, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(overlay_mask)

shirt_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
overlay_fg = cv2.bitwise_and(resized_overlay, resized_overlay, mask=overlay_mask)

combined = cv2.add(shirt_bg, overlay_fg)
shirt_image[y:y+h, x:x+w] = combined

# Display final result
cv2.imshow('Final Overlay', shirt_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here, we resize the overlay image to fit the bounding box of the detected t-shirt contour and blend it using masks to preserve the background details.

Step 6: Fine-Tune the Blending for Realism

To ensure the overlay looks natural, we might need to adjust blending techniques or add filters to simulate shadows:

# Optionally, add a Gaussian blur on the overlay edges to soften them
blurred_overlay = cv2.GaussianBlur(combined, (5, 5), 0)
shirt_image[y:y+h, x:x+w] = blurred_overlay

# Display the final, refined result
cv2.imshow('Final T-Shirt with Design', shirt_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

By fine-tuning, you can adjust how the overlay interacts with the t-shirt's natural shadows and wrinkles, enhancing realism.

Common Errors/Troubleshooting

  • Image Not Displaying: Ensure file paths are correct and images are in the expected format.
  • Overlay Misaligned: Check the contour detection and resizing logic. Adjust bounding box calculations if needed.
  • Mask Issues: If the mask does not fit the t-shirt correctly, verify contour selection logic and thresholding values.

By following these steps, you can effectively overlay images on a t-shirt using Python OpenCV, creating a digital mockup that is both realistic and useful for design purposes. This knowledge can be applied in various sectors, including fashion design and e-commerce.

Frequently Asked Questions

Can this method be used for other clothing items?

Yes, the same principles can be applied to any clothing item, but you may need to adjust the contour detection parameters.

How do I ensure the overlay image is correctly positioned?

Ensure the contour detection accurately identifies the t-shirt boundaries and adjust the bounding box as necessary.

What if the overlay looks unnatural?

Try adjusting blending techniques or using filters to better match the lighting and texture of the t-shirt.