Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to play with new heroes in the vanguard

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use python to play with the new heroes of the vanguard. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

With the help of Python language and OpenCV tool library to complete a simple P-diagram operation process.

As a visual engineer, if you only know how to use PS, you must be OUT. I have been focusing on all kinds of fancy P maps for many years, and I have been learning PS,AI,PR,AE,XD. After countless design software, I finally realized a unique stunt-"code P diagram".

Today, I will teach you the supreme secret book of the P-map world! I hope you will go further and further on the road of zuo si.

Let's take a look at today's material: a positive photo of Ash, the new hero of the vanguard:

And then this is a background picture:

Today's task is very simple. I will cutout the characters in the first picture and paste them on the background image.

This operation is not complicated with PS. Let's take a look at how this process can be implemented in code.

Material treatment

First, import some toolkits

Opencv (cv2) for image processing

Numpy for data calculation.

Matplotlib is used for drawing.

Import cv2

Import numpy as np

Import matplotlib.pyplot as plt

Establish a function to display the picture, so that it is easy to view the effect in real time in the process of P map.

It uses the functions of matplotlib, which is consistent with the process of making charts.

# set up a function to display pictures

Def show (image):

Plt.imshow (image)

Plt.axis ('off')

Plt.show ()

Then, import the foreground map.

Because opencv pictures use BGR image format by default, and the pictures we usually use are RGB (red, green, blue), we need to convert the format again, otherwise the color will be distorted when viewing.

Finally, print the picture specification and the picture itself.

# Import foreground image

Img=cv2.imread ('img.png') # Picture Import

Img = cv2.cvtColor (img,cv2.COLOR_BGR2RGB) # convert color model

Print (img.shape) # print picture specification

Show (img) # display picture

Let's take a look at the effect, which is 1054 pixels high, 703 pixels long and 3 channels.

In the same way, import the background image

# Import background image

Back_img = cv2.imread ('back_img.jpg') # Picture Import

Back_img = cv2.cvtColor (back_img,cv2.COLOR_BGR2RGB) # convert color model

Print (back_img.shape) # print picture specification

Show (back_img) # display picture

The effect is as follows: 1079 high, 1920 long and 3 channels long.

We found that the height of the figure picture is about the same as the background height, and we only need the portrait in the middle, so let's crop the picture properly first.

# crop the picture

Img = img [0 1000,150,550] # crop picture size

Show (img) # display picture

By slicing, the logo was cut off.

Reduce the size of the picture by 10%, which is the most suitable size.

# Zoom the picture

Print (img.shape) # print picture specification

Img=cv2.resize (img,None,fx=0.9,fy=0.9) # Image shrinks by 10%

Print (img.shape) # print picture specification

Print the size of the picture and find that the cut is successful.

The picture is saved in the form of a digital matrix in the computer. Each of the three color channels of red, green and blue is divided into 256 orders, represented by the 256 numbers of 0-255 respectively. For example, the picture of 900cm 360 can be understood as a pixel matrix of 900 rows and 360 columns, and each pixel is confirmed by the three numbers of R ~ (?) Therefore, we first record the number of rows and columns of the picture, and then we can read each pixel using methods such as traversal, and then calculate the matrix.

# split image information

Rows,cols,channels = img.shape # split picture information

Matting: three effects

The method of matting is the same as PS, we need to create a mask first. Before we start, we need to convert the picture to HSV format, which is a more intuitive color model that can better digitize colors.

# convert format

Img_hsv = cv2.cvtColor (img,cv2.COLOR_RGB2HSV) # converts the picture to HSV format for matting

Show (img_hsv) # display picture

Take a look at the effect:

Although you can't look directly at it, the practice is obvious, as long as the non-blue parts are extracted. We set a threshold below the minimum threshold and above the maximum threshold, the image becomes 0, and the image becomes 255 between the thresholds.

# matting

Lower_blue=np.array ([0re0jug0]) # get the minimum threshold

Upper_blue=np.array ([0255255]) # get the maximum threshold

Mask = cv2.inRange (img_hsv, lower_blue, upper_blue) # create a mask

Show (mask) # display Mask

Then, the mask just came out.

However, we found that there are so many dots among the characters that I need to get rid of them. Here, the basic method of morphological image processing is used, which erodes first and then expands. The principle is to take the local minimum and maximum values in the small area of the original image, and the logic behind it is the convolution neural network in deep learning.

Through the attempt, I found that the open operation (the integration operation that corrodes and then expands) can be used to complete this process directly, and the effect is relatively good.

Erode=cv2.erode (mask,None,iterations=3) # Image corrosion

Show (erode) # display picture

Dilate=cv2.dilate (erode,None,iterations=1) # Image inflation

Show (dilate) # display picture

Opening = cv2.morphologyEx (mask, cv2.MORPH_OPEN, cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (8) # Open Operation

Show (opening) # display picture

You can compare the effects of corrosion, expansion and opening operations after corrosion:

Image merging

Finally, it's time for image merging. First set the starting position of the character in the background image. Then iterate through each pixel in the mask, and if it is 0 (black), the color on the character image is assigned to the background image.

Center = [70240] # set the starting position of the foreground image

For i in range (rows):

For j in range (cols):

If opening [iMagazine j] = = 0: # for black

Back_img [center [0] + iMagery center [1] + j] = img [iMagazine j] # assignment color

Show (back_img) # display picture

After running, the result is displayed:

Due to the limited picture quality and simplified code, it is a little rough, but generally it has reached the function.

Finally, adjust the picture format and save it.

Back_img = cv2.cvtColor (back_img,cv2.COLOR_RGB2BGR) # Image format conversion

Back_img=cv2.resize (back_img,None,fx=0.8,fy=0.8) # Image zooming 20%

Cv2.imwrite ('result.png',back_img) # saves the image on how to use python to play with the new heroes of the vanguard. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report