-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgifduration.py
More file actions
executable file
·89 lines (71 loc) · 2.42 KB
/
Copy pathgifduration.py
File metadata and controls
executable file
·89 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# encoding: utf-8
"""
This script takes one or more animated GIF files as input and calculates their
total durations, returning zero for non-animated GIF files.
Requires Pillow, formerly known as the Python Imaging Library (PIL).
Code by Markus Amalthea Magnuson <markus@polyscopic.works>
"""
from __future__ import print_function
import getopt
import os.path
import sys
from PIL import Image, ImageSequence
help_message = """
Supply one or more animated GIF files as input to get their total durations.
"""
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h:v", ["help", "verbose"])
except getopt.error as err:
raise Usage(err.msg)
# Option processing.
verbose = False
for option, value in opts:
if option in ("-v", "--verbose"):
verbose = True
if option in ("-h", "--help"):
raise Usage(help_message)
except Usage as err:
print(sys.argv[0].split("/")[-1] + ": " + str(err.msg), file=sys.stderr)
print("\t for help use --help", file=sys.stderr)
return 2
# Start processing the images.
for path in args:
try:
im = Image.open(path)
except IOError as err:
print("%s:" % os.path.basename(path), file=sys.stderr)
print(err, file=sys.stderr)
print("---", file=sys.stderr)
continue
durations = []
for frame in ImageSequence.Iterator(im):
try:
durations.append(frame.info["duration"])
except KeyError:
# Ignore if there was no duration, we will not count that frame.
pass
if not durations:
print("Not an animated GIF image")
else:
if verbose:
for index, duration in enumerate(durations):
print(
"Frame %d: %d ms (%0.2f seconds)"
% (index + 1, duration, duration / 1000.0)
)
total_duration = sum(durations)
print(
"Total duration: %d ms (%0.2f seconds)"
% (total_duration, total_duration / 1000.0)
)
print("---")
if __name__ == "__main__":
sys.exit(main())