Hello, everyone, I am the talented brother.

National Day is finally coming, recently there is a five-star red flag translucent gradient avatar is very hot, in douyin, wechat group are popular.

So, as a Python player, why don’t you share your strength and create these avatars quickly in Python? ?

Come on, show!

1. Principle introduction

So what we see is an avatar like this, which is basically a red flag gradient and then we overlay it on top of our avatar.

So, my idea is to get a region with the same size as my avatar through the red flag image, and then increase the opacity of this region from left to right, and then merge the image with the avatar, and finally save it.

Based on this idea and the PIL library introduced in PIL tutorial, we can roughly split the implementation steps into the following steps:

  • Read flag and profile photoopen
  • Intercept part of the flagcrop
  • Opacity gradient from left to rightputpixel
  • Paste the area into your avatarpaste+resize
  • Save your new avatarsave

Now that we know the implementation steps, let’s get started!

2. Implementation steps

Remember to prepare your flag and your profile photo to your local site

Background reply 955, get the national flag file in the National Day folder, or add the author’s wechat reply at the end of the article to get the National Day.

2.1. Read the picture

from PIL import Image

guoqi = Image.open(PNG 'Five-star Red flag')
touxiang = Image.open('avatar. JPG')
Copy the code

2.2. Intercept area

Since my head is square, I need to cut the square area to make it easier to paste the transparent gradient flag.

Get the size of the flag
x,y = guoqi.size
# set the upper-left and lower-right coordinates according to the requirements.
quyu = guoqi.crop((262.100, y+62,y-100))
Copy the code

2.3. Set the transparent gradient

In PIL library, getPixel ((I,j)) means to get the color value of (I,j) pixel. Similarly, we can set the color of (I,j) pixel by putPixel ((I,j), color).

For color, it is a tuple containing four parameters (R,G,B,alpha), respectively RGB value and transparency, where transparency 255 means opaque, 0 means 100% transparent.

With this in mind, we are ready to start the transparency gradient.

This is the simplest meet demand is transparent gradient from left to right, in turn, high transparency (parameter values smaller), considering that from 255 to 0, and can only be an integer, here since WeChat head is 900 * 900, so I consider is once every 3 pixels to transparent gradient, when more than 255 opacity to 100%, that is, the corresponding parameter to 0.

Get the size of the avatar
w,h = touxiang.size
# Resize the area to the size of the avatar
quyu = quyu.resize((w,h))
# Transparent gradient Settings
for i in range(w):
    for j in range(h):
        color = quyu.getpixel((i, j))
        alpha = 255-i//3
        if alpha < 0:
            alpha=0
        color = color[:-1] + (alpha, )
        quyu.putpixel((i, j), color)
Copy the code

2.4. Paste to your profile picture and save

It is important to keep the transparent background when pasting, otherwise it is not fun, equal to direct full coverage

touxiang.paste(quyu,(0.0),quyu)
touxiang.save('Five Star Red Flag translucent gradient avature.png')
Copy the code

Of course, we can also use other transparent gradient way, such as the upper left corner to the lower right corner and so on, specific here does not expand the demonstration, interested can directly responsible for the complete code test play!

3. Complete code

from PIL import Image

# fetch image
guoqi = Image.open(PNG 'Five-star Red flag')
touxiang = Image.open('avatar. JPG')

Get the size of the flag
x,y = guoqi.size
# set the upper-left and lower-right coordinates according to the requirements.
quyu = guoqi.crop((262.100, y+62,y-100))

Get the size of the avatar
w,h = touxiang.size
# Resize the area to the size of the avatar
quyu = quyu.resize((w,h))
# Transparent gradient Settings
for i in range(w):
    for j in range(h):
        color = quyu.getpixel((i, j))
        alpha = 255-i//3
        if alpha < 0:
            alpha=0
        color = color[:-1] + (alpha, )
        quyu.putpixel((i, j), color)

Paste into your avatar and save
touxiang.paste(quyu,(0.0),quyu)
touxiang.save('Five Star Red Flag translucent gradient avature.png')
Copy the code

The above is all the content of this time, if you like, hope to give me a like and watch oh!! More welcome to share to more friends ~

If in doubt, you can add the author’s wechat to exchange and learn together!!