SoFunction
Updated on 2024-11-16

Use Python to add a small Christmas hat or Santa Claus icon to an avatar with source code.

With Christmas approaching, would like to add a Santa hat to add to your avatar. If not an avatar, add a Santa to accompany it.

Adding Christmas hats to avatars with Python, and looking at probably all the articles from the big guns of 2017 as well:/p/32283641

Main Processes
material preparation
Face Detection and Face Keypoint Detection

Resize and add hats

Face detection is performed with dlib's positive face detector, and five key points of the face are extracted with the model provided by dlib

Adjust hat size with cap

The points at the corners of the two eyes are selected, and the center is sought as the reference coordinate in the x-direction for placing the hat, and the coordinate in the y-direction is represented by the y-coordinate on the upper line of the face frame. Then we adjust the size of the hat according to the size of the face obtained from the face detection, making the hat the right size.

# Pick the point at the corner of the left and right eye
    point1 = (0)
    point2 = (2)
    # Find the center of two points
    eyes_center = ((+)//2,(+)//2)
    # (img,eyes_center,3,color=(0,255,0)) 
    # ("image",img)
    # ()
    # Adjust hat size to face size
    factor = 1.5
    resized_hat_h = int(round(rgb_hat.shape[0]*w/rgb_hat.shape[1]*factor))
    resized_hat_w = int(round(rgb_hat.shape[1]*w/rgb_hat.shape[1]*factor))
    if resized_hat_h > y:
     resized_hat_h = y-1
    # Adjust hat size to face size
    resized_hat = (rgb_hat,(resized_hat_w,resized_hat_h))

Add small icons

Of course some students have avatars that are not characters or cannot be accurately identified as irrelevant, all added logos. (i.e. add small icon on the lower right).

Small icons avoid monotony by choosing one at random from the icons:

The code is as follows:

# Watermarked images
 num = (1, 5)
 logo = ("img_icon/santa_" + str(num) + ".png")
 
 img = (imgPath)
 
 print(, )
 
 # Layers
 layer = ("RGBA", , (255, 255, 255, 0))
 (logo, ([0] - [0], [1]-[1]))

 # Override
 img_after = (layer, img, layer)
 # img_after.show()
 img_after.save(outImgePath)

The results are as follows

Source Code:/silencesmile/Christmas_Hat

summarize

The above is a small introduction to the use of Python to the avatar with a Christmas hat or Santa Claus small icon with source code, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!