|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "5bda0cfe", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 8, |
| 14 | + "id": "e7f83b9c", |
| 15 | + "metadata": { |
| 16 | + "ExecuteTime": { |
| 17 | + "end_time": "2022-10-18T22:20:53.560637Z", |
| 18 | + "start_time": "2022-10-18T22:20:52.693953Z" |
| 19 | + } |
| 20 | + }, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "from PIL import Image, ImageDraw, ImageFont\n", |
| 24 | + "\n", |
| 25 | + "fnt = ImageFont.truetype(\"Arial.ttf\", 36)\n", |
| 26 | + "\n", |
| 27 | + "def create_image_with_text(wh, text):\n", |
| 28 | + " width, height = wh\n", |
| 29 | + " img = Image.new('RGB', (300, 50), (245,230,180))\n", |
| 30 | + " draw = ImageDraw.Draw(img)\n", |
| 31 | + " # draw.ellipse takes a 4-tuple (x0, y0, x1, y1) where (x0, y0) is the top-left bound of the box\n", |
| 32 | + " # and (x1, y1) is the lower-right bound of the box.\n", |
| 33 | + " draw.text((width, height), text, font=fnt, fill=\"black\")\n", |
| 34 | + " return img\n", |
| 35 | + "# Create the frames\n", |
| 36 | + "frames = []\n", |
| 37 | + "x, y = 300, 0\n", |
| 38 | + "for i in range(250):\n", |
| 39 | + " new_frame = create_image_with_text((x,y), \"Have you ever *watched* a 4D-array?\")\n", |
| 40 | + " frames.append(new_frame)\n", |
| 41 | + " x -= 4\n", |
| 42 | + "\n", |
| 43 | + "# Save into a GIF file that loops forever\n", |
| 44 | + "frames[0].save('moving_text.gif', format='GIF',\n", |
| 45 | + " append_images=frames[1:], save_all=True, duration=30, loop=0)" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 35, |
| 51 | + "id": "467a40a3", |
| 52 | + "metadata": { |
| 53 | + "ExecuteTime": { |
| 54 | + "end_time": "2022-07-05T09:04:59.719192Z", |
| 55 | + "start_time": "2022-07-05T09:04:59.523313Z" |
| 56 | + } |
| 57 | + }, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "from PIL import Image, ImageDraw, ImageFont\n", |
| 61 | + "\n", |
| 62 | + "fnt = ImageFont.truetype(\"OpenSans-SemiBold.ttf\", 36)\n", |
| 63 | + "def create_image_with_text(wh, text):\n", |
| 64 | + " global img\n", |
| 65 | + " width, height = wh\n", |
| 66 | + " img = Image.new('RGB', (650, 55), (55, 55, 55))\n", |
| 67 | + " draw = ImageDraw.Draw(img)\n", |
| 68 | + " # draw.ellipse takes a 4-tuple (x0, y0, x1, y1) where (x0, y0) is the top-left bound of the box\n", |
| 69 | + " # and (x1, y1) is the lower-right bound of the box.\n", |
| 70 | + " draw.text((width, height), text, font = fnt, fill=(240,210,130))\n", |
| 71 | + " return img\n", |
| 72 | + "\n", |
| 73 | + "# Create the frames\n", |
| 74 | + "frames = []\n", |
| 75 | + "x, y = 0, 0\n", |
| 76 | + "string = \"have you ever *watched* a 4D-array?\"\n", |
| 77 | + "\n", |
| 78 | + "for i in range(len(string)):\n", |
| 79 | + " new_frame = create_image_with_text((x,y), string[:i+1])\n", |
| 80 | + " frames.append(new_frame)\n", |
| 81 | + "\n", |
| 82 | + "for i in range(5):\n", |
| 83 | + " frames.append(new_frame)\n", |
| 84 | + "\n", |
| 85 | + "# Save into a GIF file that loops forever\n", |
| 86 | + "frames[0].save('progressive_text.gif', format='GIF',\n", |
| 87 | + " append_images=frames[1:], save_all=True, duration=100, loop=0)" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "id": "94682e99", |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [] |
| 97 | + } |
| 98 | + ], |
| 99 | + "metadata": { |
| 100 | + "kernelspec": { |
| 101 | + "display_name": "Python 3 (ipykernel)", |
| 102 | + "language": "python", |
| 103 | + "name": "python3" |
| 104 | + }, |
| 105 | + "language_info": { |
| 106 | + "codemirror_mode": { |
| 107 | + "name": "ipython", |
| 108 | + "version": 3 |
| 109 | + }, |
| 110 | + "file_extension": ".py", |
| 111 | + "mimetype": "text/x-python", |
| 112 | + "name": "python", |
| 113 | + "nbconvert_exporter": "python", |
| 114 | + "pygments_lexer": "ipython3", |
| 115 | + "version": "3.10.6" |
| 116 | + }, |
| 117 | + "toc": { |
| 118 | + "base_numbering": 1, |
| 119 | + "nav_menu": {}, |
| 120 | + "number_sections": true, |
| 121 | + "sideBar": true, |
| 122 | + "skip_h1_title": false, |
| 123 | + "title_cell": "Table of Contents", |
| 124 | + "title_sidebar": "Contents", |
| 125 | + "toc_cell": false, |
| 126 | + "toc_position": {}, |
| 127 | + "toc_section_display": true, |
| 128 | + "toc_window_display": false |
| 129 | + } |
| 130 | + }, |
| 131 | + "nbformat": 4, |
| 132 | + "nbformat_minor": 5 |
| 133 | +} |
0 commit comments