Skip to content

Commit 2a95549

Browse files
committed
added gif stuff
1 parent b78e926 commit 2a95549

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

discord_bot/cogs/commands.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import discord
2+
import io
23
from discord.ext import commands
34
from discord import app_commands
45
from util.state import StateBot
56
from util.embeds import Embeds
67
from util.views import JudgeView
8+
from util.gifs import perform_gif_overlay
79
from random import shuffle
810
from datetime import date, timedelta
911

@@ -114,6 +116,14 @@ async def whojudge_debug(self, interaction: discord.Interaction):
114116
)
115117
)
116118

119+
@app_commands.command(name="screen", description="ragebait")
120+
@app_commands.describe(gif="GIF URL")
121+
async def screen(self, interaction: discord.Interaction, gif: str):
122+
res = perform_gif_overlay(gif)
123+
return await interaction.response.send_message(
124+
file=discord.File(io.BytesIO(res), filename="output.gif")
125+
)
126+
117127

118128
async def setup(bot):
119129
await bot.add_cog(Commands(bot))

discord_bot/util/gifs.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import ffmpeg
2+
import os
3+
4+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
5+
6+
W, H = 300, 300
7+
PAD = 52
8+
W_PAD = W
9+
H_PAD = H + PAD
10+
11+
12+
def perform_gif_overlay(URL):
13+
input_img = ffmpeg.input(
14+
URL,
15+
)
16+
17+
overlay_gif = input_img.filter("scale", W, H)
18+
19+
overlay_gif = overlay_gif.filter("pad", W_PAD, H_PAD, 0, 0)
20+
21+
overlay_gif = overlay_gif.filter(
22+
"perspective",
23+
x0="0",
24+
y0="0",
25+
x1=str(117),
26+
y1="36",
27+
x2="100",
28+
y2=str(414),
29+
x3=str(202),
30+
y3=str(374),
31+
sense="destination",
32+
)
33+
34+
mask_alpha = (
35+
ffmpeg.input(os.path.join(BASE_DIR, "mask_bw.png"))
36+
.filter("format", "gray") # grayscale from black/white
37+
.filter("format", "yuva444p") # add alpha channel
38+
.filter("colorchannelmixer", aa=1.0) # set alpha from luma
39+
)
40+
41+
# Merge alpha into base
42+
masked = ffmpeg.filter([overlay_gif, mask_alpha], "alphamerge")
43+
44+
monitor = ffmpeg.input(os.path.join(BASE_DIR, "hunched.jpg"))
45+
46+
combined = ffmpeg.overlay(monitor, masked, x=182, y=129)
47+
48+
# ffmpeg.output(combined, "output.gif", loop=0).run()
49+
out_bytes, _ = ffmpeg.output(combined, "pipe:", format="gif", loop=0).run(
50+
capture_stdout=True
51+
)
52+
return out_bytes

discord_bot/util/hunched.jpg

50.2 KB
Loading

discord_bot/util/mask_bw.png

10.5 KB
Loading

0 commit comments

Comments
 (0)