コード例 #1
0
def test_warp_img():
    base = (300, 200)
    top = (100, 200)
    warped_path, warped_base, warped_top = warp_img(IMG_PATH, base, top)

    expected_warped_path = os.path.join(TEMP_FOLDER, 'platane-1_warped.jpeg')
    assert_equals(warped_path, expected_warped_path)
    assert_equals(warped_base, (450, 250))
    assert_equals(warped_top, (25, 250))
    assert_true(os.path.exists(warped_path))

    # The resulting image has a fixed size
    assert_equals(get_img_size(warped_path), (500, 500))
コード例 #2
0
def snapshot(request):
    """Index page."""
    filename = request.matchdict['file']
    settings = dict(request.registry.settings)
    pic_dir = settings['thumbs.document_root']
    file_path = os.path.join(pic_dir, filename)

    try:
        height, width = get_img_size(file_path)
    except IOError:
        logger.error("Failed to open file with path: %s", file_path,
                     exc_info=True)
        raise HTTPNotFound("Could not load image for snap %s" % filename)

    # security loop
    elmts = filename.split(os.sep)
    for unsecure in ('.', '..'):
        if unsecure in elmts:
            return HTTPNotFound()

    file_uuid = os.path.splitext(filename)[0]

    if request.method == 'POST':
        base = _toint(request.POST['bottom_y']), _toint(request.POST['bottom_x'])
        top = _toint(request.POST['top_y']), _toint(request.POST['top_x'])
        warped_img_path, new_base, new_top = warp_img(file_path, base, top)
        # There should be only one
        snap_idx, snap_type = 'snaps', 'snaptype'
        snap = request.db.get(snap_idx, snap_type, file_uuid)
        warped_filename = os.path.basename(warped_img_path)
        if snap is not None:
            snap['warped_filename'] = warped_filename
            snap['warped'] = True
            snap['base_x'] = base[0]
            snap['base_y'] = base[1]
            snap['top_x'] = top[0]
            snap['top_y'] = top[1]
            logger.debug("Warping snap %s", file_uuid)
            request.db.index(snap, snap_idx, snap_type, file_uuid)
            request.db.refresh()
            # Precompute the cached features incrementally
            compute_features_collection([snap], pic_dir, cache=FEATURES_CACHE)
        else:
            logger.warning("Could not find snap for %s", file_uuid)
        return HTTPFound(location='/warped/%s' % warped_filename)

    data = {'snapshot': filename,
            'width': width,
            'height': height}

    return _basic(request, data)
コード例 #3
0
def test_compute_features():
    base = (300, 200)
    top = (100, 200)
    warped_path, warped_base, warped_top = warp_img(IMG_PATH, base, top)
    features = compute_features(warped_path)
    assert_equals(features.shape, (7688,))