Last active
March 3, 2018 01:37
-
-
Save Jan200101/8458acd2e90b71b17fbd9dd4eda49d94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os import mkdir, getcwd | |
| from shutil import rmtree | |
| from imageio import get_writer, imread | |
| try: | |
| from PIL import Image, ImageDraw, ImageFont | |
| from PIL.GifImagePlugin import getheader, getdata | |
| except ImportError: | |
| raise ImportError('Missing Pillow Module') | |
| exit | |
| def create(string, dir_path, length: int = 1): | |
| def _gif(string, dir_path, length: int = 1): | |
| try: | |
| mkdir(dir_path+r'/tempdir') | |
| except: | |
| pass | |
| string += ' ' | |
| for x in range(0, length): | |
| try: | |
| mkdir(dir_path+r'/tempdir/{}'.format(x)) | |
| except: | |
| pass | |
| ctr = 0 | |
| images = [] | |
| fontsize = 30 | |
| font = ImageFont.truetype("arial.ttf", fontsize) | |
| # string[0] + string is used to keep the gif at the first character for one frame longer to show the message clearer | |
| for y in range(len(string[0] + string)): | |
| # 30x30 is the size of emotes | |
| # a black background looks the most pleasing to the eye | |
| # TODO: Find out how to get transparency working with imageio | |
| img = Image.new('RGB', (30, 30), (0, 0, 0)) | |
| d = ImageDraw.Draw(img) | |
| d.text((1, 1), (string[0] + string)[ctr], fill=(255, 255, 255), font=font) | |
| img.save(dir_path + r'/tempdir/{}/'.format(x) + str(ctr) + ".jpeg") | |
| images.append(Image.open(dir_path + r'/tempdir/{}/'.format(x) + str(ctr) + ".jpeg")) | |
| ctr += 1 | |
| with get_writer('gif{}.gif'.format(x), mode='I', duration=0.5) as writer: | |
| for filename in range(0, len(string[0] + string)): | |
| image = imread(r'tempdir/{}/'.format(x) + str(filename) + '.jpeg') | |
| writer.append_data(image) | |
| string = string[1:] + string[:1] | |
| # ImageIO uses the files until function end. Nesting functions as workaround | |
| _gif(string, dir_path, length) | |
| rmtree(r'tempdir') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment