Fundamentals 9 min read

Python Tutorial: Create a Panda‑Head Emoji by Merging Images with OpenCV and Pillow

This tutorial demonstrates how to import a foreground photo, resize, binarize, rotate, clean, and combine it with a panda‑head background using OpenCV, then add English and Chinese text with Pillow, and finally save the resulting emoji image.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Tutorial: Create a Panda‑Head Emoji by Merging Images with OpenCV and Pillow

The article explains a step‑by‑step process for generating a custom emoji image that places a friend's photo onto a panda‑head background using Python libraries OpenCV and Pillow.

Implementation steps :

Import the foreground photo.

Resize and rotate the foreground as needed.

Import the panda‑head background image.

Combine foreground and background to form the emoji.

Add English or Chinese text below the emoji.

Save the final image.

Python implementation

1. Import required libraries

<code>import cv2</code>
<code>import numpy as mp</code>
<code>import matplotlib.pyplot as plt</code>
<code>from PIL import Image, ImageDraw, ImageFont</code>

A helper function for displaying images:

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

2. Load and preprocess the foreground image

<code>image = cv2.imread('SXC.jpg', 0)  # load as grayscale</code>
<code>image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_CUBIC)</code>
<code>ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)</code>
<code>image_roi = image_binary[74:185, 0:150]</code>

3. Rotate the region of interest

<code>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))</code>

4. Remove unwanted black areas using cv2.fillPoly

<code>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))</code>

5. Extract and enlarge the cleaned foreground

<code>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)</code>

6. Load the background image and combine

<code>background = cv2.imread('back.jpg', 0)
<h_f, w_f = foreground_roi_resize.shape</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</code>

7. Add text

English text using OpenCV:

<code>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)</code>

Chinese text using Pillow:

<code>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)
plt_show(emoji_text)</code>

8. Save the final emoji

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

The complete script is provided at the end of the article, allowing readers to reproduce the meme generation process.

emojiPythonTutorialImageProcessingopencvpillow
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

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