def single_capture(self, stepfunc, timecode): self.open_if_closed() self.init_cont() while 1: try: cd, id = self.video.GetCaptureData() break except sv.error: pass sgi.nap(1) if stepfunc: # This might step the video d=stepfunc() # to the next frame if not self.use_24: data = cd.InterleaveFields(1) else: x, y = self.vout.getsize() if self.use_compress: if self.rgb24_size == 1: data = cd.YUVtoYUV422DC(0) elif self.rgb24_size == 2: data = cd.YUVtoYUV422DC_quarter(1) x = x/2 y = y/2 elif self.rgb24_size == 3: data = cd.YUVtoYUV422DC_sixteenth(1) x = x/4 y = y/4 else: data = cd.YUVtoRGB(1) if self.maxx*self.maxy*4 <> len(data): print 'maxx,maxy,exp,got=', self.maxx, print self.maxy,self.maxx*self.maxy*4, print len(data) fl.showmessage('Wrong sized data') return 0 if self.rgb24_size <> 1: data = imageop.scale(data, 4, \ self.maxx, self.maxy, x, y) if self.use_jpeg: import jpeg data = jpeg.compress(data, x, y, 4) if self.use_compress: data = self.compressor.Compress(1, data) cd.UnlockCaptureData() self.end_cont() if timecode == None: timecode = (self.nframes+1) * (1000/25) return self.write_frame(timecode, data)
format = 'jpeg' elif info[0] == 'grey': width, height = vin.getsize() pf = vin.packfactor width, height = width / pf, height / pf bytes = 1 format = 'jpeggrey' else: sys.stderr.write('Vmkjpeg: input not in rgb or grey format\n') return 1 info = (format,) + info[1:] vout.setinfo(info) vout.writeheader() n = 0 try: while 1: t, data, cdata = vin.getnextframe() n = n + 1 sys.stderr.write('Frame ' + `n` + '...') data = jpeg.compress(data, width, height, bytes) vout.writeframe(t, data, None) sys.stderr.write('\n') except EOFError: pass return 0 # Don't forget to call the main program main()
def grab_jpeg(w, h, pf): data, dummy = grab_rgb(w, h, pf) import jpeg data = jpeg.compress(data, w, h, 4) return data, None
import imageop
format = 'jpeg' elif info[0] == 'grey': width, height = vin.getsize() pf = vin.packfactor width, height = width / pf, height / pf bytes = 1 format = 'jpeggrey' else: sys.stderr.write('Vmkjpeg: input not in rgb or grey format\n') return 1 info = (format, ) + info[1:] vout.setinfo(info) vout.writeheader() n = 0 try: while 1: t, data, cdata = vin.getnextframe() n = n + 1 sys.stderr.write('Frame ' + ` n ` + '...') data = jpeg.compress(data, width, height, bytes) vout.writeframe(t, data, None) sys.stderr.write('\n') except EOFError: pass return 0 # Don't forget to call the main program main()
# Class to grab frames from a window.
#! /usr/bin/env python # Compress an rgb or grey video file to jpeg format # Usage: # # Vmkjpeg [infile [outfile]] # Options: # # infile : input file (default film.video) # outfile : output file (default out.video) import sys import jpeg sys.path.append('/ufs/guido/src/video') import VFile # Main program -- mostly command line parsing def main(): args = sys.argv[1:] if len(args) < 1: args.append('film.video') if len(args) < 2: args.append('out.video') if len(args) > 2: sys.stderr.write('usage: Vmkjpeg [infile [outfile]]\n') sys.exit(2) sts = process(args[0], args[1]) sys.exit(sts) # Copy one file to another def process(infilename, outfilename):
def rgb2jpeg(img, x, y): import jpeg return jpeg.compress(img, x, y, 4)
def grey2jpeggrey(img, x, y): import jpeg return jpeg.compress(img, x, y, 1)
#! /usr/bin/env python
def example(): specs = ({ 'fn': 'images/rgb/Baboon.raw', 'size': (512, 512), 'grey_level': False, 'quality': 50, 'subsampling_mode': 1 }, { 'fn': 'images/rgb/Lena.raw', 'size': (512, 512), 'grey_level': False, 'quality': 50, 'subsampling_mode': 1 }, { 'fn': 'images/rgb/Baboon.raw', 'size': (512, 512), 'grey_level': False, 'quality': 5, 'subsampling_mode': 1 }, { 'fn': 'images/rgb/Lena.raw', 'size': (512, 512), 'grey_level': False, 'quality': 5, 'subsampling_mode': 1 }, { 'fn': 'images/grey_level/Baboon.raw', 'size': (512, 512), 'grey_level': True, 'quality': 50, 'subsampling_mode': 1 }, { 'fn': 'images/grey_level/Lena.raw', 'size': (512, 512), 'grey_level': True, 'quality': 50, 'subsampling_mode': 1 }) for spec in specs: with open(spec['fn'], 'rb') as raw_file: original = np.fromfile(raw_file, dtype=np.uint8) raw_file.seek(0) compressed = compress(raw_file, size=spec['size'], grey_level=spec['grey_level'], quality=spec['quality'], subsampling_mode=spec['subsampling_mode']) with open('compressed.protojpg', 'wb') as compressed_file: compressed['data'].tofile(compressed_file) header = compressed['header'] # get compressed header (metadata) with open('compressed.protojpg', 'rb') as compressed_file: extracted = extract(compressed_file, header={ 'size': header['size'], 'grey_level': header['grey_level'], 'quality': header['quality'], 'subsampling_mode': header['subsampling_mode'], 'remaining_bits_length': header['remaining_bits_length'], 'data_slice_lengths': header['data_slice_lengths'] }) logging.getLogger(__name__).info('PSNR: %.4f' % psnr(original, extracted)) show_raw_images((original, extracted), (spec['size'], spec['size']), (spec['fn'], 'Compressed and Extracted'), grey_level=spec['grey_level'])
#! /usr/bin/env python # Compress an rgb or grey video file to jpeg format # Usage: # # Vmkjpeg [infile [outfile]] # Options: # # infile : input file (default film.video) # outfile : output file (default out.video) import sys import jpeg sys.path.append('/ufs/guido/src/video') import VFile # Main program -- mostly command line parsing def main(): args = sys.argv[1:] if len(args) < 1: args.append('film.video') if len(args) < 2: args.append('out.video') if len(args) > 2: sys.stderr.write('usage: Vmkjpeg [infile [outfile]]\n') sys.exit(2) sts = process(args[0], args[1]) sys.exit(sts)