Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created October 3, 2020 02:55
Show Gist options
  • Save ahmed4end/9d17647d40b0945e5066f11ed59b21b6 to your computer and use it in GitHub Desktop.
Save ahmed4end/9d17647d40b0945e5066f11ed59b21b6 to your computer and use it in GitHub Desktop.
make squircles using pillow ♥
import sys
from PIL import Image
import numpy as np
import math
def squircle(size=(50,50), color="black", level=4):
i = Image.new(mode = 'RGBA', size=size , color=color )
l = level
w, h = i.size
r = w if w < h else h
a = np.full((r, r), 255, 'u1')
for x in range(r):
for y in range(r):
d = math.pow(x**l + y**l, 1/l) - r + 2
if d > 1:
a[y, x] = 0
elif d > 0:
a[y, x] = round((1 - d)*255)
a = np.concatenate([np.array([a[:, 0]]).repeat(w - r, 0).T, a], 1)
a = np.concatenate([np.array([a[0, :]]).repeat(h - r, 0), a])
a = np.concatenate([np.concatenate([np.rot90(a, 2), a[:, ::-1]]), np.concatenate([a[::-1, :], a])], 1)
m = Image.fromarray(a)
m.thumbnail(i.size, Image.LANCZOS)
i.putalpha(m)
return i
if __name__ == "__main__":
i = squircle()
i.save("squircle.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment