Make one image out of avatar and frame in Python with Pillow

2024/11/15 9:22:26

If I have

enter image description here

and

enter image description here

need to get

enter image description here

def create_avatar(username):avatar, frame, avatar_id = get_avatar(username)if avatar is not None and frame is not None:try:image = Image.new("RGBA", size)image.putalpha(0)# Paste avatar onto background with positioning offsetimage.paste(avatar, (157, 160), avatar)# image.save('DEBUG-avatar-pasted.png')# Paste frame over avatar and background retaining transarencyimage.paste(frame, (0, 0), frame)except Exception as e:print(e)image = Image.open("images/unknown_avatar.png").resize((512, 512))elif avatar is not None:image = Image.open(f"images/{avatar_id}.png").resize((512, 512))else:image = Image.open("images/unknown_avatar.png").resize((512, 512))image.save(f'images/{username}.png', format='png')

I have a problem with Bad transperancy mask, but need to do that

Output image sizes needs to be 512x512

Answer

You are almost there. The problem is that the frame needs to be converted to "RGBA" first.

Consider that the avatar does not need a mask or an alpha channel. You are just pasting a solid image on the canvas image. It's the frame that needs attention in these regards.

from PIL import Imageframe  = Image.open(f"front.png").convert('RGBA')
avatar = Image.open(f"back.jpg")image  = Image.new("RGBA", frame.size, "#00000000")image.paste(avatar, (85, 85))
image.paste(frame, (0, 0), frame)image.resize((512,512)).save('result.png', format='png')

enter image description here

https://en.xdnf.cn/q/119679.html

Related Q&A

Could not broadcast input array from shape (1285) into shape (1285, 5334)

Im trying to follow some example code provided in the documentation for np.linalg.svd in order to compare term and document similarities following an SVD on a TDM matrix. Heres what Ive got:results_t =…

Python URL Stepping Returns Only First Page Results

Any help with the below code would be appreciated. I have checked the results of h and g using print to verify that they are incrementing the url properly, but the program seems to be only repeating th…

Text processing to find co-occurences of strings

I need to process a series of space separated strings i.e. text sentences. ‘Co-occurrence’ is when two tags (or words) appear on the same sentence. I need to list all the co-occurring words when they…

Flask doesnt render any image [duplicate]

This question already has answers here:How to serve static files in Flask(24 answers)Link to Flask static files with url_for(2 answers)Closed 6 years ago.I have a flask application where I need to rend…

Bug in python thread

I have some raspberry pi running some python code. Once and a while my devices will fail to check in. The rest of the python code continues to run perfectly but the code here quits. I am not sure wh…

how does a function changes the value of a variable outside its scope? Python

i was coding this code and noticed something weird, after my function has been called on the variable, the value of the variable gets changed although its outside of the functions scope, how exactly is…

Python extracting element using bs4, very basic thing I think I dont understand

So Im using Beautiful Soup to try to get an element off of a page using the tag and class. Here is my code: import requests from bs4 import BeautifulSoup# Send a GET request to the webpage url = "…

Why Isnt my Gmail Account Bruteforcer Working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Python: Split Start and End Date into All Days Between Start and End Date

Ive got data called Planned Leave which includes Start Date, End Date, User ID and Leave Type.I want to be able to create a new data-frame which shows all days between Start and End Date, per User ID.S…

Python and java AES/ECB/PKCS5 encryption

JAVA VERSION:public class EncryptUtil {public static String AESEncode(String encodeRules, String content) {try {KeyGenerator keygen = KeyGenerator.getInstance("AES");keygen.init(128, new Secu…