6
6
7
7
from PIL import Image
8
8
9
+ from .formatter import FileFormatter
9
10
from .formatter import FormatterFactory
10
- from .formatter import ThumbnailFormat
11
11
from .thumbnails import Thumbnails
12
12
from .thumbnails import arange
13
13
17
17
DEFAULT_BASEPATH = ""
18
18
19
19
20
- def register_format (typename ):
21
- """Register a new thumbnail format to the factory."""
20
+ def register_formatter (typename ):
21
+ """Register a new thumbnail formatter to the factory."""
22
22
23
- def _registrator (cls ):
24
- if not issubclass (cls , ThumbnailFormat ):
25
- raise ValueError ("Thumbnail format must implement"
26
- "the ThumbnailFormat interface." )
23
+ def _register_factory (cls ):
24
+ if not issubclass (cls , FileFormatter ):
25
+ raise ValueError ("The formatter must implement the FileFormatter interface." )
27
26
28
27
cls .extension = typename
29
28
FormatterFactory .thumbnails [typename ] = cls
30
29
return cls
31
30
32
- return _registrator
31
+ return _register_factory
33
32
34
33
35
- @register_format ("vtt" )
36
- class VTT ( ThumbnailFormat ):
34
+ @register_formatter ("vtt" )
35
+ class VTTFormatter ( FileFormatter ):
37
36
"""Implements the methods for generating thumbnails in the WebVTT format."""
38
37
39
38
def __init__ (self , video ):
40
39
super ().__init__ (video )
41
40
self ._master_name = self .filename + ".png"
42
41
43
42
def prepare_thumbnails (self ):
44
- _thumbnails = self .video . thumbnails (True )
43
+ _thumbnails = self .thumbnails (True )
45
44
master = Image .new (mode = "RGBA" , size = next (_thumbnails ))
46
45
47
- for frame , start , end , x , y in self .video . thumbnails ():
46
+ for frame , start , end , x , y in self .thumbnails ():
48
47
with Image .open (frame ) as image :
49
48
image = image .resize ((self .width , self .height ), Image .ANTIALIAS )
50
49
master .paste (image , (x , y ))
@@ -60,19 +59,19 @@ def _format_time(secs):
60
59
_lines = ["WEBVTT\n \n " ]
61
60
_img_src = self .basepath + self ._master_name
62
61
63
- for frame , start , end , x , y in self .video . thumbnails ():
62
+ for frame , start , end , x , y in self .thumbnails ():
64
63
_thumbnail = "%s --> %s\n %s#xywh=%d,%d,%d,%d\n \n " % (
65
64
_format_time (start ), _format_time (end ),
66
65
_img_src , x , y , self .width , self .height
67
66
)
68
67
_lines .append (_thumbnail )
69
68
70
- with open (self .output_format , "w" ) as fp :
69
+ with open (self .thumbnail_file , "w" ) as fp :
71
70
fp .writelines (_lines )
72
71
73
72
74
- @register_format ("json" )
75
- class JSON ( ThumbnailFormat ):
73
+ @register_formatter ("json" )
74
+ class JSONFormatter ( FileFormatter ):
76
75
"""Implements the methods for generating thumbnails in the JSON format."""
77
76
78
77
def __init__ (self , video ):
@@ -88,7 +87,7 @@ def prepare_thumbnails(self):
88
87
def generate (self ):
89
88
_content = {}
90
89
91
- for frame , start , end , x , y in self .video . thumbnails ():
90
+ for frame , start , end , x , y in self .thumbnails ():
92
91
frame = self ._outdir + os .sep + os .path .split (frame )[1 ]
93
92
with Image .open (frame ) as image :
94
93
image .resize ((self .width , self .height ), Image .ANTIALIAS ).save (frame )
@@ -98,16 +97,16 @@ def generate(self):
98
97
}
99
98
_content [int (start )] = _thumbnail
100
99
101
- with open (self .output_format , "w" ) as fp :
100
+ with open (self .thumbnail_file , "w" ) as fp :
102
101
json .dump (_content , fp , indent = 2 )
103
102
104
103
105
104
__version__ = "v1.0"
106
105
__all__ = (
107
- FormatterFactory ,
108
- register_format ,
109
- ThumbnailFormat ,
110
- Thumbnails ,
111
- JSON ,
112
- VTT ,
106
+ "register_formatter" ,
107
+ "FormatterFactory" ,
108
+ "FileFormatter" ,
109
+ "JSONFormatter" ,
110
+ "VTTFormatter" ,
111
+ "Thumbnails" ,
113
112
)
0 commit comments