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 realize a simple self-made avatar artifact

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Python to achieve a simple self-made avatar artifact". In daily operation, I believe that many people have doubts about how to use Python to achieve a simple self-made avatar artifact. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the doubts about "how to use Python to achieve a simple homemade avatar artifact." Next, please follow the editor to study!

Preface

It is said that the New year is a new atmosphere, so, in order to welcome the arrival of 2022!

Text

First of all, let's give you a few groups of New year's avatars! Come on, catch it.

1) introduction to the principle

Python's PIL library (pip install Pillow) is mainly used, and the implementation steps are simple:

1. Read the profile picture and flag image with processing, which involves the function open

2. Set the flag image to a transparency gradient from left to right, which involves the function getpixel putpixel

3. Merge the translucent flag image generated in step 2 with the profile picture to be processed, involving the function paste

4. Save the new avatar, which involves the function save

2) preparing

Considering that the Spring Festival is nothing more than fireworks, New year's words and so on.

Of course, as a programmer who can search pictures on the Internet and not do it himself, Baidu is indispensable.

After browsing, I found the following pictures

3) Code display from PIL import Image # read pictures And reset the size of the New year picture to the size of the profile picture s_avatar = Image.open ('avatar 66.jpg'). Convert ("RGBA") s_banner = Image.open ('New year 66.png'). Convert ("RGBA") .resize (s_avatar.size) # sets the New year transparent gradient w H = s_banner.sizefor i in range (w): for j in range (h): # alpha = int (255-((I + j) * 255 / 1.4 / 1.4 / w* 1.3)) if int (255-((I + j) * 255 / 1.4 / 1.4 / w* 1.3)) > 0 else 0 # alpha = int ) if int (255-(i* (255/int (watt0.8) > 0 else 0 s_banner.putpixel ((I) J), s_banner.getpixel ((I, j)) [:-1] + (alpha,) # paste the New year picture into the profile picture and save the s_avatar.paste (s_banne, (0Power0), s_banne) s_avatar.save ('translucent gradient avatar .png')

Note: by adjusting the value of 0.8 in the code, you can get different gradient effects.

If you don't think the gradient from left to right is interesting:

Alpha = int (255-(i* (255/int (wrought 0.8) if int (255-(i* (255/int (wrought 0.8) > 0 else 0

You can also set the gradient from top left to bottom right, for example, replace the code that sets the transparency with the following

Alpha = int (I + j) * 255 / 1.4 / w * 1.3)) if int (I + j) * 255 / 1.4 / w * 1.3)) > 0 else 04)

The first set of random images

2 original pictures

Combinatorial effect drawing

The second group of random images

2 original pictures

Effect picture

In fact, like the National Day gradient avatar, which was very popular before, it can also be made. Let's show you a group.

The third group of gradual images of National Day

At this point, the study on "how to use Python to achieve a simple homemade avatar artifact" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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