This morning was inexplicable brush screen again, like last Christmas, everyone @wechat official Christmas hat, this year @wechat official to the flag, and even the majority of circle friends put forward more excessive requirements, to girlfriend, to suddenly rich, to hair of….













SkrEric doesn’t do that, but adding a flag to your avatar is a piece of work. Python can do it in 10 lines of code



Without further ado, here is the code:



# -*- coding: utf8 -*-

import cv2

# Read the picture and flag

img_head = cv2.imread(‘head.jpg’)

img_flag = cv2.imread(‘flag.png’)

Get the width of the head and flag image

w_head, h_head = img_head.shape[:2]

w_flag, h_flag = img_flag.shape[:2]

# Calculate pattern scaling

scale = w_head / w_flag / 4

# Zoom pattern

img_flag = cv2.resize(img_flag, (0, 0), fx=scale, fy=scale)

Get the new width after scaling

w_flag, h_flag = img_flag.shape[:2]

# Merge images by 3 channels

for c in range(0, 3):

img_head[w_head – w_flag:, h_head – h_flag:, c] = img_flag[:, :, c]

Save the final result

cv2.imwrite(‘new_head.jpg’, img_head)

All you need to do is copy the code above into the py file you created, and place the image of your avatar and the image of your flag in the same directory, and name the avatar as head.jpg and the flag as flag.png(of course, you can splicken any image except the flag). The final result is new_head.jpg, which is the image with the flag



The effect is as follows:







Basic idea:



1. Read in the picture of the head and flag



2. According to the length and width of the head and the flag, we calculate how much the pattern needs to be scaled



3. Add the zoomed flag to the lower right corner of your profile picture by replacing pixels



The python-opencv library is used to process the image. You can import the image using Pycharm or PIP install python-Opencv



Thanks to Crossin for the code!



Finally, I hope the motherland is prosperous and prosperous.