wait for play in play.c
[sparrow.git] / prepare_images.py
blobad89b1d0bf5cefe583ff57be393bf3df8382432f
1 #!/usr/bin/python
3 import os, sys, re
4 import subprocess
5 import array
6 import struct
7 import Image
8 from itertools import count
10 from sparrow import FRAME_STRUCTURE, INDEX_FILE, TEXT_INDEX_FILE, BLOB_NAME
11 from sparrow import save_frames, save_frames_text, link_frames
12 from sparrow import Frame
14 class Sequence:
15 def __init__(self, seq_id):
16 self.id = seq_id
17 self.frames = []
18 self.append = self.frames.append
21 def stretch(im, size, filter=Image.NEAREST):
22 im.load()
23 im = im._new(im.im.stretch(size, filter))
24 return im
26 def downscale_pil(jpeg_data):
27 p = subprocess.Popen(['djpeg', '-scale', '1/8', '-greyscale'],
28 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
30 out, err = p.communicate(jpeg_data)
31 # out will be scaled down by 8, ie from 800x600 to 100x75
32 # we want it down to 8x6, 4x3 or thereabouts
33 # precision doesn't matter
34 header, imstring = out.split('255\n', 1)
35 header = header.split()
36 size = [int(x) for x in header[1:3]]
37 im = Image.fromstring("L", size, imstring)
38 im = stretch(im, (8, 6))
39 return im.tostring()
41 def process_dir(dirname, blobname=BLOB_NAME):
42 fn_re = re.compile(r'(\d{3})-(\d{5}).jpg')
44 jpegblob = open(blobname, 'w')
46 frames = []
47 glob_index = 0
49 files = sorted(x for x in os.listdir(dirname) if fn_re.match(x))
51 seq_id = None
52 frame_counter = count()
53 for fn in files:
54 f = open(os.path.join(dirname, fn))
55 jpeg = f.read()
56 f.close()
57 jpegblob.write(jpeg)
59 frame = Frame()
60 frame.index = frame_counter.next()
61 frame.glob_index = glob_index
62 frame.jpeg_len = len(jpeg)
63 frame.summary = downscale_pil(jpeg)
64 frame.successors = [0] * 8
66 m = fn_re.match(fn)
67 new_seq_id = m.group(1)
68 if new_seq_id != seq_id:
69 seq_id = new_seq_id
70 elif frames:
71 prev = frames[-1]
72 prev.successors[0] = frame.index
74 frames.append(frame)
75 glob_index += len(jpeg)
78 save_frames(frames, INDEX_FILE + '-prelink')
79 save_frames_text(frames, TEXT_INDEX_FILE + '-prelink')
81 link_frames(frames)
83 save_frames(frames, INDEX_FILE)
84 save_frames_text(frames, TEXT_INDEX_FILE)
89 process_dir(sys.argv[1])