def scanlines(self, width, height, offsets): """ Merge multi-page screenshots and yield scanlines. """ overlaps = [height - offset for offset in offsets] # print 'offsets: ', offsets # print 'overlaps:', overlaps total = 0 row_bytes = 3 * width for index in range(0, len(overlaps) + 1): top = 0 bottom = 0 if index == 0: top = self.top_skip else: top = overlap_top(overlaps[index - 1]) if index == len(offsets): bottom = self.bottom_skip else: bottom = overlap_bottom(overlaps[index]) bottom = height - bottom segment = bottom - top total += segment filename = self.page_filename(index + 1) print filename, top, bottom, segment, total infile = open(filename, 'rb') hashmatch.read_ppm_header(infile) infile.seek(top * row_bytes, 1) for dummy in range(top, bottom): scanline = array('B') scanline.fromfile(infile, row_bytes) yield scanline infile.close()
def scanlines(self, offsets): """ Merge multi-page screenshots and yield scanlines. """ overlaps = [self.height - offset for offset in offsets] # print "offsets: ", offsets # print "overlaps:", overlaps total = 0 row_bytes = 3 * self.width for index in range(0, len(overlaps) + 1): top = 0 bottom = 0 if index == 0: top = self.top_skip else: top = overlap_top(overlaps[index-1]) if index == len(offsets): bottom = self.bottom_skip else: bottom = overlap_bottom(overlaps[index]) bottom = self.height - bottom segment = bottom - top total += segment filename = self.page_filename(index+1) print filename, top, bottom, segment, total infile = open(filename, 'rb') hashmatch.read_ppm_header(infile) infile.seek(top*row_bytes, 1) for dummy in range(top, bottom): scanline = array('B') scanline.fromfile(infile, row_bytes) yield scanline infile.close()
def check_screenshot(self, filename): """ Check if the screenshot file looks ok. """ if not os.path.exists(filename): raise RuntimeError("screenshot file %s not found" % filename) if not os.path.getsize(filename): raise RuntimeError("screenshot file %s is empty" % filename) magic, width, height, maxval = hashmatch.read_ppm_header(open(filename, "rb")) if magic != "P6": raise RuntimeError("%s isn't a PPM file with 24 bpp" % filename) if width != self.width or height != self.height: raise RuntimeError( "%s has %dx%d pixels, not the requested size %dx%d" % (filename, width, height, self.width, self.height) ) if maxval != 255: raise RuntimeError("%s has maxval %d, but only maxval 255 is supported" % (maxval, filename))
def check_screenshot(self, filename): """ Check if the screenshot file looks ok. """ if not os.path.exists(filename): raise RuntimeError("screenshot file %s not found" % filename) if not os.path.getsize(filename): raise RuntimeError("screenshot file %s is empty" % filename) magic, width, height, maxval = hashmatch.read_ppm_header( open(filename, 'rb')) if magic != 'P6': raise RuntimeError("%s isn't a PPM file with 24 bpp" % filename) if width != self.width or height != self.height: raise RuntimeError( "%s has %dx%d pixels, not the requested size %dx%d" % (filename, width, height, self.width, self.height)) if maxval != 255: raise RuntimeError( "%s has maxval %d, but only maxval 255 is supported" % (maxval, filename))
def browsershot(self, pngfilename='browsershot.png'): """ Take a number of screenshots and merge them into one tall image. """ filename = self.page_filename(1) self.screenshot(filename) self.check_screenshot(filename) magic, width, height, maxval = hashmatch.read_ppm_header( open(filename, 'rb')) assert magic == 'P6', "%s isn't a PPM file with 24 bpp" % filename assert maxval == 255, "%s doesn't have 24 bits per pixel" % filename offsets = self.scroll_pages(height) total = height + sum(offsets) - self.top_skip - self.bottom_skip # print 'total:', total scanlines = self.scanlines(width, height, offsets) outfile = file(pngfilename, 'wb') writer = png.Writer(width, total) writer.write(outfile, scanlines) outfile.close()