I am trying to put player headshots inside / mask a circle around. However I get a colour on the background rather than transparent.Anyone have any idea how I might achieve this?
Currently this is the code that I have used.
import urllib.request
from PIL import Image, ImageDraw
def crop_image(url):
img = Image.open(urllib.request.urlopen(f"{url}"))
img = img.convert("RGBA")
# Create a circular mask
size = (min(img.size),) * 2
mask = Image.new('L', size, 0)
draw = ImageDraw.Draw(mask)
draw.circle((size[0] // 2, size[1] // 2), radius=size[0] // 2, fill="white", outline="grey", width=5)
# Apply the mask to the image
img = img.crop((0, 0, size[0], size[1]))
img.putalpha(mask)
img.save(r"C:UserspeterskDesktopoutput.png")
return img
url = "https://static.www.nfl.com/image/private/t_person_squared_mobile/f_auto/league/icjfdfsncqlsqe49iujv"
crop_image(url)