Algorithm for drawing an outline or stroke around any alpha transparent image
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To draw an outline around a transparent image, you should treat the alpha channel as the true shape of the object. The most reliable algorithm is to expand that alpha mask outward, subtract the original mask, and then composite the colored ring underneath the source image.
The Core Idea: Outline the Alpha Mask
An image with transparency already tells you which pixels belong to the visible object. Fully transparent pixels are background, opaque pixels are part of the object, and partially transparent pixels describe soft edges.
Instead of trying to detect visual edges from RGB values, work from the alpha channel directly:
- Extract the alpha channel.
- Grow the alpha mask outward by the stroke width.
- Subtract the original mask from the grown mask.
- Fill the remaining ring with the outline color.
- Composite the original image over that outline.
This works on arbitrary shapes because it follows the actual silhouette, not a rectangle around the image.
Why Dilation Works
The “grow the mask” step is a standard image-processing operation called dilation. If a pixel is near an opaque pixel, dilation turns it on. Repeating that process or using a larger structuring element creates a thicker border.
If expanded_mask is the dilated alpha and original_mask is the original alpha, then the outline area is approximately:
That subtraction leaves only the outside ring. You can then tint that ring with any color and place the original image on top.
A Runnable Pillow Example
Python and Pillow make this straightforward. The example below loads a PNG, generates a black outline, and saves the result.
ImageFilter.MaxFilter behaves like a simple dilation for this use case. The alpha values spread outward, and the subtraction step isolates the stroke region.
Handling Soft Edges and Anti-Aliasing
Alpha-transparent artwork often contains partially transparent edge pixels for anti-aliasing. That is a good thing. If you operate on the alpha mask instead of a hard binary edge map, the final outline usually looks smoother.
For icons or game sprites, you may want a crisp outline. In that case, threshold the alpha channel first:
You can threshold before dilation if you want a harder silhouette. For painted artwork, keeping the original soft alpha often produces a more natural result.
Controlling Stroke Placement
The algorithm above creates an outer stroke. That is what most users expect, because it preserves the visible content of the original image.
If you want a centered stroke, things get trickier because part of the outline must overlap the original object. A common approximation is:
- Dilate the mask outward.
- Erode the mask inward.
- Use the difference between them as the stroke band.
For many UI and sprite workflows, an outer stroke is simpler and avoids eating into the original art.
A Simple Numpy Version
If you are working in a custom rendering pipeline, the same idea applies outside Pillow. The following sketch shows the logic using arrays:
The important part is not the exact library. The important part is that the outline comes from geometric operations on the alpha mask.
Common Pitfalls
One common mistake is detecting edges from RGB colors while ignoring transparency. That fails on sprites with transparent padding or internal color changes. Start from alpha instead.
Another issue is forgetting to add padding around the image before expanding the mask. If the source object touches the canvas edge, the stroke can get clipped. Allocate a slightly larger canvas when necessary.
A third problem is using a hard threshold on artwork that depends on soft transparency. That can make the outline jagged. Choose binary masks for crisp assets and grayscale alpha for smoother assets.
Summary
- The cleanest outline algorithm works from the alpha channel, not from RGB edges.
- Create the stroke by dilating the alpha mask and subtracting the original mask.
- Composite the colored outline underneath the original RGBA image.
- Threshold the alpha only when you want a harder, more pixelated edge.
- Add canvas padding if the object touches the border and the stroke gets clipped.
Related reading
- Algorithm for finding similar images
- Algorithm for finding similar images using an index
- Algorithm to compare two images
- Algorithm to find a square shape in an image?
- Algorithm for drawing line with thickness / width
- Algorithm for enumerating all possible ways a rectangle can be split into n smaller rectangles
- algorithm to trace border in 2D array
- Algorithmic question Best angle to view trees from fixed camera

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.