Fundamentals 11 min read

Create a Custom Panda Emoji with Python: Step‑by‑Step OpenCV & Pillow Tutorial

This tutorial walks you through building a cute panda‑head meme by importing, resizing, binarizing, rotating, and compositing foreground photos onto a background image using OpenCV and Pillow, then adding English or Chinese text and saving the final emoji.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Create a Custom Panda Emoji with Python: Step‑by‑Step OpenCV & Pillow Tutorial

Project Overview

In everyday life we often collect funny photos of friends; this project uses a cute panda head as a background and places friends' photos on it to create a meme, as shown in the example images.

Implementation Steps

Import the foreground photo (friend's picture).

Resize, rotate, and pad the foreground as needed.

Import the panda‑head background image.

Combine foreground and background to form the emoji.

Add text below the emoji.

Python Implementation

1. Import Required Libraries

import cv2
import numpy as mp
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont

The project mainly uses OpenCV; Pillow is required for adding Chinese text.

2. Helper Plot Function

def plt_show(img):
    imageRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(imageRGB)
    plt.show()

3. Load Foreground Photo

image = cv2.imread('SXC.jpg', 0)  # load as grayscale
plt_show(image)

4. Proportionally Resize Foreground

The foreground is larger than the background, so we scale it down by 0.3.

image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_CUBIC)
plt_show(image_resize)

5. Binarize Foreground

Pixels > 80 become 255; others become 0.

ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)
plt_show(image_binary)

6. Extract Region of Interest (ROI)

image_roi = image_binary[74:185, 0:150]
plt_show(image_roi)

7. Rotate the ROI

The background is upright while the foreground is slightly tilted; we rotate it ~15° counter‑clockwise.

rows, cols = image_roi.shape
M = cv2.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 15, 1)
image_rotate = cv2.warpAffine(image_roi, M, (140, 130))
plt_show(image_rotate)

8. Remove Unwanted Black Areas

We fill unnecessary regions with white using cv2.fillPoly.

h, w = image_rotate.shape
image_rotate_copy = image_rotate.copy()
pts1 = np.array([[0, 20], [64, 0], [0, 0]], np.int32)
pts2 = np.array([[0, 18], [0, h], [80, h]], np.int32)
pts3 = np.array([[0, 100], [0, h], [w, h], [w, 100]], np.int32)
pts4 = np.array([[111, 0], [w, 0], [w, 30]], np.int32)
pts5 = np.array([[124, 0], [115, h], [w, h]], np.int32)
pts6 = np.array([[120, 40], [95, 100], [120, 100]], np.int32)
foreground = cv2.fillPoly(image_rotate_copy, [pts1], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts2], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts3], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts4], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts5], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts6], (255, 255, 255))
plt_show(foreground)

9. Extract ROI Again and Upscale

foreground_roi = foreground[0:93, 0:125]
plt_show(foreground_roi)
foreground_roi_resize = cv2.resize(foreground_roi, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC)
plt_show(foreground_roi_resize)

10. Load Background Image

background = cv2.imread('back.jpg', 0)
plt_show(background)

11. Combine Foreground and Background

h_f, w_f = foreground.shape
h_b, w_b = background.shape
left = (w_b - w_f) // 2
right = left + w_f
top = 100
bottom = top + h_f
emoji = background
emoji[top:bottom, left:right] = foreground
plt_show(emoji)

12. Add Text Below the Emoji

12.1 English Text

OpenCV can directly draw English text.

emoji_copy = emoji.copy()
cv2.putText(emoji_copy, "FXXK!!", (210, 500), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 0), 5)
plt_show(emoji_copy)

12.2 Chinese Text

For Chinese characters we use Pillow.

PilImg = Image.fromarray(emoji)  # cv2 → PIL
draw = ImageDraw.Draw(PilImg)
ttfront = ImageFont.truetype('simhei.ttf', 34)
draw.text((210, 450), "你瞅啥!!", fill=0, font=ttfront)
emoji_text = cv2.cvtColor(np.array(PilImg), cv2.COLOR_RGB2BGR)  # PIL → cv2
plt_show(emoji_text)

13. Save the Emoji

cv2.imwrite('./emoji.png', np.array(emoji_text))

Full Source Code

import cv2
import numpy as mp
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont

def plt_show(img):
    imageRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(imageRGB)
    plt.show()

image = cv2.imread('SXC.jpg', 0)  # foreground
image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_CUBIC)
ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)
image_roi = image_binary[74:185, 0:150]
rows, cols = image_roi.shape
M = cv2.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 15, 1)
image_rotate = cv2.warpAffine(image_roi, M, (140, 130))
# fill unwanted areas
h, w = image_rotate.shape
image_rotate_copy = image_rotate.copy()
pts1 = np.array([[0, 20], [64, 0], [0, 0]], np.int32)
pts2 = np.array([[0, 18], [0, h], [80, h]], np.int32)
pts3 = np.array([[0, 100], [0, h], [w, h], [w, 100]], np.int32)
pts4 = np.array([[111, 0], [w, 0], [w, 30]], np.int32)
pts5 = np.array([[124, 0], [115, h], [w, h]], np.int32)
pts6 = np.array([[120, 40], [95, 100], [120, 100]], np.int32)
foreground = cv2.fillPoly(image_rotate_copy, [pts1], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts2], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts3], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts4], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts5], (255, 255, 255))
foreground = cv2.fillPoly(image_rotate_copy, [pts6], (255, 255, 255))
foreground_roi = foreground[0:93, 0:125]
foreground_roi_resize = cv2.resize(foreground_roi, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC)
background = cv2.imread('back.jpg', 0)
# combine
h_f, w_f = foreground_roi_resize.shape
h_b, w_b = background.shape
left = (w_b - w_f) // 2
right = left + w_f
top = 80
bottom = top + h_f
emoji = background
emoji[top:bottom, left:right] = foreground_roi_resize
# add Chinese text via Pillow
PilImg = Image.fromarray(emoji)
draw = ImageDraw.Draw(PilImg)
ttfront = ImageFont.truetype('simhei.ttf', 34)
draw.text((210, 450), "你瞅啥!!", fill=0, font=ttfront)
emoji_text = cv2.cvtColor(np.array(PilImg), cv2.COLOR_RGB2BGR)
cv2.imwrite('./emoji.png', np.array(emoji_text))
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.

PythonImageProcessingOpenCVComputerVisionMemeGeneration
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.