Skip to content

Commit

Permalink
Working on better gif support (mostly)
Browse files Browse the repository at this point in the history
Related to #4
Export each frame to a .jpg file to be reassembled has resolved issues of all frames being fully transparent; however, some gifs only contain color diffs for subsequent frames instead of complete frames. Further work is needed to determine if cumulative jpg assembly/exports can be accomplished when saving each modulated frame to a .jpg file.
Also worked on helpful user messages for the bot; checked for file size of resulting file before attempting to send to avoid rejection by Discord for files over 8MB. (Also did other small maintenance things.)
  • Loading branch information
etcadinfinitum committed Dec 16, 2018
1 parent 4bb2a77 commit 497a4e4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ __pycache__/
.*.swp
extra/generating_images_demo/
env/
extra/*/
28 changes: 23 additions & 5 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def mkgif(filename):
framecount = 0
for frame in original.sequence:
with Image(frame) as mod_frame:
mod_frame.modulate(-800, 200)
mod_frame.modulate(200, -800)
mod_frame.evaluate(operator='gaussiannoise', value=0.05)
mod_frame.function('sinusoid', params)
# mod_frame.function('sinusoid', params)
mod_frame.save(filename=(outdir + '/' + ('0' * (zeros - len(str(framecount)))) + str(framecount) + '.jpg'))
framecount += 1
frames = sorted(glob.glob(outdir + '/*.jpg'))
Expand Down Expand Up @@ -87,10 +87,13 @@ async def on_message(message):

# help messages are good ^^,
if message.content.startswith('deepfriedHELP'):
msg = 'Welcome to THE DEEP FRYER.\nUse your powers wisely.\n\n'
msg += 'Commands:\tAttachment:\tResult:\nFryThis\t\t\tjpg, png\t\t\tHecking FRIED\n'
msg += 'FryThis\t\t\tNone\t\t\t\tTry it!\n\n'
msg = '```Welcome to THE DEEP FRYER.\nUse your powers wisely.\n\n'
msg += 'Commands: Attachment: Result:\n'
msg += 'FryThis jpg, png Hecking FRIED\n'
msg += 'FryThis None Try it!\n\n'
msg += 'BETA FEATURES:\n'
msg += 'The Phryer supports GIFs! (Mostly.) \nInvoke with the \'FryThis\' command.\n'
msg += 'For an extra serving of weird, add \'chaos\' to your command.```'
await client.send_message(message.channel, msg)

# FRY THIS
Expand Down Expand Up @@ -125,15 +128,30 @@ async def on_message(message):
if is_valid:
if is_gif:
result_file, text = mkgif(result_file)
if os.path.getsize(result_file) > 7800000:
text = 'The fried gif is too large; RIP. '
if not creds.SUPPORT_LINKS:
text += 'Harass your admin to get image links supported.'
else:
text += 'The image link is: '
# need logic here to supply a link to the image
await client.send_file(message.channel, result_file, content=text, filename="test.gif")
else:
result_file, text = deepfry(result_file)
if os.path.getsize(result_file) > 7800000:
text = 'The fried image is too large; RIP. '
if not creds.SUPPORT_LINKS:
text += 'Harass your admin to get image links supported.'
else:
text += 'The image link is: '
# need logic here to supply a link to the image
await client.send_file(message.channel, result_file, content=text, filename="test.jpg")
else:
# tell the user they're a dumbass for trying to fry a non-img file type
pass
result_file = None
text = 'Send an image file, dumbass.'
await client.send_message(message.channel, text)
# the sender did not send an attachment; oops
else:
phrase = ['You forgot to take out the garbage, you ', 'You must construct additional memes, you ', 'Get your own dang meme, ', '?????, you ', 'I\'d give you a nasty look but you\'ve already got one, ', 'You are living proof that morons are a subatomic particle, ', 'You must be a cactus because you\'re a prick, you ', 'I was hoping for a battle of memes but you appear to be unarmed, you ', 'Cool story, ']
Expand Down
78 changes: 78 additions & 0 deletions extra/testing_overlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
from wand.image import Image
from shutil import copyfile
import glob
import pdb
import logging

modulate_presets_1 = { (-100, -200): 'dark'}
modulate_presets_2 = { (200, 300): 'default', (-1000, -1000): 'galaxy', (200, -800): 'flir'}
cwd = './defaults_test/'

def mkgif(filename):
outfiles = ['testing_overlay/no_over.gif', 'testing_overlay/combine_prev_frame.gif']
# outfile = tempfile.mkstemp(prefix="deepfry", suffix=".gif")[1]
combine = False
for outfile in outfiles:
logging.info("About to run mkgif(%s) -> %s", filename, outfile)
outdir = outfile.split('.')[0]
tmpfile=outdir + '/tmp.jpg'
if not os.path.isdir(outdir):
os.mkdir(outdir)
with Image(filename=filename) as original:
zeros = len(str(len(original.sequence)))
framecount = 0
for frame in original.sequence:
if not combine:
print('testing no composite call for result file %s' % outfile)
with Image(frame) as mod_frame:
mod_frame.modulate(200, -800)
mod_frame.evaluate(operator='gaussiannoise', value=0.05)
# mod_frame.function('sinusoid', params)
mod_frame.save(filename=(outdir + '/' + ('0' * (zeros - len(str(framecount)))) + str(framecount) + '.jpg'))
framecount += 1
else:
print('TESTING COMPOSITE CALL for result file %s' % outfile)
do_prev = False
if framecount == 0:
print('frying first image')
with Image(frame) as mod_frame:
with Image(mod_frame) as img:
img.save(filename=tmpfile)
mod_frame.modulate(200, -800)
mod_frame.evaluate(operator='gaussiannoise', value=0.05)
# mod_frame.function('sinusoid', params)
mod_frame.save(filename=(outdir + '/' + ('0' * (zeros - len(str(framecount)))) + str(framecount) + '.jpg'))
else:
print('frying subsequent images: overlay with previous??')
with Image(tmpfile) as previous:
with Image(frame) as new:
previous.composite(new, left=0, top=0)
with Image(previous) as img:
img.save(filename=tmpfile)
previous.modulate(200, -800)
previous.evaluate(operator='gaussiannoise', value=0.05)
# previous.function('sinusoid', params)
previous.save(filename=(outdir + '/' + ('0' * (zeros - len(str(framecount)))) + str(framecount) + '.jpg'))
framecount += 1
os.remove(tmpfile)
frames = sorted(glob.glob(outdir + '/*.jpg'))
with Image() as final:
for frame in frames:
with Image(filename=frame) as img:
final.sequence.append(img)
final.type='optimize'
final.save(filename=outfile)
combine = True
return outfile, None

if not os.path.isdir(cwd):
os.mkdir(cwd)
else:
for filename in glob.glob(cwd + 'modulate*.jpg'):
os.remove(filename)

def main():
mkgif('./testing_overlay/signal-2018-12-15-201849.gif')

main()

0 comments on commit 497a4e4

Please sign in to comment.