Creating a Panda‑Head Meme with Python, OpenCV and Pillow
This tutorial demonstrates how to import, resize, threshold, rotate, and blend foreground photos with a panda‑head background using OpenCV and Pillow in Python, then add English or Chinese text and save the resulting meme image.
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 onto it to create a meme.
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 meme.
Add text below the meme.
Python Implementation
1. Import Required Libraries
import cv2
import numpy as mp
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFontThe project mainly uses OpenCV; Pillow is required for adding Chinese text.
2. Plotting Helper 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. Resize Foreground Proportionally
image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_CUBIC)
plt_show(image_resize)5. Binarize Foreground
ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)
plt_show(image_binary)6. Extract Region of Interest
image_roi = image_binary[74:185, 0:150]
plt_show(image_roi)7. Rotate Foreground
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
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 and Resize ROI Again
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 Meme
12.1 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
PilImg = Image.fromarray(emoji) # cv2 to 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 back to cv2
plt_show(emoji_text)13. Save the Meme
cv2.imwrite('./emoji.png', np.array(emoji_text))Full 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
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))Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
