Creating Custom Stickers from PNG Images using OpenCV
Creating a custom sticker from an image, especially a PNG image, can be an exciting task. In this tutorial, we'll guide you through a step-by-step approach to create stickers from PNG images using Python and OpenCV. This can be a fun project or even a useful tool in graphic design.
Prerequisites
Make sure you have the following libraries installed:
- OpenCV (cv2)
- NumPy
- Pillow (PIL)
You can install them using pip:
pip install opencv-python numpy pillow
Script Breakdown
1. Reading the Image
We'll start by reading a PNG image and converting it into the RGBA color space to include the alpha channel.
def read_image(img_path):
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
return cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)
2. Extracting Alpha Channel
The alpha channel is responsible for transparency in an image. We'll extract this channel for further processing.
def extract_alpha_channel(img):
return img[:, :, 3]
3. Finding the Largest Contour
We'll find the largest contour in the alpha channel, and then apply GaussianBlur to smooth it.
def get_largest_contour(alpha_channel):
# Smoothing using GaussianBlur
smoothed = cv2.GaussianBlur(alpha_channel, (15, 15), 0)
contours_smoothed = cv2.findContours(smoothed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours_smoothed = contours_smoothed[0] if len(contours_smoothed) == 2 else contours_smoothed[1]
big_contour_smoothed = max(contours_smoothed, key=cv2.contourArea)
# Use the smoothed contour
peri = cv2.arcLength(big_contour_smoothed, True)
return cv2.approxPolyDP(big_contour_smoothed, 0.001 * peri, True)
4. Drawing the Contour
The contour is then drawn on a black background to create a mask.
def draw_filled_contour_on_black_background(big_contour, shape):
contour_img = np.zeros(shape)
cv2.drawContours(contour_img, [big_contour], 0, 255, -1)
return contour_img
5. Applying Dilation
We'll apply dilation to enlarge the white region in the mask, creating a smooth border around the image.
def apply_dilation(img):
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (30, 30))
return cv2.morphologyEx(img, cv2.MORPH_DILATE, kernel)
6. Applying Overlays
The mask is then overlaid onto the original image, and the alpha channel is applied to merge them together.
def apply_overlays(canvas, img, dilate):
alpha = np.expand_dims(img[:, :, 3], 2)
alpha = np.repeat(alpha, 3, 2)
alpha = alpha / 255
canvas[dilate == 255] = (255, 255, 255, 255)
canvas[:, :, 0:3] = canvas[:, :, 0:3] * (1 - alpha) + alpha * img[:, :, 0:3]
return canvas
7. Creating the Sticker
We'll combine all the previous steps to create the final sticker.
Creating custom stickers from PNG images is simple and fun with Python and OpenCV. You can further experiment with different shapes and sizes for dilation or use various contouring techniques to achieve different effects. Happy sticker making!
def create_sticker(img):
alpha = extract_alpha_channel(img)
big_contour = get_largest_contour(alpha)
contour_img = draw_filled_contour_on_black_background(big_contour, alpha.shape)
dilate = apply_dilation(contour_img)
canvas = np.zeros(img.shape, dtype=np.uint8)
canvas = apply_overlays(canvas, img, dilate)
return canvas.astype(np.uint8)