Example #1
0
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()
Example #2
0
            continue
        
        appended_files.append(pdf_url)

        _update_step(STEP_TILING_UPLOADING)

        draw_postblobs(postblob_img, blobs_abcde)
        _append_image('postblob.jpg', postblob_img)
        postblob_img.save('postblob.jpg')
        
        print >> stderr, '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)
        
        print >> stderr, 'done.'
        print >> stderr, 'tiles...',
        
        yield 5*60
        
        minrow, mincol, minzoom = 2**20, 2**20, 20
        maxrow, maxcol, maxzoom = 0, 0, 0

        for (coord, tile_img) in generate_tiles(input, s2p, *geo_args):

            _append_image('%(zoom)d/%(column)d/%(row)d.jpg' % coord.__dict__, tile_img)
Example #3
0
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()