Created
March 14, 2018 10:12
-
-
Save Aluriak/952d495e8e1a5baa2f8eb1d667631274 to your computer and use it in GitHub Desktop.
gif writer with pillow
This file contains 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
"""Write a gif where a line moves horizontally. | |
Note that this code does not work with pillow 3.4.0 | |
(only the first frame is written). | |
Tested with pillow 5.0.0. | |
""" | |
from PIL import Image | |
if __name__ == "__main__": | |
SIZE = 100 | |
images = [] | |
# write the gif | |
for offset in range(SIZE): | |
img = Image.new('L', (SIZE, SIZE), 0) | |
pixmap = img.load() | |
for row in range(SIZE): | |
pixmap[offset, row] = 255 | |
images.append(img) | |
first, *lasts = images | |
assert lasts, "only one frame in gif" | |
first.save('progress-bar.gif', save_all=True, append_images=images, duration=10) | |
# count number of frames | |
gif = Image.open('progress-bar.gif') | |
nb_frame = 1 | |
try: | |
while True: | |
gif.seek(gif.tell()+1) | |
nb_frame += 1 | |
except EOFError: | |
pass | |
assert nb_frame == SIZE, 'nb frame: ' + str(nb_frame) | |
def basic_use(): | |
import random | |
from PIL import ImageDraw | |
img = Image.new('RGB', (400, 400), (255, 255, 255)) | |
draw = ImageDraw.Draw(img) | |
draw.line((0, 5, 10, 5), (0, 0, 0), 2) | |
draw.line((5, 0, 5, 10), (0, 0, 0), 2) | |
img.save("test-bad-lines.png", 'PNG') | |
rand = lambda: random.randint(0, 255) | |
rand_color = lambda: (rand(), rand(), rand()) | |
pixmap = img.load() | |
for i in range(400): | |
for j in range(400): | |
pixmap[i, j] = rand_color() | |
img.save("test-random.png", 'PNG') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment