Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. def do_gmagik(self, ctx, gif):
  2. try:
  3. try:
  4. gif = PIL.Image.open(gif)
  5. except:
  6. return '\N{WARNING SIGN} Invalid Gif.'
  7. if gif.size >= (3000, 3000):
  8. return '\N{WARNING SIGN} `GIF resolution exceeds maximum >= (3000, 3000).`'
  9. elif gif.n_frames > 150 and ctx.message.author.id != self.bot.owner.id:
  10. return "\N{WARNING SIGN} `GIF has too many frames (> 150 Frames).`"
  11. count = 0
  12. frames = []
  13. while gif:
  14. b = BytesIO()
  15. try:
  16. gif.save(b, 'GIF')
  17. except:
  18. continue
  19. b.seek(0)
  20. frames.append(b)
  21. count += 1
  22. try:
  23. gif.seek(count)
  24. except EOFError:
  25. break
  26. imgs2 = []
  27. for image in frames:
  28. try:
  29. im = wand.image.Image(file=image)
  30. except:
  31. continue
  32. i = im.clone()
  33. i.transform(resize='800x800>')
  34. i.liquid_rescale(width=int(i.width*0.5), height=int(i.height*0.5), delta_x=1, rigidity=0)
  35. i.liquid_rescale(width=int(i.width*1.5), height=int(i.height*1.5), delta_x=2, rigidity=0)
  36. i.resize(i.width, i.height)
  37. b = BytesIO()
  38. i.save(file=b)
  39. b.seek(0)
  40. imgs2.append(b)
  41. imgs2 = [PIL.Image.open(i) for i in imgs2]
  42. final = BytesIO()
  43. i = imgs2[0].save(final, 'GIF', loop=0, save_all=True, append_images=imgs2)
  44. final.seek(0)
  45. return final
  46. except Exception as e:
  47. return f'{str(e)} {e.__traceback__.tb_lineno}'
  48.  
  49. async def do_gmagik2(self, url):
  50. path = self.files_path(self.bot.random(True))
  51. await self.download(url, path)
  52. args = ['convert', '(', path, '-resize', '256x256>', '-resize', '256x256<', ')']
  53. i = 5
  54. while i <= 70:
  55. args.extend(['(', '-clone', '0', '(', '+clone', '-liquid-rescale', '{0}%'.format(int(100-i)), ')', '(', '+clone', '-resize', '256', ')', '-delete', '-2', '-delete', '-2', ')'])
  56. i += 5
  57. args.extend(['-delay', '8', '-set', 'delay', '8', 'gif:-'])
  58. final = await self.bot.run_process(args, b=True)
  59. return path, final
  60.  
  61. @commands.command(pass_context=True, aliases=['gmagick'])
  62. @commands.cooldown(1, 10, commands.BucketType.server)
  63. async def gmagik(self, ctx, url:str=None):
  64. try:
  65. x = await self.bot.say("ok, processing (this might take a while)")
  66. get_images = await self.get_images(ctx, urls=url, limit=1, gif=True, msg=False)
  67. if not get_images:
  68. get_images = await self.get_images(ctx, urls=url, limit=2)
  69. if get_images:
  70. url = get_images[0]
  71. if self.get_switch('gmagik2'):
  72. final = await self.generic_api('gmagik2', url)
  73. if isinstance(final, str):
  74. return await self.bot.say(final)
  75. else:
  76. path, final = await self.do_gmagik2(url)
  77. os.remove(path)
  78. await self.bot.upload(final, filename='gmagik.gif')
  79. else:
  80. ctx.command.reset_cooldown(ctx)
  81. return
  82. else:
  83. url = get_images[0]
  84. if self.get_switch(ctx.command.name):
  85. final = await self.g_api(url, ctx.message.author.id, 'gmagik')
  86. if not final:
  87. return await self.bot.say('\N{WARNING SIGN} **Command download function failed...**')
  88. else:
  89. b = await self.bytes_download(url)
  90. if sys.getsizeof(b) > 6500000 and ctx.message.author.id != self.bot.owner.id:
  91. return await self.bot.say("\N{NO ENTRY} `GIF Too Large (> 6.5 mb).`")
  92. final = await self.bot.loop.run_in_executor(None, self.do_gmagik, ctx, b)
  93. if isinstance(final, str):
  94. return await self.bot.say(final)
  95. await self.bot.upload(final, filename='gmagik.gif')
  96. except Exception as e:
  97. await self.bot.say(code.format(e))
  98. raise
  99. finally:
  100. await self.bot.delete_message(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement