Last active
May 30, 2024 17:19
-
-
Save meysampg/d2d74e6cb9e59a0c401c9187815538b3 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 PIL import Image, ImageDraw, ImageFont | |
| def get_text_dimensions(text_string, font): | |
| # https://stackoverflow.com/a/46220683/9263761 | |
| ascent, descent = font.getmetrics() | |
| text_width = font.getmask(text_string).getbbox()[2] | |
| text_height = font.getmask(text_string).getbbox()[3] + descent | |
| return (text_width, text_height) | |
| def frange(start, stop=None, step=None): | |
| start = float(start) | |
| if stop == None: | |
| stop = start + 0.0 | |
| start = 0.0 | |
| if step == None: | |
| step = 1.0 | |
| count = 0 | |
| while True: | |
| temp = float(start + count * step) | |
| if step > 0 and temp >= stop: | |
| yield stop | |
| break | |
| elif step < 0 and temp <= stop: | |
| break | |
| yield temp | |
| count += 1 | |
| def addText(x, y, text, font): | |
| img = Image.new('RGB', (64, 64), 'white') | |
| d = ImageDraw.Draw(img) | |
| d.text((x, y), text, font=font, fill=(0, 0, 0)) | |
| return img | |
| font = ImageFont.truetype('Vazir-Regular.ttf', 60) | |
| text = "نمیدانم اطلاعی ندارم" | |
| text_size = get_text_dimensions(text, font) | |
| images = [] | |
| n = text_size[0] | |
| step = float(n) // 50.0 | |
| for i in frange(0, n+128, step): | |
| images.append(addText(-n+i, -17, text, font)) | |
| images[0].save("enterexit.gif", save_all=True, append_images=images[1:], | |
| optimize=False, duration=65, loop=0) |
meysampg
commented
Dec 31, 2022
Author

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment