This commit is contained in:
@ -604,20 +604,27 @@ class ImageFilterImplement:
|
|||||||
shadow_color = "black") -> Image.Image:
|
shadow_color = "black") -> Image.Image:
|
||||||
if image.mode != 'RGBA':
|
if image.mode != 'RGBA':
|
||||||
image = image.convert('RGBA')
|
image = image.convert('RGBA')
|
||||||
offset = (x_offset, y_offset)
|
offset = (-x_offset, -y_offset)
|
||||||
# 创建阴影图层
|
# 创建阴影图层
|
||||||
shadow = Image.new('RGBA', image.size, (0,0,0,0))
|
|
||||||
shadow_rgb = ColorHandle.parse_color(shadow_color)
|
shadow_rgb = ColorHandle.parse_color(shadow_color)
|
||||||
shadow_draw = Image.new('RGBA', image.size, shadow_rgb + (0,))
|
shadow = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
||||||
alpha = image.split()[3].point(lambda p: int(p * opacity))
|
shadow_draw = ImageDraw.Draw(shadow)
|
||||||
shadow.paste(shadow_draw, (0,0), alpha)
|
shadow_draw.rectangle(
|
||||||
|
[0, 0, image.size[0], image.size[1]],
|
||||||
|
fill=(shadow_rgb[0], shadow_rgb[1], shadow_rgb[2], int(255 * opacity))
|
||||||
|
)
|
||||||
|
# 应用遮罩
|
||||||
|
shadow.putalpha(image.split()[-1])
|
||||||
|
# 移动
|
||||||
|
shadow = shadow.transform(
|
||||||
|
shadow.size,
|
||||||
|
Image.AFFINE,
|
||||||
|
(1, 0, offset[0], 0, 1, offset[1])
|
||||||
|
)
|
||||||
|
# 应用模糊
|
||||||
shadow = shadow.filter(ImageFilter.GaussianBlur(blur))
|
shadow = shadow.filter(ImageFilter.GaussianBlur(blur))
|
||||||
# 创建结果图像
|
# 将阴影叠加在原图下方
|
||||||
result = Image.new('RGBA', (image.width + abs(offset[0]), image.height + abs(offset[1])), (0,0,0,0))
|
result = ImageFilterImplement.apply_overlay(shadow, image)
|
||||||
shadow_position = (max(offset[0],0), max(offset[1],0))
|
|
||||||
image_position = (max(-offset[0],0), max(-offset[1],0))
|
|
||||||
result.paste(shadow, shadow_position, shadow)
|
|
||||||
result.paste(image, image_position, image)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user