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
15 def __init__(self
, seq_id
):
18 self
.append
= self
.frames
.append
21 def stretch(im
, size
, filter=Image
.NEAREST
):
23 im
= im
._new
(im
.im
.stretch(size
, filter))
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))
41 def process_dir(dirname
, blobname
=BLOB_NAME
):
42 fn_re
= re
.compile(r
'(\d{3})-(\d{5}).jpg')
44 jpegblob
= open(blobname
, 'w')
49 files
= sorted(x
for x
in os
.listdir(dirname
) if fn_re
.match(x
))
52 frame_counter
= count()
54 f
= open(os
.path
.join(dirname
, fn
))
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
67 new_seq_id
= m
.group(1)
68 if new_seq_id
!= seq_id
:
72 prev
.successors
[0] = frame
.index
75 glob_index
+= len(jpeg
)
78 save_frames(frames
, INDEX_FILE
+ '-prelink')
79 save_frames_text(frames
, TEXT_INDEX_FILE
+ '-prelink')
83 save_frames(frames
, INDEX_FILE
)
84 save_frames_text(frames
, TEXT_INDEX_FILE
)
89 process_dir(sys
.argv
[1])