def extract_tile_for_coord(image, coord, scan2coord): """ Return an image for a given coordinate. """ coord_bbox = coord.column, coord.row, coord.column + 1, coord.row + 1 tile_img = extract_image(scan2coord, coord_bbox, image, (256, 256), 256/8) return tile_img
def extract_tile_for_coord(image, coord, scan2coord): """ Return an image for a given coordinate. """ coord_bbox = coord.column, coord.row, coord.column + 1, coord.row + 1 tile_img = extract_image(scan2coord, coord_bbox, image, (256, 256), 256 / 8) return tile_img
def generate_tiles_for_zoom(image, scan2coord, zoom): """ Yield a stream of coordinates and tile images for a given zoom level. """ ul = scan2coord(Point(0, 0)) ur = scan2coord(Point(image.size[0], 0)) lr = scan2coord(Point(*image.size)) ll = scan2coord(Point(0, image.size[1])) minrow = min(ul.y, ur.y, lr.y, ll.y) maxrow = max(ul.y, ur.y, lr.y, ll.y) mincol = min(ul.x, ur.x, lr.x, ll.x) maxcol = max(ul.x, ur.x, lr.x, ll.x) for row in range(int(minrow), int(maxrow) + 1): for col in range(int(mincol), int(maxcol) + 1): coord = Coordinate(row, col, zoom) coord_bbox = col, row, col + 1, row + 1 tile_img = extract_image(scan2coord, coord_bbox, image, (256, 256), 256/8) yield (coord, tile_img)
_append_file('preblobs.jpg', open(preblobs_filename, 'r').read()) postblob_img = Image.open(postblob_filename) move(highpass_filename, 'highpass.jpg') move(preblobs_filename, 'preblobs.jpg') unlink(postblob_filename) _update_step(STEP_FINDING_NEEDLES) for (s2p, paper, orientation, blobs_abcde) in paper_matches(blobs): yield 10 print >> stderr, paper, orientation, '--', s2p qrcode_img = extract_image(s2p, (-90-9, -90-9, 0+9, 0+9), input, (500, 500)) _append_image('qrcode.png', qrcode_img) qrcode_img.save('qrcode.png') yield 10 _update_step(STEP_READING_QR_CODE) try: print_id, north, west, south, east, _paper, _orientation, pdf_url = read_code(qrcode_img) except CodeReadException: print >> stderr, 'could not read the QR code.' continue if (_paper, _orientation) != (paper, orientation): continue
def main(apibase, password, scan_id, url): """ """ # # Prepare a shorthand for pushing data. # def _finish_scan(uploaded_file, print_id, print_page_number, print_url, min_coord, max_coord, img_bounds): if scan_id: finish_scan(apibase, password, scan_id, uploaded_file, print_id, print_page_number, print_url, min_coord, max_coord, img_bounds) def _update_scan(uploaded_file, progress): if scan_id: update_scan(apibase, password, scan_id, progress) def _fail_scan(): if scan_id: fail_scan(apibase, password, scan_id) def _append_file(name, body): """ Append generally a file. """ if scan_id: append_scan_file(scan_id, name, body, apibase, password) def _append_image(filename, image): """ Append specifically an image. """ buffer = StringIO() format = filename.lower().endswith('.jpg') and 'JPEG' or 'PNG' image.save(buffer, format) _append_file(filename, buffer.getvalue()) handle, highpass_filename = mkstemp(prefix='highpass-', suffix='.jpg') close(handle) handle, preblobs_filename = mkstemp(prefix='preblobs-', suffix='.jpg') close(handle) handle, postblob_filename = mkstemp(prefix='postblob-', suffix='.png') close(handle) try: print 'Downloading', url input = imageopen(url) blobs = imgblobs(input, highpass_filename, preblobs_filename, postblob_filename) s, h, path, p, q, f = urlparse(url) uploaded_file = basename(path) _update_scan(uploaded_file, 0.2) _append_file('highpass.jpg', open(highpass_filename, 'r').read()) _append_file('preblobs.jpg', open(preblobs_filename, 'r').read()) postblob_img = Image.open(postblob_filename) move(highpass_filename, 'highpass.jpg') move(preblobs_filename, 'preblobs.jpg') unlink(postblob_filename) _update_scan(uploaded_file, 0.3) for (s2p, paper, orientation, blobs_abcde) in paper_matches(blobs): print paper, orientation, '--', s2p qrcode_img = extract_image(s2p, (-90-9, -90-9, 0+9, 0+9), input, (500, 500)) _append_image('qrcode.png', qrcode_img) qrcode_img.save('qrcode.png') try: print_id, print_url, north, west, south, east, _paper, _orientation, _layout = read_code(qrcode_img) except CodeReadException: print 'could not read the QR code.' continue if (_paper, _orientation) != (paper, orientation): continue print_page_number = None if print_url.startswith(apibase): if '/' in print_id: print_id, print_page_number = print_id.split('/', 1) else: print_id = None draw_postblobs(postblob_img, blobs_abcde) _append_image('postblob.jpg', postblob_img) postblob_img.save('postblob.jpg') _update_scan(uploaded_file, 0.4) print 'geotiff...', paper_width_pt, paper_height_pt = get_paper_size(paper, orientation) geo_args = paper_width_pt, paper_height_pt, north, west, south, east geotiff_bytes, geojpeg_img, img_bounds = create_geotiff(input, s2p.inverse(), *geo_args) _append_file('walking-paper-%s.tif' % scan_id, geotiff_bytes) _append_image('walking-paper-%s.jpg' % scan_id, geojpeg_img) _update_scan(uploaded_file, 0.5) print 'done.' print 'tiles...', minrow, mincol, minzoom = 2**20, 2**20, 20 maxrow, maxcol, maxzoom = 0, 0, 0 tiles_needed = list_tiles_for_bounds(input, s2p, *geo_args) for (index, (coord, scan2coord)) in enumerate(tiles_needed): if index % 10 == 0: _update_scan(uploaded_file, 0.5 + 0.5 * float(index) / len(tiles_needed)) tile_img = extract_tile_for_coord(input, coord, scan2coord) _append_image('%(zoom)d/%(column)d/%(row)d.jpg' % coord.__dict__, tile_img) print coord.zoom, minrow = min(minrow, coord.row) mincol = min(mincol, coord.column) minzoom = min(minzoom, coord.zoom) maxrow = max(maxrow, coord.row) maxcol = max(maxcol, coord.column) maxzoom = max(minzoom, coord.zoom) print '...done.' preview_img = input.copy() preview_img.thumbnail((409, 280), Image.ANTIALIAS) _append_image('preview.jpg', preview_img) large_img = input.copy() large_img.thumbnail((900, 900), Image.ANTIALIAS) _append_image('large.jpg', large_img) min_coord = Coordinate(minrow, mincol, minzoom) max_coord = Coordinate(maxrow, maxcol, maxzoom) break except Exception, e: print 'Failed because:', e traceback.print_exc() _fail_scan()
def main(apibase, password, scan_id, url): """ """ # # Prepare a shorthand for pushing data. # def _finish_scan(uploaded_file, print_id, print_page_number, print_url, min_coord, max_coord, img_bounds): if scan_id: finish_scan(apibase, password, scan_id, uploaded_file, print_id, print_page_number, print_url, min_coord, max_coord, img_bounds) def _update_scan(uploaded_file, progress): if scan_id: update_scan(apibase, password, scan_id, progress) def _fail_scan(): if scan_id: fail_scan(apibase, password, scan_id) def _append_file(name, body): """ Append generally a file. """ if scan_id: append_scan_file(scan_id, name, body, apibase, password) def _append_image(filename, image): """ Append specifically an image. """ buffer = StringIO() format = filename.lower().endswith('.jpg') and 'JPEG' or 'PNG' image.save(buffer, format) _append_file(filename, buffer.getvalue()) handle, highpass_filename = mkstemp(prefix='highpass-', suffix='.jpg') close(handle) handle, preblobs_filename = mkstemp(prefix='preblobs-', suffix='.jpg') close(handle) handle, postblob_filename = mkstemp(prefix='postblob-', suffix='.png') close(handle) try: print 'Downloading', url input = imageopen(url) blobs = imgblobs(input, highpass_filename, preblobs_filename, postblob_filename) s, h, path, p, q, f = urlparse(url) uploaded_file = basename(path) _update_scan(uploaded_file, 0.2) _append_file('highpass.jpg', open(highpass_filename, 'r').read()) _append_file('preblobs.jpg', open(preblobs_filename, 'r').read()) postblob_img = Image.open(postblob_filename) move(highpass_filename, 'highpass.jpg') move(preblobs_filename, 'preblobs.jpg') unlink(postblob_filename) _update_scan(uploaded_file, 0.3) for (s2p, paper, orientation, blobs_abcde) in paper_matches(blobs): print paper, orientation, '--', s2p qrcode_img = extract_image(s2p, (-90 - 9, -90 - 9, 0 + 9, 0 + 9), input, (500, 500)) _append_image('qrcode.png', qrcode_img) qrcode_img.save('qrcode.png') try: print_id, print_url, north, west, south, east, _paper, _orientation, _layout = read_code( qrcode_img) except CodeReadException: print 'could not read the QR code.' continue if (_paper, _orientation) != (paper, orientation): continue print_page_number = None if print_url.startswith(apibase): if '/' in print_id: print_id, print_page_number = print_id.split('/', 1) else: print_id = None draw_postblobs(postblob_img, blobs_abcde) _append_image('postblob.jpg', postblob_img) postblob_img.save('postblob.jpg') _update_scan(uploaded_file, 0.4) print 'geotiff...', paper_width_pt, paper_height_pt = get_paper_size( paper, orientation) geo_args = paper_width_pt, paper_height_pt, north, west, south, east geotiff_bytes, geojpeg_img, img_bounds = create_geotiff( input, s2p.inverse(), *geo_args) _append_file('walking-paper-%s.tif' % scan_id, geotiff_bytes) _append_image('walking-paper-%s.jpg' % scan_id, geojpeg_img) _update_scan(uploaded_file, 0.5) print 'done.' print 'tiles...', minrow, mincol, minzoom = 2**20, 2**20, 20 maxrow, maxcol, maxzoom = 0, 0, 0 tiles_needed = list_tiles_for_bounds(input, s2p, *geo_args) for (index, (coord, scan2coord)) in enumerate(tiles_needed): if index % 10 == 0: _update_scan(uploaded_file, 0.5 + 0.5 * float(index) / len(tiles_needed)) tile_img = extract_tile_for_coord(input, coord, scan2coord) _append_image( '%(zoom)d/%(column)d/%(row)d.jpg' % coord.__dict__, tile_img) print coord.zoom, minrow = min(minrow, coord.row) mincol = min(mincol, coord.column) minzoom = min(minzoom, coord.zoom) maxrow = max(maxrow, coord.row) maxcol = max(maxcol, coord.column) maxzoom = max(minzoom, coord.zoom) print '...done.' preview_img = input.copy() preview_img.thumbnail((409, 280), Image.ANTIALIAS) _append_image('preview.jpg', preview_img) large_img = input.copy() large_img.thumbnail((900, 900), Image.ANTIALIAS) _append_image('large.jpg', large_img) min_coord = Coordinate(minrow, mincol, minzoom) max_coord = Coordinate(maxrow, maxcol, maxzoom) break except Exception, e: print 'Failed because:', e traceback.print_exc() _fail_scan()