This commit is contained in:
@ -1,10 +1,11 @@
|
||||
import random
|
||||
from PIL import Image, ImageFilter, ImageDraw, ImageStat
|
||||
from PIL import Image, ImageFilter, ImageDraw, ImageStat, ImageFont
|
||||
from PIL import ImageEnhance
|
||||
from PIL import ImageChops
|
||||
from PIL import ImageOps
|
||||
import cv2
|
||||
|
||||
from konabot.common.path import FONTS_PATH
|
||||
from konabot.plugins.fx_process.color_handle import ColorHandle
|
||||
|
||||
import math
|
||||
@ -1130,7 +1131,7 @@ class ImageFilterImplement:
|
||||
|
||||
# 基于形状的描边
|
||||
@staticmethod
|
||||
def apply_shape_stroke(image: Image.Image, stroke_width: int = 5, stroke_color: str = 'black') -> Image.Image:
|
||||
def apply_shape_stroke(image: Image.Image, stroke_width: int = 5, stroke_color: str = 'black', roughness: float = None) -> Image.Image:
|
||||
if image.mode != 'RGBA':
|
||||
image = image.convert('RGBA')
|
||||
|
||||
@ -1149,9 +1150,10 @@ class ImageFilterImplement:
|
||||
cv2.CHAIN_APPROX_SIMPLE
|
||||
)
|
||||
|
||||
# # 减少轮廓点数,以实现尖角效果
|
||||
# epsilon = 0.01 * cv2.arcLength(contours[0], True)
|
||||
# contours = [cv2.approxPolyDP(cnt, epsilon, True) for cnt in contours]
|
||||
# 减少轮廓点数,以实现尖角效果
|
||||
if roughness is not None:
|
||||
epsilon = roughness * cv2.arcLength(contours[0], True)
|
||||
contours = [cv2.approxPolyDP(cnt, epsilon, True) for cnt in contours]
|
||||
|
||||
# 将轮廓点沿法线方向外扩
|
||||
expanded_contours = expand_contours(contours, stroke_width)
|
||||
@ -1230,7 +1232,7 @@ class ImageFilterImplement:
|
||||
|
||||
# 设置通道
|
||||
@staticmethod
|
||||
def apply_set_channel(image: Image.Image, apply_image: Image.Image, channel: str = 'R', value: int = 255) -> Image.Image:
|
||||
def apply_set_channel(image: Image.Image, apply_image: Image.Image, channel: str = 'A') -> Image.Image:
|
||||
if image.mode != 'RGBA':
|
||||
image = image.convert('RGBA')
|
||||
|
||||
@ -1244,6 +1246,36 @@ class ImageFilterImplement:
|
||||
image_arr[:, :, channel_index] = apply_arr[:, :, channel_index]
|
||||
|
||||
return Image.fromarray(image_arr, 'RGBA')
|
||||
|
||||
@staticmethod
|
||||
def generate_empty(image: Image.Image, images: list[Image.Image], width: int = 512, height: int = 512) -> Image.Image:
|
||||
# 生成空白图像
|
||||
empty_image = Image.new('RGBA', (width, height), (255, 255, 255, 0))
|
||||
images.append(empty_image)
|
||||
return image
|
||||
|
||||
@staticmethod
|
||||
def generate_text(image: Image.Image, images: list[Image.Image],
|
||||
text: str = "请输入文本",
|
||||
font_size: int = 32,
|
||||
font_color: str = "black",
|
||||
font_path: str = "HarmonyOS_Sans_SC_Regular.ttf") -> Image.Image:
|
||||
# 生成文本图像
|
||||
font = ImageFont.truetype(FONTS_PATH / font_path, font_size)
|
||||
# 获取文本边界框
|
||||
padding = 10
|
||||
temp_draw = ImageDraw.Draw(Image.new('RGBA', (1,1)))
|
||||
bbox = temp_draw.textbbox((0, 0), text, font=font)
|
||||
text_width = bbox[2] - bbox[0] + padding * 2
|
||||
text_height = bbox[3] - bbox[1] + padding * 2
|
||||
# 创建文本图像
|
||||
text_image = Image.new('RGBA', (text_width, text_height), (255, 255, 255, 0))
|
||||
draw = ImageDraw.Draw(text_image)
|
||||
draw_x = padding - bbox[0]
|
||||
draw_y = padding - bbox[1]
|
||||
draw.text((draw_x,draw_y), text, font=font, fill=ColorHandle.parse_color(font_color) + (255,))
|
||||
images.append(text_image)
|
||||
return image
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user