跳转到内容

Slack GIF Creator:Slack 动画 GIF 制作

slack-gif-creator 是创建 Slack 优化 GIF 的完整工具链——4 个 Python 模块覆盖了从帧合成、动画曲线到 Slack 约束验证的完整管线。

  • 🎞 GIF 构建:GIFBuilder 类,支持逐帧添加和批量操作
  • 🎨 帧合成:预设的圆形、星星、渐变、文字等绘图函数
  • ✅ Slack 约束验证:维度、FPS、颜色数、文件大小的自动化检查
  • 📈 缓动函数:12 种动画曲线(linear、ease、bounce、elastic、back)
  • 🔄 动画模式:shake、pulse、bounce、spin、fade、slide、zoom、explode

当用户请求”为 Slack 创建一个 GIF”、“做一个动画表情”、“生成一个 GIF 动画”、“make me a GIF for Slack”等时触发。

slack-gif-creator is a complete toolchain for creating Slack-optimized animated GIFs — 4 Python modules covering the full pipeline from frame composition and animation curves to Slack constraint validation.

  • 🎞 GIF building: GIFBuilder class with frame-by-frame and batch operations
  • 🎨 Frame composition: preset drawing functions (circles, stars, gradients, text)
  • ✅ Slack constraint validation: automated checks for dimensions, FPS, colors, file size
  • 📈 Easing functions: 12 animation curves (linear, ease, bounce, elastic, back)
  • 🔄 Animation patterns: shake, pulse, bounce, spin, fade, slide, zoom, explode

Triggers when users request “create a GIF for Slack”, “make an animated emoji”, “generate an animated GIF”, “make me a GIF for Slack”, etc.

slack-gif-creator 是典型的**“脚本驱动型”** Skill——4 个 Python 模块各司其职,构成完整的 GIF 创建管线。

约 250 行的 SKILL.md 采用”知识手册 + API 参考”的混合结构:

  1. Slack Requirements:明确列出 Slack 的约束——Emoji: 128x128, FPS 10-30, Colors 48-128, 3 秒以内
  2. Core Workflow:从创建 Builder → 生成帧 → 保存优化的标准流程示例
  3. Drawing Graphics:PIL 绘图技巧——如何画圆、多边形、线条,如何做渐变
  4. Animation Concepts:8 种动画模式详解(shake, pulse, bounce, spin, fade, slide, zoom, explode)
  5. Optimization Strategies:5 种压缩方法
  6. API Reference:4 个模块的完整 API 文档

slack-gif-creator is a classic “Script-driven” Skill — 4 Python modules each with clear responsibilities, forming a complete GIF creation pipeline.

~250 line SKILL.md with a “knowledge manual + API reference” hybrid structure:

  1. Slack Requirements: Explicit Slack constraints — Emoji: 128x128, FPS 10-30, Colors 48-128, under 3 seconds
  2. Core Workflow: Standard pipeline from create Builder → generate frames → save optimized
  3. Drawing Graphics: PIL drawing techniques — circles, polygons, lines, gradients
  4. Animation Concepts: 8 animation patterns (shake, pulse, bounce, spin, fade, slide, zoom, explode)
  5. Optimization Strategies: 5 compression methods
  6. API Reference: Complete API docs for all 4 modules

slack-gif-creator 模块关系图

graph TD
  SKILL[SKILL.md] -->|指导| Claude
  Claude -->|调用| GifBuilder[core/gif_builder.py]
  Claude -->|调用| FrameComposer[core/frame_composer.py]
  Claude -->|调用| Validators[core/validators.py]
  Claude -->|调用| Easing[core/easing.py]

  GifBuilder -->|使用| FrameComposer
  GifBuilder -->|使用| Easing
  GifBuilder -->|输出 GIF| File[output.gif]
  Validators -->|验证| File

  subgraph core [core/]
      GifBuilder
      FrameComposer
      Validators
      Easing
  end

  style SKILL fill:#4fc3f7,stroke:#0288d1,color:#000
  style GifBuilder fill:#81c784,stroke:#388e3c,color:#000
  style Validators fill:#ffb74d,stroke:#f57c00,color:#000
  style File fill:#ce93d8,stroke:#7b1fa2,color:#000
脚本语言行数复杂度功能
gif_builder.pyPython~250⭐⭐⭐GIF 构建器:帧管理、颜色量化、保存优化
frame_composer.pyPython~180⭐⭐帧合成工具:预置绘图函数
validators.pyPython~140⭐⭐Slack 约束验证器:维度/大小/FPS
easing.pyPython~200⭐⭐动画缓动函数库:12 种曲线
ScriptLanguageLinesComplexityFunction
gif_builder.pyPython~250⭐⭐⭐GIF Builder: frame management, color quantization, save optimization
frame_composer.pyPython~180⭐⭐Frame composition: preset drawing utilities
validators.pyPython~140⭐⭐Slack constraint validator: dimensions/size/FPS
easing.pyPython~200⭐⭐Animation easing function library: 12 curves
gif_builder.py — GIF 构建器(核心部分) ↗ 源文件
1 class GIFBuilder: 2 """Builder for creating optimized GIFs from frames.""" 3 4 def __init__(self, width=480, height=480, fps=15): 5 self.width = width 6 self.height = height 7 self.fps = fps 8 self.frames: list[np.ndarray] = [] 9 10 def add_frame(self, frame): 11 if isinstance(frame, Image.Image): 12 frame = np.array(frame.convert("RGB")) 13 if frame.shape[:2] != (self.height, self.width): 14 pil_frame = Image.fromarray(frame) 15 pil_frame = pil_frame.resize( 16 (self.width, self.height), Image.Resampling.LANCZOS) 17 frame = np.array(pil_frame) 18 self.frames.append(frame) 19 20 def optimize_colors(self, num_colors=128, use_global_palette=True): 21 optimized = [] 22 if use_global_palette and len(self.frames) > 1: 23 # Create global palette from sampled frames 24 sample_size = min(5, len(self.frames)) 25 # ... color quantization logic 26 return optimized 27 28 def save(self, output_path, num_colors=128, 29 optimize_for_emoji=False, remove_duplicates=False): 30 if optimize_for_emoji: 31 num_colors = min(num_colors, 48) 32 # Additional emoji optimizations 33 frames = self.optimize_colors(num_colors=num_colors) 34 if remove_duplicates: 35 frames = self._remove_duplicate_frames(frames) 36 duration = int(1000 / self.fps) 37 imageio.mimsave(output_path, frames, duration=duration, 38 loop=0, palettesize=num_colors)
代码解读
L2 GIFBuilder 是核心类,管理帧的添加、颜色优化和 GIF 输出。构造函数接收 width/height/fps 参数,确保所有帧的统一规格。 L8 add_frame() 支持 numpy array 和 PIL Image 两种输入格式——自动将 PIL 转换为 RGB numpy array。自动调整尺寸到构造时指定的宽高,使用 LANCZOS 重采样保证质量。 L16 optimize_colors() 是文件大小控制的关键。use_global_palette=True 时,从所有帧中采样创建全局调色板——比每帧独立调色板的压缩效率更高。 L23 save() 是最终的输出方法。optimize_for_emoji 模式自动将颜色限制在 48 色以内。remove_duplicates 检测并移除连续的重复帧,进一步压缩。 L29 imageio.mimsave 是实际输出 GIF 的函数。duration=1000/fps 将 FPS 转换为帧间隔(毫秒)。loop=0 表示无限循环。

frame_composer.py 提供了绘图辅助函数——create_blank_frame(纯色背景)、create_gradient_background(垂直渐变)、draw_circle、draw_text、draw_star(五角星)。这些函数封装了 PIL ImageDraw 的常见操作,降低 Claude 在编写动画时的 cognitive load。

frame_composer.py provides drawing helper functions — create_blank_frame (solid background), create_gradient_background (vertical gradient), draw_circle, draw_text, draw_star (5-pointed star). These functions encapsulate common PIL ImageDraw operations, reducing Claude’s cognitive load when writing animations.

frame_composer.py — 渐变背景与星星绘制 ↗ 源文件
1 def create_gradient_background(width, height, top_color, bottom_color): 2 frame = Image.new("RGB", (width, height)) 3 draw = ImageDraw.Draw(frame) 4 r1, g1, b1 = top_color 5 r2, g2, b2 = bottom_color 6 for y in range(height): 7 ratio = y / height 8 r = int(r1 * (1 - ratio) + r2 * ratio) 9 g = int(g1 * (1 - ratio) + g2 * ratio) 10 b = int(b1 * (1 - ratio) + b2 * ratio) 11 draw.line([(0, y), (width, y)], fill=(r, g, b)) 12 return frame 13 14 def draw_star(frame, center, size, fill_color, outline_color=None, outline_width=1): 15 draw = ImageDraw.Draw(frame) 16 x, y = center 17 points = [] 18 for i in range(10): 19 angle = (i * 36 - 90) * math.pi / 180 20 radius = size if i % 2 == 0 else size * 0.4 21 px = x + radius * math.cos(angle) 22 py = y + radius * math.sin(angle) 23 points.append((px, py)) 24 draw.polygon(points, fill=fill_color, outline=outline_color, width=outline_width) 25 return frame}
代码解读
L1 create_gradient_background 逐行绘制渐变。每行根据 y/height 比例在 top_color 和 bottom_color 之间线性插值——简单但视觉效果好。 L12 draw_star 使用循环计算五角星的 10 个顶点(5 个外顶点 + 5 个内顶点)。外半径和内半径(外半径的 40%)交替排列。 L13 angle 计算:每个点间隔 36 度(360/10),起始偏移 -90 度使得星星的尖角朝上。这是标准的五角星几何。 L18 polygon 绘制填充的多边形,同时支持 outline。width=outline_width 确保轮廓线有足够厚度——SKILL.md 要求 width>=2 避免看上去粗糙。

validators.py 是 Slack 平台适配的”质量门”。validate_gif() 检查文件是否存在、读取维度/帧数/时长/文件大小,并根据 is_emoji 参数进行不同的验证规则。is_slack_ready() 是简化的布尔版本。

validators.py is the “quality gate” for Slack platform adaptation. validate_gif() checks file existence, reads dimensions/frame count/duration/file size, and applies different validation rules based on the is_emoji parameter. is_slack_ready() is a simplified boolean version.

validators.py — Slack 约束验证器(核心逻辑) ↗ 源文件
1 def validate_gif(gif_path, is_emoji=True, verbose=True): 2 # Check file existence 3 if not gif_path.exists(): 4 return False, {"error": f"File not found: {gif_path}"} 5 6 # Get file size 7 size_bytes = gif_path.stat().st_size 8 size_kb = size_bytes / 1024 9 10 # Get dimensions and frame info 11 with Image.open(gif_path) as img: 12 width, height = img.size 13 # Count frames by seeking 14 frame_count = 0 15 try: 16 while True: 17 img.seek(frame_count) 18 frame_count += 1 19 except EOFError: 20 pass 21 # Get duration info 22 duration_ms = img.info.get("duration", 100) 23 24 # Validate dimensions 25 if is_emoji: 26 optimal = width == height == 128 27 acceptable = width == height and 64 <= width <= 128 28 dim_pass = acceptable 29 else: 30 aspect_ratio = max(w,h)/min(w,h) 31 dim_pass = aspect_ratio <= 2.0 and 320<=min(w,h)<=640 32 33 return dim_pass, results_dict
代码解读
L2 validate_gif() 返回 (passes, results_dict) 元组——清晰的 API 设计。passes 是布尔值快速判断,results_dict 包含所有详细信息用于调试。 L3 第一步:检查文件是否存在。如果文件不存在,立即返回 False + 错误信息——fail-fast 模式。 L10 使用 PIL 打开 GIF,读取 width/height。通过 seek() 循环计数帧数——PIL 的 GIF 帧遍历标准模式。 L20 Emoji 模式验证:最优是 128x128,可接受是 64-128 的等宽高。这种"optimal + acceptable"的双层标准提供了更友好的用户体验。 L23 消息 GIF 验证:宽高比 <= 2.0,最小边在 320-640 之间。这是 Slack 消息中 GIF 的实际约束。

easing.py 提供了 12 种缓动函数:linear、ease_in_quad/out_quad/in_out_quad、ease_in_cubic/out_cubic/in_out_cubic、ease_in_bounce/out_bounce、ease_in_elastic/out_elastic、ease_in_back/out_back。所有函数接收 t(0.0-1.0)返回缓动后的值(0.0-1.0)。核心还有一个 interpolate() 函数用于实际插值。

easing.py provides 12 easing functions: linear, ease_in_quad/out_quad/in_out_quad, ease_in_cubic/out_cubic/in_out_cubic, ease_in_bounce/out_bounce, ease_in_elastic/out_elastic, ease_in_back/out_back. All functions take t (0.0-1.0) and return eased value (0.0-1.0). There’s also a core interpolate() function for actual interpolation.

easing.py — 缓动函数与插值 ↗ 源文件
1 EASING_FUNCTIONS = { 2 "linear": linear, 3 "ease_in": ease_in_quad, 4 "ease_out": ease_out_quad, 5 "ease_in_out": ease_in_out_quad, 6 "bounce_out": ease_out_bounce, 7 "elastic_out": ease_out_elastic, 8 "back_out": ease_out_back, 9 } 10 11 def interpolate(start=0, end=1, t=0, easing="linear"): 12 eased_t = EASING_FUNCTIONS[easing](t) 13 return start + (end - start) * eased_t
代码解读
L1 EASING_FUNCTIONS 字典将字符串名称映射到函数对象——这是"策略模式"在 Python 中的简洁实现。用字典替代了 switch/case 或 if/elif 链。 L8 interpolate() 是通用的插值函数。接收 start/end/t/easing,先对 t 应用缓动函数,再进行线性插值。start 为 0、end 为 400 时,返回 0 到 400 之间的缓动位置。

slack-gif-creator 脚本依赖图

graph LR
  A[gif_builder.py] -->|import| B[easing.py]
  C[frame_composer.py]
  D[validators.py]
  A -->|PIL + imageio + numpy| GIF[output.gif]
  D -->|PIL| GIF

  Claude -->|调用| A
  Claude -->|调用| C
  Claude -->|调用| D
  Claude -->|调用| B

  style A fill:#81c784,stroke:#388e3c,color:#000
  style B fill:#ffb74d,stroke:#f57c00,color:#000
  style C fill:#4fc3f7,stroke:#0288d1,color:#000
  style D fill:#ce93d8,stroke:#7b1fa2,color:#000
  style GIF fill:#e0e0e0,stroke:#9e9e9e,color:#000
  1. 领域约束编码化:Slack 的 GIF 约束(128x128、10-30 FPS、48-128 色)被编码在 validators.py 和 SKILL.md 中,确保每次输出都符合平台要求
  2. 模块化架构:4 个模块职责清晰(构建/合成/验证/缓动),可以独立开发和测试
  3. 插值函数抽像:interpolate(start, end, t, easing) 是一个极好的 API 设计——将缓动、插值和动画逻辑分离
  4. 动画模式库:8 种动画模式(shake/pulse/bounce/spin 等)作为知识编码在 SKILL.md 中,而非硬编码在脚本中
  5. 双重品质检查:validators.py 提供程序化约束检查,SKILL.md 提供视觉品质建议(厚线条、深度层次、颜色搭配)

“如果你想为其他平台(Twitter、Discord、WhatsApp)创建媒体生成 Skill…”

  1. 保留模块化架构:Builder + Composer + Validator 分离是最好的起点
  2. 替换领域约束:在 validators.py 中替换 Slack 的维度/大小/时长限制为目标平台的限制
  3. 保留缓动库:easing.py 是通用的,无需修改
  4. 扩展合成工具:frame_composer.py 中添加目标平台特有的元素(如直接显示表情符号)
  5. 调整优化策略:在 gif_builder.py 中添加目标平台特定的压缩参数

⚠️ GIF 文件大小容易超标: Slack 对 GIF 大小有隐式限制(尤其是 emoji)——optimize_for_emoji 的参数组合需要仔细调整

⚠️ 色彩量化影响画质: 48 色的 GIF 看起来可能与原始设计差异很大——需要在 SKILL.md 中列出”颜色数 vs 画质”的权衡

⚠️ 帧率选择需考虑内容: 10 FPS 对快节奏动画不够,30 FPS 对慢动画浪费——没有”万能”的帧率

⚠️ 缓动函数命名需一致: SKILL.md 中使用 “bounce_out” 但在 interpolate() 中使用的是 “ease_out_bounce”——API 命名不一致可能误导 Claude

⚠️ PIL 字体不支持中文/emoji: draw_text 中使用了 ImageFont.load_default(),不支持复杂文本——需要明确告知用户限制

  1. Domain Constraints Encoded: Slack’s GIF constraints (128x128, 10-30 FPS, 48-128 colors) encoded in validators.py and SKILL.md, ensuring every output meets platform requirements
  2. Modular Architecture: 4 modules with clear responsibilities (build/compose/validate/ease), independently developable and testable
  3. Interpolation Abstraction: interpolate(start, end, t, easing) is an excellent API design — decoupling easing, interpolation, and animation logic
  4. Animation Pattern Library: 8 animation patterns encoded as knowledge in SKILL.md, not hard-coded in scripts
  5. Dual Quality Check: validators.py provides programmatic constraint checking, SKILL.md provides visual quality guidance (thick lines, depth, color harmony)

“If you want to create media generation Skills for other platforms (Twitter, Discord, WhatsApp)…”

  1. Keep modular architecture: Builder + Composer + Validator separation is the best starting point
  2. Replace domain constraints: swap Slack’s dimension/size/duration limits in validators.py
  3. Keep easing library: easing.py is universal, no modification needed
  4. Extend composition tools: add platform-specific elements to frame_composer.py
  5. Adjust optimization strategies: add platform-specific compression parameters in gif_builder.py

⚠️ GIF file size easily exceeds limits: Slack has implicit GIF size limits — optimize_for_emoji parameter combination needs careful tuning

⚠️ Color quantization affects quality: A 48-color GIF may look very different from the original design — list “color count vs quality” tradeoffs in SKILL.md

⚠️ Frame rate depends on content: 10 FPS is too slow for fast animation, 30 FPS wasteful for slow animation — no “one-size-fits-all” frame rate

⚠️ Easing function naming inconsistency: SKILL.md uses “bounce_out” but interpolate() uses “ease_out_bounce” — inconsistent API naming may mislead Claude

⚠️ PIL font doesn’t support Chinese/emoji: draw_text uses ImageFont.load_default() which doesn’t support complex text — clearly document this limitation

模式说明适用于...
领域约束编码将平台限制硬编码为验证函数任何针对特定平台的媒体生成 skill
插值抽像interpolate(start, end, t, easing) 统一动画任何需要动画的 Python skill
策略模式字典用字典映射字符串到函数需要多策略选择的 Python 脚本
模块化管线构建/合成/验证/输出职责分离任何多步骤的媒体处理 skill
PatternDescriptionApplies to...
Domain ConstraintsPlatform limitations hard-coded as validation functionsAny platform-specific media generation skill
Interpolation Abstractioninterpolate(start, end, t, easing) unified animationAny Python skill needing animation
Strategy Pattern DictDictionary mapping strings to functionsPython scripts needing multi-strategy selection
Modular PipelineBuild/Compose/Validate/Output separationAny multi-step media processing skill