Created
October 3, 2020 02:55
-
-
Save ahmed4end/9d17647d40b0945e5066f11ed59b21b6 to your computer and use it in GitHub Desktop.
make squircles using pillow ♥
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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