search
HomeBackend DevelopmentPython TutorialGive you a Santa hat using Python

Christmas is coming. Although we can’t celebrate this Western festival, we still have to join in the fun. I believe there are already a lot of Christmas hat-related peripherals circulating. Today we will do it ourselves and add a Christmas hat to the avatar

Basic knowledge preparation

In computers, images are saved in the form of a matrix, rows first and columns second. Therefore, an image with width × height × color channel = 480 × 256 × 3 will be stored in a 256 × 480 × 3 three-dimensional tensor. Image processing is also calculated according to this idea (including image processing under OpenCV), that is, height × width × color channel.

Digital image

For a digital image, what we see is a real picture visible to the naked eye, but to the computer, this image is just a bunch of different brightnesses. point. An image of size M × N can be represented by an M × N matrix. The value of the matrix element represents the brightness of the pixel at this position. Generally speaking, the larger the pixel value, the brighter the point.

Generally speaking, grayscale images are represented by 2-dimensional matrices, and color (multi-channel) images are represented by 3-dimensional matrices (M × N × 3).

Image channel

Describes a pixel. If it is grayscale, then only one value is needed to describe it, which is a single channel. If a pixel has three colors, RGB, to describe it, it has three channels. A four-channel image is R, G, B plus an A channel, indicating transparency. Generally called alpha channel, indicating transparency.

ROI and mask

Setting Region of Interest (ROI), translated into vernacular as, setting the region of interest. Mask is an image masking process, which is equivalent to covering the parts we don't care about, leaving the ROI part. The alpha mentioned above can be used as a mask.

Matrix (Numpy) knowledge

Matrix indexing, slicing, etc., I don’t know much about them here, so I won’t go into details. Friends can learn by themselves.

Environment preparation

After we have the basic knowledge, let’s take a brief look at the code.

First install the OpenCV and dlib libraries you need to use, use pip to install them respectively

pip install python-opencv

pip install dlib

Then manually download the data model file shape_predictor_5_face_landmarks.dat from the Internet, address As follows: http://dlib.net/files/, download and put it in the project directory.

Interested students can play with shape_predictor_68_face_landmarks.dat, which recognizes as many as 68 key points on faces.

Give you a Santa hat using Python

Code processing

Hat processing

The first thing we have to do is to process the hat. The pictures we use are as follows

Give you a Santa hat using Python

First extract the rgb and alpha values ​​of the hat image

# 帽子Give you a Santa hat using Python
hat_img3 = cv2.imread("hat.png", -1)
r, g, b, a = cv2.split(hat_img3)
rgb_hat = cv2.merge((r, g, b))
cv2.imwrite("rgb_hat.jpg", rgb_hat)
cv2.imwrite("alpha.jpg", a)
print(a)
print(hat_img3.shape)
print(rgb_hat.shape)

The effect we get is as follows:

rgb image

Give you a Santa hat using Python

alpha graph

Give you a Santa hat using Python

The printed value of a is as follows:

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

Face detection

The following is face detection, using dlib processing.

# 人脸检测
dets = self.detector(img, 1)
x, y, w, h = dets[0].left(), dets[0].top(), dets[0].right() - dets[0].left(), dets[0].bottom() - dets[0].top()
# 关键点检测
shape = self.predictor(img, dets[0])
point1 = shape.parts()[0]
point2 = shape.parts(2)
# 求两点中心
eyes_center = ((point1.x + point2.x) // 2, (point1.y + point2.y) // 2)

The next step is to scale down the picture of the hat

# 帽子和人脸转换比例
hat_w = int(round(dets[0].right()/1.5))
hat_h = int(round(dets[0].bottom() / 2))
if hat_h > y:
hat_h = y - 1
hat_newsize = cv2.resize(rgb_hat, (hat_w, hat_h))
mask = cv2.resize(a, (hat_w, hat_h))
mask_inv = cv2.bitwise_not(mask)
dh = 0
dw = 0

bg_roi = img[y+dh-hat_h:y+dh,(eyes_center[0]-hat_w//3):(eyes_center[0]+hat_w//3*2)]

ROI extraction

Perform ROI extraction

# 用alpha通道作为mask
mask = cv2.resize(a, (resized_hat_w, resized_hat_h))
mask_inv = cv2.bitwise_not(mask)

mask variable takes out the area of ​​​​the hat.

Give you a Santa hat using Python

mask_inv variable is used to remove the area where the hat is installed in the face image.

Give you a Santa hat using Python

Next, take out the area (ROI) where the hat is installed in the face picture

# 原图ROI
# bg_roi = img[y+dh-resized_hat_h:y+dh, x+dw:x+dw+resized_hat_w]
bg_roi = img[y + dh - resized_hat_h:y + dh,
 (eyes_center[0] - resized_hat_w // 3):(eyes_center[0] + resized_hat_w // 3 * 2)]

Then next, in the face picture Take out the hat-shaped area

# 原图ROI中提取放帽子的区域
bg_roi = bg_roi.astype(float)
mask_inv = cv2.merge((mask_inv, mask_inv, mask_inv))
alpha = mask_inv.astype(float) / 255
# 相乘之前保证两者大小一致(可能会由于四舍五入原因不一致)
alpha = cv2.resize(alpha, (bg_roi.shape[1], bg_roi.shape[0]))
# print("alpha size: ",alpha.shape)
# print("bg_roi size: ",bg_roi.shape)
bg = cv2.multiply(alpha, bg_roi)
bg = bg.astype('uint8')

Here is to convert the default uint8 type of the picture into a float type for operation, and finally convert it back.

Synthesized picture

Give you a Santa hat using Python

The black part is where we want to place the hat.

Extract the hat part from the hat picture.

# 提取帽子区域
hat = cv2.bitwise_and(resized_hat, resized_hat, mask=mask)

Use the hat image you just resized to extract.

Give you a Santa hat using Python

可以看到,除了帽子部分,其他区域已经掩模处理了。

以上就是提取ROI的过程,比较难懂,需要好好琢磨,尤其是矩阵的切片、mask处理部分。

合成Give you a Santa hat using Python

最后一步就是把人脸Give you a Santa hat using Python与帽子合成到一起了,也就是把人脸空余帽子部分的Give you a Santa hat using Python区域和帽子只展示帽子区域的Give you a Santa hat using Python区域(有点拗口)合并在一起。

# 相加之前保证两者大小一致(可能会由于四舍五入原因不一致)
hat = cv2.resize(hat, (bg_roi.shape[1], bg_roi.shape[0]))
# 两个ROI区域相加
add_hat = cv2.add(bg, hat)

效果如下:

Give you a Santa hat using Python

刚刚好,完美叠加Give you a Santa hat using Python。

最后把这个片段放回人脸原图中,展示Give you a Santa hat using Python

img[y+dh-hat_h:y+dh, (eyes_center[0]-hat_w//3):(eyes_center[0]+hat_w//3*2)] = add_hat

Give you a Santa hat using Python

美美的Give you a Santa hat using Python就出来啦!

我们再尝试几张不同的Give you a Santa hat using Python。

Give you a Santa hat using Python

Give you a Santa hat using Python

整体效果还不错哦,需要注意的是,在测试的时候,我们尽量选择人脸占比比较大的Give you a Santa hat using Python来合成,效果要好很多哦~

The above is the detailed content of Give you a Santa hat using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.