Slack GIF 建立工具 - 彈性工具包
一個用於建立針對 Slack 最佳化的動畫 GIF 的工具包。提供 Slack 限制驗證器、可組合的動畫基本元件,以及選擇性的輔助工具。可根據創意需求任意套用這些工具。
Slack 的需求
Slack 對 GIF 有特定的要求,取決於使用情境:
訊息 GIF:
- 最大大小:約 2MB
- 最佳尺寸:480x480
- 典型 FPS:15-20
- 顏色限制:128-256
- 持續時間:2-5 秒
表情符號 GIF:
- 最大大小:64KB(嚴格限制)
- 最佳尺寸:128x128
- 典型 FPS:10-12
- 顏色限制: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
# Jello 搖晃
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
)
若要實作自訂文字渲染,可在較大的 GIF 中使用 PIL 的 ImageDraw.text()。
色彩管理
專業外觀的 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 種顏色
- 避免漸層(實色壓縮效果更好)
- 簡化設計(較少的元素)
- 在儲存方法中使用
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)