-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopify.py
More file actions
executable file
·175 lines (118 loc) · 4.41 KB
/
loopify.py
File metadata and controls
executable file
·175 lines (118 loc) · 4.41 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
import time
import os
import os.path
import subprocess
import logging
import tempfile
import PIL.Image as Image
import ConfigParser
if os.uname() == 'Darwin':
from watchdog.observers.fsevents import FSEventsObserver as Observer
else:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import multiprocessing
def loopify(gifsicle, queue, outdir, maxwidth=None):
gif_queue = []
tmpdir = tempfile.gettempdir()
for path in queue:
fname = os.path.basename(path)
fname, ext = os.path.splitext(fname)
gif = os.path.join(tmpdir, "%s.gif" % fname)
im = Image.open(path)
sz = im.size
# TO DO: account for tall things...
if maxwidth:
w = int(maxwidth)
wpercent = (w/float(sz[0]))
h = int((float(sz[1]) * float(wpercent)))
im = im.resize((w, h))
im = im.convert('P', palette=Image.ADAPTIVE)
im.save(gif)
if not os.path.exists(gif):
continue
gif_queue.append(gif)
os.unlink(path)
if len(gif_queue) == 0:
return False
outname = "loopr-%s.gif" % int(time.time())
outfile = os.path.join(outdir, outname)
logging.debug("write new loop to %s" % outfile)
args = [
gifsicle,
"-w",
"--loopcount",
"--careful",
]
# passing /some/dir/*.gif makes subcommands
# freak out and generally be sad
# also, passing very long arguments makes
# something fail - probably a args list is
# too long error...
args.extend(gif_queue)
args.extend(["-o", outfile])
logging.debug(args)
try:
subprocess.check_call(args)
except Exception, e:
logging.error(e)
return
for path in gif_queue:
os.unlink(path)
pool = multiprocessing.Pool()
#
class Eyeballs(FileSystemEventHandler):
def __init__(self, observer, opts):
self.observer = observer
self.opts = opts
self.cfg = ConfigParser.ConfigParser()
self.cfg.read(opts.config)
self.watch = self.cfg.get('loopify', 'watch')
self.out = self.cfg.get('loopify', 'out')
self.count = int(self.cfg.get('loopify', 'count'))
self.gifsicle = self.cfg.get('loopify', 'gifsicle')
if not os.path.exists(self.gifsicle):
raise Exception, "Can't find gifsicle (%s)" % self.gifsicle
self.maxwidth = self.cfg.get('loopify', 'max-width')
self.queue = []
def on_any_event(self, event):
if event.event_type == 'created':
self.queue.append(event.src_path)
# WTF linux?
elif event.event_type == 'moved':
self.queue.append(event.dest_path)
else:
return False
logging.debug("queue length: %s (trigger at %s)" % (len(self.queue), self.count))
if len(self.queue) >= self.count:
queue = self.queue[:self.count]
self.queue = self.queue[self.count:]
logging.debug("loopify now")
pool.apply_async(loopify, (self.gifsicle, queue, self.out, self.maxwidth))
if __name__ == '__main__':
import sys
import optparse
parser = optparse.OptionParser()
parser.add_option('-c', '--config', dest='config', action='store', help='path to a loopr config file - see source code for a sample config')
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="enable chatty logging; default is false", default=False)
# MAYBE: allow CLI flags to override the config file?
# parser.add_option("-w", "--watch", dest="watch", help="", default=None)
# parser.add_option("-o", "--out", dest="out", help="", default=None)
# parser.add_option("-c", "--count", dest="count", help="", default=200)
# parser.add_option("--gifsicle", dest="gifsicle", help="", default="/usr/local/bin/gifsicle"),
# parser.add_option("--max-width", dest="maxwidth", help="", default=None)
(opts, args) = parser.parse_args()
if opts.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
observer = Observer()
event_handler = Eyeballs(observer, opts)
observer.schedule(event_handler, path=event_handler.watch, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()