Exemplo n.º 1
0
def read_image(data, args):
    try:
        from cross2sheet.image import ImageGrid
    except ImportError as e:
        if e.name in ('cv2', 'numpy'):
            raise NotRecognized(
                'Image detection disabled because the module %s was not found.'
                % e.name)
        else:
            raise e
    try:
        img = ImageGrid(data)
    except ValueError:
        raise NotRecognized
    grid = img.grid()
    if args.detect_background:
        grid.features.extend(img.read_background(args.color_levels))
    if args.detect_bars:
        grid.features.extend(img.read_bars())
    if args.autonumber_cells_with_text:
        grid.features.extend(img.autonumber_if_text_found())
    if args.ocr_text:
        grid.features.extend(img.read_text_ocr())
    if args.autonumber is None:
        args.autonumber = not (args.autonumber_cells_with_text
                               or args.ocr_text)
    return grid
Exemplo n.º 2
0
 def setUp(self):
     url = self.url
     if url.startswith('20'):
         url = 'http://web.mit.edu/puzzle/www/' + url
     req = urlopen(url)
     data = req.read()
     req.close()
     self.img = ImageGrid(data)
     self.maxDiff = None
Exemplo n.º 3
0
def the_wicked_switch():
    req=urlopen('http://web.mit.edu/puzzle/www/2012/puzzles/a_circus_line/the_wicked_switch/1.png')
    data=req.read()
    req.close()
    img=ImageGrid(data)
    grid=img.grid()
    grid.features.extend(img.read_background())
    grid.features.extend(img.read_text_ocr())
    grid.features.extend(outside_bars(grid))
    save_xlsx(grid,'the_wicked_switch.xlsx')
Exemplo n.º 4
0
def a_puzzle_with_answer_nowhere_man():
    req=urlopen('http://web.mit.edu/puzzle/www/2014/puzzle/puzzle_with_answer_nowhere_man/grid.png')
    data=req.read()
    req.close()
    img=ImageGrid(data)
    grid=img.grid()
    grid.features.extend(img.read_background())
    grid.features.extend(img.read_bars())
    grid.features.extend(outside_bars(grid))
    grid.features.extend(autonumber(grid))
    save_xlsx(grid,'nowhere_man.xlsx')
Exemplo n.º 5
0
def display():
    try:
        if 'file' in request.files:
            data=request.files['file'].read()
        elif request.form['url'].startswith('http'):
            data=urlopen(request.form['url']).read()
        else:
            return redirect(url_for('select'))
        img=ImageGrid(data)
        dim=img.dimensions()
        if dim[0]<=0 or dim[1]<=0:
            return render_template('select.html',error_msg='Failed to recognize crossword grid.')
        d=TableData(img=img)
        t=Table(d)
        return render_template('convert.html',table=t,data=d.to_json())
    except ValueError:
        return render_template('select.html',error_msg='File format not recognized.')
    except URLError as e:
        return render_template('select.html',error_msg='Could not load url: {}.'.format(e.reason.strerror))