| name | slack-gif-creator |
| description | 为 Slack 创建优化动画 GIF 的工具包,包含大小约束验证器和可组合动画原语。当用户请求为 Slack 创建动画 GIF 或表情动画时使用此技能,例如"为 Slack 制作一个 X 做 Y 的 GIF"。 |
| license | 完整条款见 LICENSE.txt |
Slack GIF 创建器 - 灵活工具包
一个为 Slack 创建优化动画 GIF 的工具包。提供 Slack 约束验证器、可组合动画原语和可选助手工具。根据需要应用这些工具来实现创意愿景。
Slack 的要求
Slack 对 GIF 有基于用途的特定要求:
消息 GIF:
- 最大大小:约 2MB
- 最佳尺寸:480x480
- 典型帧率:15-20
- 颜色限制:128-256
- 时长:2-5 秒
表情 GIF:
- 最大大小:64KB(严格限制)
- 最佳尺寸:128x128
- 典型帧率:10-12
- 颜色限制:32-48
- 时长:1-2 秒
表情 GIF 具有挑战性 - 64KB 限制是严格的。有帮助的策略:
- 总共限制为 10-15 帧
- 最多使用 32-48 种颜色
- 保持设计简单
- 避免渐变
- 经常验证文件大小
工具包结构
此技能提供三种类型的工具:
- 验证器 - 检查 GIF 是否符合 Slack 要求
- 动画原语 - 可组合的运动构建块(抖动、弹跳、移动、万花筒)
- 助手工具 - 用于常见需求的可选函数(文本、颜色、效果)
可以完全自由地应用这些工具。
核心验证器
要确保 GIF 符合 Slack 约束,使用这些验证器:
from core.gif_builder import GIFBuilder
builder = GIFBuilder(width=128, height=128, fps=10)
info = builder.save('emoji.gif', num_colors=48, optimize_for_emoji=True)
文件大小验证器:
from core.validators import check_slack_size
passes, info = check_slack_size('emoji.gif', is_emoji=True)
尺寸验证器:
from core.validators import validate_dimensions
passes, info = validate_dimensions(128, 128, is_emoji=True)
完整验证:
from core.validators import validate_gif, is_slack_ready
all_pass, results = validate_gif('emoji.gif', is_emoji=True)
if is_slack_ready('emoji.gif', is_emoji=True):
print("准备上传!")
动画原语
这些是可组合的运动构建块。以任何组合应用于任何对象:
抖动
from templates.shake import create_shake_animation
frames = create_shake_animation(
object_type='emoji',
object_data={'emoji': '😱', 'size': 80},
num_frames=20,
shake_intensity=15,
direction='both'
)
弹跳
from templates.bounce import create_bounce_animation
frames = create_bounce_animation(
object_type='circle',
object_data={'radius': 40, 'color': (255, 100, 100)},
num_frames=30,
bounce_height=150
)
旋转
from templates.spin import create_spin_animation, create_loading_spinner
frames = create_spin_animation(
object_type='emoji',
object_data={'emoji': '🔄', 'size': 100},
rotation_type='clockwise',
full_rotations=2
)
frames = create_spin_animation(rotation_type='wobble', full_rotations=3)
frames = create_loading_spinner(spinner_type='dots')
脉冲/心跳
from templates.pulse import create_pulse_animation, create_attention_pulse
frames = create_pulse_animation(
object_data={'emoji': '❤️', 'size': 100},
pulse_type='smooth',
scale_range=(0.8, 1.2)
)
frames = create_pulse_animation(pulse_type='heartbeat')
frames = create_attention_pulse(emoji='⚠️', num_frames=20)
淡入淡出
from templates.fade import create_fade_animation, create_crossfade
frames = create_fade_animation(fade_type='in')
frames = create_fade_animation(fade_type='out')
frames = create_crossfade(
object1_data={'emoji': '😊', 'size': 100},
object2_data={'emoji': '😂', 'size': 100}
)
缩放
from templates.zoom import create_zoom_animation, create_explosion_zoom
frames = create_zoom_animation(
zoom_type='in',
scale_range=(0.1, 2.0),
add_motion_blur=True
)
frames = create_zoom_animation(zoom_type='out')
frames = create_explosion_zoom(emoji='💥')
爆炸/碎裂
from templates.explode import create_explode_animation, create_particle_burst
frames = create_explode_animation(
explode_type='burst',
num_pieces=25
)
frames = create_explode_animation(explode_type='shatter')
frames = create_explode_animation(explode_type='dissolve')
frames = create_particle_burst(particle_count=30)
摆动/抖动
from templates.wiggle import create_wiggle_animation, create_excited_wiggle
frames = create_wiggle_animation(
wiggle_type='jello',
intensity=1.0,
cycles=2
)
frames = create_wiggle_animation(wiggle_type='wave')
frames = create_excited_wiggle(emoji='🎉')
滑动
from templates.slide import create_slide_animation, create_multi_slide
frames = create_slide_animation(
direction='left',
slide_type='in',
overshoot=True
)
frames = create_slide_animation(direction='left', slide_type='across')
objects = [
{'data': {'emoji': '🎯', 'size': 60}, 'direction': 'left', 'final_pos': (120, 240)},
{'data': {'emoji': '🎪', 'size': 60}, 'direction': 'right', 'final_pos': (240, 240)}
]
frames = create_multi_slide(objects, stagger_delay=5)
翻转
from templates.flip import create_flip_animation, create_quick_flip
frames = create_flip_animation(
object1_data={'emoji': '😊', 'size': 120},
object2_data={'emoji': '😂', 'size': 120},
flip_axis='horizontal'
)
frames = create_flip_animation(flip_axis='vertical')
frames = create_quick_flip('👍', '👎')
变形/转换
from templates.morph import create_morph_animation, create_reaction_morph
frames = create_morph_animation(
object1_data={'emoji': '😊', 'size': 100},
object2_data={'emoji': '😂', 'size': 100},
morph_type='crossfade'
)
frames = create_morph_animation(morph_type='scale')
frames = create_morph_animation(morph_type='spin_morph')
移动效果
from templates.move import create_move_animation
frames = create_move_animation(
object_type='emoji',
object_data={'emoji': '🚀', 'size': 60},
start_pos=(50, 240),
end_pos=(430, 240),
motion_type='linear',
easing='ease_out'
)
frames = create_move_animation(
object_type='emoji',
object_data={'emoji': '⚽', 'size': 60},
start_pos=(50, 350),
end_pos=(430, 350),
motion_type='arc',
motion_params={'arc_height': 150}
)
frames = create_move_animation(
object_type='emoji',
object_data={'emoji': '🌍', 'size': 50},
motion_type='circle',
motion_params={
'center': (240, 240),
'radius': 120,
'angle_range': 360
}
)
frames = create_move_animation(
motion_type='wave',
motion_params={
'wave_amplitude': 50,
'wave_frequency': 2
}
)
from core.easing import interpolate, calculate_arc_motion
for i in range(num_frames):
t = i / (num_frames - 1)
x = interpolate(start_x, end_x, t, easing='ease_out')
万花筒效果
from templates.kaleidoscope import apply_kaleidoscope, create_kaleidoscope_animation
kaleido_frame = apply_kaleidoscope(frame, segments=8)
frames = create_kaleidoscope_animation(
base_frame=my_frame,
num_frames=30,
segments=8,
rotation_speed=1.0
)
from templates.kaleidoscope import apply_simple_mirror
mirrored = apply_simple_mirror(frame, mode='quad')
自由组合原语,遵循这些模式:
for i in range(num_frames):
frame = create_blank_frame(480, 480, bg_color)
t_bounce = i / (num_frames - 1)
y = interpolate(start_y, ground_y, t_bounce, 'bounce_out')
if y >= ground_y - 5:
shake_x = math.sin(i * 2) * 10
x = center_x + shake_x
else:
x = center_x
draw_emoji(frame, '⚽', (x, y), size=60)
builder.add_frame(frame)
助手工具
这些是用于常见需求的可选助手。根据需要使用、修改或替换为自定义实现。
GIF 构建器(组装和优化)
from core.gif_builder import GIFBuilder
builder = GIFBuilder(width=480, height=480, fps=20)
for frame in my_frames:
builder.add_frame(frame)
builder.save('output.gif',
num_colors=128,
optimize_for_emoji=False)
主要功能:
- 自动颜色量化
- 重复帧移除
- Slack 限制大小警告
- 表情模式(积极优化)
文本渲染
对于像表情这样的小 GIF,文本可读性具有挑战性。常见的解决方案涉及添加轮廓:
from core.typography import draw_text_with_outline, TYPOGRAPHY_SCALE
draw_text_with_outline(
frame, "BONK!",
position=(240, 100),
font_size=TYPOGRAPHY_SCALE['h1'],
text_color=(255, 68, 68),
outline_color=(0, 0, 0),
outline_width=4,
centered=True
)
要实现自定义文本渲染,使用 PIL 的 ImageDraw.text(),这对较大的 GIF 效果良好。
颜色管理
专业外观的 GIF 通常使用协调的调色板:
from core.color_palettes import get_palette
palette = get_palette('vibrant')
bg_color = palette['background']
text_color = palette['primary']
accent_color = palette['accent']
要直接处理颜色,使用 RGB 元组 - 任何适合用例的方式都可以。
视觉效果
用于冲击时刻的可选效果:
from core.visual_effects import ParticleSystem, create_impact_flash, create_shockwave_rings
particles = ParticleSystem()
particles.emit_sparkles(x=240, y=200, count=15)
particles.emit_confetti(x=240, y=200, count=20)
particles.update()
particles.render(frame)
frame = create_impact_flash(frame, position=(240, 200), radius=100)
frame = create_shockwave_rings(frame, position=(240, 200), radii=[30, 60, 90])
缓动函数
平滑运动使用缓动而非线性插值:
from core.easing import interpolate
y = interpolate(start=0, end=400, t=progress, easing='ease_in')
y = interpolate(start=0, end=400, t=progress, easing='ease_out')
y = interpolate(start=0, end=400, t=progress, easing='bounce_out')
scale = interpolate(start=0.5, end=1.0, t=progress, easing='elastic_out')
可用缓动:linear、ease_in、ease_out、ease_in_out、bounce_out、elastic_out、back_out(过冲),以及 core/easing.py 中的更多选项。
帧组合
如果需要的基本绘图工具:
from core.frame_composer import (
create_gradient_background,
draw_emoji_enhanced,
draw_circle_with_shadow,
draw_star
)
frame = create_gradient_background(480, 480, top_color, bottom_color)
draw_emoji_enhanced(frame, '🎉', position=(200, 200), size=80, shadow=True)
优化策略
当 GIF 太大时:
对于消息 GIF(>2MB):
- 减少帧数(降低帧率或缩短时长)
- 减少颜色(128 → 64 色)
- 减小尺寸(480x480 → 320x320)
- 启用重复帧移除
对于表情 GIF(>64KB)- 要激进:
- 总共限制为 10-12 帧
- 最多使用 32-40 种颜色
- 避免渐变(纯色压缩更好)
- 简化设计(更少元素)
- 在保存方法中使用
optimize_for_emoji=True
示例组合模式
简单反应(脉冲)
builder = GIFBuilder(128, 128, 10)
for i in range(12):
frame = Image.new('RGB', (128, 128), (240, 248, 255))
scale = 1.0 + math.sin(i * 0.5) * 0.15
size = int(60 * scale)
draw_emoji_enhanced(frame, '😱', position=(64-size//2, 64-size//2),
size=size, shadow=False)
builder.add_frame(frame)
builder.save('reaction.gif', num_colors=40, optimize_for_emoji=True)
from core.validators import check_slack_size
check_slack_size('reaction.gif', is_emoji=True)
带冲击的动作(弹跳 + 闪光)
builder = GIFBuilder(480, 480, 20)
for i in range(15):
frame = create_gradient_background(480, 480, (240, 248, 255), (200, 230, 255))
t = i / 14
y = interpolate(0, 350, t, 'ease_in')
draw_emoji_enhanced(frame, '⚽', position=(220, int(y)), size=80)
builder.add_frame(frame)
for i in range(8):
frame = create_gradient_background(480, 480, (240, 248, 255), (200, 230, 255))
if i < 3:
frame = create_impact_flash(frame, (240, 350), radius=120, intensity=0.6)
draw_emoji_enhanced(frame, '⚽', position=(220, 350), size=80)
if i > 2:
draw_text_with_outline(frame, "GOAL!", position=(240, 150),
font_size=60, text_color=(255, 68, 68),
outline_color=(0, 0, 0), outline_width=4, centered=True)
builder.add_frame(frame)
builder.save('goal.gif', num_colors=128)
组合原语(移动 + 抖动)
from templates.shake import create_shake_animation
shake_frames = create_shake_animation(
object_type='emoji',
object_data={'emoji': '😰', 'size': 70},
num_frames=20,
shake_intensity=12
)
builder = GIFBuilder(480, 480, 20)
for i in range(40):
t = i / 39
if i < 20:
frame = create_blank_frame(480, 480, (255, 255, 255))
x = interpolate(50, 300, t * 2, 'linear')
draw_emoji_enhanced(frame, '🚗', position=(int(x), 300), size=60)
draw_emoji_enhanced(frame, '😰', position=(350, 200), size=70)
else:
frame = shake_frames[i - 20]
draw_emoji_enhanced(frame, '🚗', position=(300, 300), size=60)
builder.add_frame(frame)
builder.save('scare.gif')
设计理念
此工具包提供建筑块,而非死板配方。处理 GIF 请求:
- 理解创意愿景 - 应该发生什么?情绪是什么?
- 设计动画 - 分解为阶段(预期、动作、反应)
- 按需应用原语 - 抖动、弹跳、移动、效果 - 自由混合
- 验证约束 - 检查文件大小,特别是表情 GIF
- 如需迭代 - 如果超过大小限制,减少帧数/颜色
目标是在 Slack 技术约束内的创作自由。
依赖
要使用此工具包,仅在尚未安装时安装这些依赖:
pip install pillow imageio numpy