forked from LindseyB/starwars-dot-gif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeGifs.py
160 lines (130 loc) · 4.34 KB
/
makeGifs.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
from images2gif import writeGif
from PIL import Image, ImageFont, ImageDraw
from numpy import array
import os
import re
import ConfigParser
import pysrt
import random
sub_files = { 4: 'subs/IV-A.New.Hope[1977]DvDrip-aXXo.srt',
5: 'subs/V-The.Empire.Strikes.Back[1980]DvDrip-aXXo.srt',
6: 'subs/VI-Return.Of.The.Jedi[1983]DvDrip-aXXo.srt' }
def striptags(data):
# I'm a bad person, don't ever do this.
# Only okay, because of how basic the tags are.
p = re.compile(r'<.*?>')
return p.sub('', data)
def drawText(draw, x, y, text, font):
# black outline
draw.text((x-1, y),text,(0,0,0),font=font)
draw.text((x+1, y),text,(0,0,0),font=font)
draw.text((x, y-1),text,(0,0,0),font=font)
draw.text((x, y+1),text,(0,0,0),font=font)
# white text
draw.text((x, y),text,(255,255,255),font=font)
def makeGif(source, sub_index, rand=False, no_quote=False, custom_subtitle=""):
config = ConfigParser.ConfigParser()
config.read("config.cfg")
config.sections()
vlc_path = config.get("general", "vlc_path")
video_path = config.get("general", "ep"+str(source)+"_path")
screencap_path = os.path.join(os.path.dirname(__file__), "screencaps")
# delete the contents of the screencap path
file_list = os.listdir(screencap_path)
for file_name in file_list:
os.remove(os.path.join(screencap_path, file_name))
# read in the quotes for the selected movie
subs = pysrt.open(sub_files[source])
if rand:
sub_index = random.randint(0, len(subs)-1)
if no_quote:
start = (3600 * subs[sub_index].end.hours) + (60 * subs[sub_index].end.minutes) + subs[sub_index].end.seconds + (0.001*subs[sub_index].end.milliseconds)
end = (3600 * subs[sub_index+1].start.hours) + (60 * subs[sub_index+1].start.minutes) + subs[sub_index+1].start.seconds + (0.001*subs[sub_index+1].start.milliseconds)
else:
start = (3600 * subs[sub_index].start.hours) + (60 * subs[sub_index].start.minutes) + subs[sub_index].start.seconds + (0.001*subs[sub_index].start.milliseconds)
end = (3600 * subs[sub_index].end.hours) + (60 * subs[sub_index].end.minutes) + subs[sub_index].end.seconds + (0.001*subs[sub_index].end.milliseconds)
text = striptags(subs[sub_index].text).split("\n")
if len(custom_subtitle) > 0:
text = [custom_subtitle]
# tell vlc to go get images for gifs
cmd = " ".join([
'"{vlc_path}"',
'-Idummy',
'--video-filter',
'scene',
'-V',
'dummy',
'--no-audio',
'--scene-height=256',
'--scene-width=512',
'--scene-format=png',
'--scene-ratio=1',
'--start-time={start}',
'--stop-time={end}',
'--scene-prefix=thumb',
'--scene-path="{screencap_path}"',
'"{video_path}"',
'vlc://quit'
]).format(**{
"vlc_path": vlc_path,
"start": start,
"end": end,
"screencap_path": screencap_path,
"video_path": video_path
})
os.popen(cmd)
file_names = sorted((fn for fn in os.listdir(screencap_path)))
images = []
font = ImageFont.truetype("fonts/DejaVuSansCondensed-BoldOblique.ttf", 16)
# remove the first image from the list
file_names.pop(0)
for f in file_names:
try:
image = Image.open(os.path.join(screencap_path,f))
draw = ImageDraw.Draw(image)
try:
image_size
except NameError:
image_size = image.size
# deal with multi-line quotes
try:
if len(text) == 2:
# at most 2?
text_size = font.getsize(text[0])
x = (image_size[0]/2) - (text_size[0]/2)
y = image_size[1] - (2*text_size[1]) - 5 # padding
drawText(draw, x, y, text[0], font)
text_size = font.getsize(text[1])
x = (image_size[0]/2) - (text_size[0]/2)
y += text_size[1]
drawText(draw, x, y, text[1], font)
else:
text_size = font.getsize(text[0])
x = (image_size[0]/2) - (text_size[0]/2)
y = image_size[1] - text_size[1] - 5 # padding
drawText(draw, x, y, text[0], font)
except NameError:
pass
# do nothing.
# if not all black?
if image.getbbox():
# add it to the array
images.append(array(image))
print 'image appended.'
else:
print 'all black frame found.'
except IOError:
print 'empty frame found.'
filename = "star_wars.gif"
# create a fuckin' gif
print "generating gif..."
writeGif(filename, images, nq=10, dither=True)
if rand:
try:
return text
except:
return []
if __name__ == '__main__':
# by default we create a random gif
makeGif(random.randint(4,6), 0, rand=True, no_quote=bool(random.getrandbits(1)))