In [9]:
import numpy as np
from PIL import Image, ImageDraw
In [2]:
#https://stackoverflow.com/questions/51486297/cropping-an-image-in-a-circular-way-using-python
In [13]:
import requests
import io
In [14]:
buf = requests.get("https://crashlaker.github.io/assets/static/crashlaker-logo.png").content
buf = io.BytesIO(buf)
In [17]:
img = Image.open(buf).convert("RGB")
npImage=np.array(img)
h,w=img.size

# Create same size alpha layer with circle
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)

# Convert alpha Image to numpy array
npAlpha=np.array(alpha)

# Add alpha layer to RGB
npImage=np.dstack((npImage,npAlpha))
In [21]:
Image.fromarray(npImage).save('result.png')
In [20]:
Image.fromarray(npImage)
Out[20]:
In [ ]: