Slack GIF 製作工具 - 彈性工具組
一套用於建立專為 Slack 最佳化的動畫 GIF 工具組。內含 Slack 限制驗證器、可組合的動畫基本元件,以及選用的輔助工具。根據創意需求,自由套用這些工具即可。
Slack 的需求
Slack 對 GIF 有特定的要求,依用途而異:
訊息用 GIF:
- 最大大小:約 2MB
- 最佳尺寸:480x480
- 典型幀率:15-20 FPS
- 色彩限制:128-256 色
- 持續時間:2-5 秒
表情符號用 GIF:
- 最大大小:64KB(嚴格限制)
- 最佳尺寸:128x128
- 典型幀率:10-12 FPS
- 色彩限制:32-48 色
- 持續時間:1-2 秒
表情符號 GIF 極具挑戰性 — 64KB 的限制非常嚴格。有幫助的策略如下:
- 將總幀數限制在 10-15 幀
- 最多使用 32-48 色
- 保持設計簡單
- 避免漸層
- 經常驗證檔案大小
工具組結構
此技能提供三種類型的工具:
- 驗證器 — 檢查 GIF 是否符合 Slack 的需求
- 動畫基本元件 — 可組合的運動建構區塊(抖動、彈跳、移動、萬花筒)
- 輔助工具 — 用於常見需求的選用函式(文字、色彩、特效)
這些工具可完全自由地依照任何方式套用。
核心驗證器
為確保 GIF 符合 Slack 的限制,請使用這些驗證器:
from core.gif_builder import GIFBuilder
# 建立 GIF 後,檢查是否符合需求
builder = GIFBuilder(width=128, height=128, fps=10)
# ... 以任何方式加入您的幀 ...
# 儲存並檢查大小
info = builder.save('emoji.gif', num_colors=48, optimize_for_emoji=True)
# save 方法會在檔案超過限制時自動發出警告
# info 字典包含:size_kb, size_mb, frame_count, duration_seconds
檔案大小驗證器:
from core.validators import check_slack_size
# 檢查 GIF 是否符合大小限制
passes, info = check_slack_size('emoji.gif', is_emoji=True)
# 回傳:(True/False, 包含大小細節的字典)
尺寸驗證器:
from core.validators import validate_dimensions
# 檢查尺寸
passes, info = validate_dimensions(128, 128, is_emoji=True)
# 回傳:(True/False, 包含尺寸細節的字典)
完整驗證:
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' # 或 'horizontal', 'vertical'
)
彈跳
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')
# 用於表情符號 GIF 的注意脈衝
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')
# 用於表情符號 GIF 的興奮搖晃
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')
# 用於表情符號 GIF 的快速翻轉
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')
# 旋轉變形(類似 3D 翻轉)
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')
# 或:x, y = calculate_arc_motion(start, end, height, t)
萬花筒效果
from templates.kaleidoscope import apply_kaleidoscope, create_kaleidoscope_animation
# 套用於單幀
kaleido_frame = apply_kaleidoscope(frame, segments=8)
# 或建立動畫萬花筒
frames = create_kaleidoscope_animation(
base_frame=my_frame, # 或為 None 以使用示範圖案
num_frames=30,
segments=8,
rotation_speed=1.0
)
# 簡單鏡像效果(更快)
from templates.kaleidoscope import apply_simple_mirror
mirrored = apply_simple_mirror(frame, mode='quad') # 4 向鏡像
# 模式:'horizontal', 'vertical', 'quad', 'radial'
要自由組合基本元件,請遵循以下模式:
# 範例:彈跳 + 抖動以產生衝擊
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')
# 在衝擊時(y 到達地面時)加入抖動
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'], # 60px
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') # 或 'pastel', 'dark', 'neon', 'professional'
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):
- 減少幀數(降低 FPS 或縮短持續時間)
- 減少色彩(128 → 64 色)
- 減少尺寸(480x480 → 320x320)
- 啟用重複幀移除
針對表情符號 GIF(>64KB)— 要積極處理:
- 將總幀數限制在 10-12 幀
- 最多使用 32-40 色
- 避免漸層(純色壓縮效果更好)
- 簡化設計(減少元素)
- 在 save 方法中使用
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)
載入動畫
frames = create_loading_spinner(spinner_type='dots')
builder = GIFBuilder(128, 128, 10)
for frame in frames:
builder.add_frame(frame)
builder.save('loading.gif', num_colors=40, optimize_for_emoji=True)
平台支援
此技能設計為與支援 Python 的 AI 代理平台相容,包括 Claude Code、Cursor 和 Codex。透過提供的 Python 模組,使用標準的影像處理程式庫(如 Pillow)以程式化方式建立 GIF。