Spaces:
Running
Running
File size: 1,796 Bytes
2d48693 |
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 |
import typing as t
from PIL import Image, ImageColor, ImageOps, ImageChops, ImageFilter
import numpy as np
class Canvas:
def __init__(self, filename=None, height=500, width=500):
self.filename = filename
self.height, self.width = height, width
self.img = Image.new("RGBA", (self.height, self.width), (0, 0, 0, 0))
def draw(self, dots, color: t.Union[tuple, str]):
if isinstance(color, str):
color = ImageColor.getrgb(color)
if isinstance(dots, tuple):
dots = [dots]
for dot in dots:
if dot[0]>=self.height or dot[1]>=self.width or dot[0]<0 or dot[1]<0:
# print(dot)
continue
self.img.putpixel(dot, color + (255,))
def add_white_border(self, border_size=5):
# 确保输入图像是 RGBA 模式
if self.img.mode != "RGBA":
self.img = self.img.convert("RGBA")
# 提取 alpha 通道
alpha = self.img.getchannel("A")
# print(alpha.size)
dilated_alpha = alpha.filter(ImageFilter.MaxFilter(size=5))
# # print(dilated_alpha.size)
white_area = Image.new("RGBA", self.img.size, (255, 255, 255, 255))
white_area.putalpha(dilated_alpha)
# 合并膨胀后的白色区域与原图像
result = Image.alpha_composite(white_area, self.img)
# expanded_alpha = ImageOps.expand(alpha, border=border_size, fill=255)
# white_border = Image.new("RGBA", image.size, (255, 255, 255, 255))
# white_border.putalpha(alpha)
return result
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
# self.img = add_white_border(self.img)
self.img.save(self.filename)
pass
|