This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_utils.py
53 lines (38 loc) · 1.73 KB
/
img_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from PIL import Image, ImageDraw
import requests
import numpy as np
from io import BytesIO
def img_from_url(url: str) -> Image.Image:
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
def create_dark_block(width: int, height: int) -> Image.Image:
return Image.new("RGBA", (width, height), (26, 26, 26))
def create_light_block(width: int, height: int, mode="RGBA") -> Image.Image:
return Image.new(mode, (width, height), (41, 41, 41))
def create_rounded_mask(size: tuple[int, int], radius: int):
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle((0, 0, *size), radius, fill=255)
return mask
def linear_gradient_img(size: tuple[int, int], color1: tuple[int, int, int], color2: tuple[int, int, int]) -> Image.Image:
w, h = size
gradient = np.zeros((h,w,3), np.uint8)
gradient[:,:,0] = np.linspace(color1[0], color2[0], w, dtype=np.uint8)
gradient[:,:,1] = np.linspace(color1[1], color2[1], w, dtype=np.uint8)
gradient[:,:,2] = np.linspace(color1[2], color2[2], w, dtype=np.uint8)
return Image.fromarray(gradient)
def linear_gradient_l_img(size: tuple[int, int], color1: int, color2: int) -> Image.Image:
w, h = size
gradient = np.zeros((h,w), np.uint8)
gradient[:,:] = np.linspace(color1, color2, w, dtype=np.uint8)
return Image.fromarray(gradient, "L")
def create_relic_background(size: tuple[int, int], rarity: int) -> Image.Image:
color = (92, 255, 89)
if rarity == 3: color = (92, 89, 255)
if rarity == 4: color = (189, 123, 255)
if rarity == 5: color = (255, 222, 89)
img = Image.new("RGBA", size, color)
mask = linear_gradient_l_img(size, 255, 255-204)
img.putalpha(mask)
return img