Python and add to the picture Santa Claus Christmas hats or small badge

With the arrival of Christmas, I want to give their avatar plus a Christmas hat. If not avatar, add a Santa Claus companionship.

Christmas hats in Python to add head, looked at probably come from the 2017 Great God of the article:  https://zhuanlan.zhihu.com/p/32283641

 

 

 

The main flow

Material Preparations

Face Detection and Face Detection Key points

Resize, add a hat

Face detection using face detector dlib positive, the five key points with dlib provide model extraction face

# Dlib face critical point detector   
      Predictor \ _path = "the Shape \ _predictor \ _5 \ _face \ _landmarks.dat"   
      Predictor = dlib.shape \ _predictor (Predictor \ _path) # dlib positive face detector  Detector = dlib.get \ _frontal \ _face \ _detector () # = n face detection detector Dets (IMG, . 1) # If a face is detected IF len (Dets)> 0: for D in Dets: X, Y, W, H \ = d.left ( ), d.top (), d.right () - d.left (), d.bottom () - d.top () # X, Y, W, H = faceRect cv2.rectangle (IMG, (X, Y), (X + W, Y + H), ( 255, 0, 0), 2, . 8, 0) # key detection, key 5 = Predictor Shape (IMG, D) for point
  
in shape.parts(): cv2.circle(img,(point.x,point.y),3,color=(0,255,0)) cv2.imshow("image",img) cv2.waitKey()

Adjust the size of hat, cap

Select two corner points, find the coordinates of the center as the reference direction x hat is placed, the coordinate in the y direction lines on the face of the box employing y coordinates. Then we face detection based on the size of the person's face to get adjusted hat size, hat size makes the right.

# Select left and right eye canthus point   
              Point1 = shape.part ( 0)   
              Point2 = shape.part ( 2)   # find two center  Eyes \ _center = ((+ point1.x point2.x) //2,(point1.y point2.y +) // 2) # cv2.circle (img, Eyes \ _center, 3, Color = (0,255,0)) # cv2.imshow ( "Image", img) # cv2.waitKey () # according to people adjust the size of the face hat size = factor for 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 the y-= \ -1 # face resizing resized according to the size of the hat
  
\_hat = cv2.resize(rgb\_hat,(resized\_hat\_w,resized\_hat\_h))

Add small icons

Of course, some students of the picture or not a person can not accurately identify extraneous, all add logo. (Ie add a small icon in the lower right).

Small icon avoid monotony, is selected from a random icons:

Icon positions can also adjust the size and location depending on preference

code show as below:

# 水印图片  
    num = random.randint(1, 5)  
    logo = Image.open("img\_icon/santa\_" + str(num) + ".png")  
  
    img = Image.open(imgPath)  
  
print(img.size, logo.size) # 图层 layer = Image.new("RGBA", img.size, (255, 255, 255, 0)) layer.paste(logo, (img.size\[0\] - logo.size\[0\], img.size\[1\]-logo.size\[1\])) # 覆盖 img\_after = Image.composite(layer, img, layer) # img\_after.show() img\_after.save(outImgePath)

The results are as follows

Guess you like

Origin www.cnblogs.com/7758520lzy/p/12096184.html