Ejemplo n.º 1
0
def test_read_json_qr_robust_fail_multiple():
    doc = pdf.open_ensuring_pdf("test/fixtures/qr/multiple_qrs.pdf")
    with pytest.raises(ValueError):
        read_json = qr.read_json_qr_robust(doc[0],
                                           relative_rect=(0, 0, 1, 1),
                                           zoom=3)
        print(read_json)

    with pytest.raises(ValueError):
        read_json = qr.read_json_qr_robust(doc[0],
                                           relative_rect=(0, 0, 1, 1),
                                           zoom=[3, 4, 5])
        print(read_json)
Ejemplo n.º 2
0
def worker(input_):
    fn, dest = input_
    doc = pdf.open_ensuring_pdf(fn)

    for p in doc.pages():
        qr_data = qr.read_json_qr_robust(p, relative_rect=(0, .8, .4, 1))

        st_num = qr_data["st_num"]
        q = qr_data["doc_id"]
        index = qr_data["page_index"]

        out_fn = os.path.join(dest, f"{st_num}.{q}.{index:0>3}")
        out_d = pdf.doc_from_pages([p])
        out_d.save(out_fn)
Ejemplo n.º 3
0
def worker(input_):
    fn, dest = input_
    *_, only_fn = os.path.split(fn)

    in_doc = pdf.open_ensuring_pdf(fn)

    for i, p in enumerate(in_doc.pages()):

        bubble_array = bubbles.read_robust(p, (0.8, 0, 1, 0.5))
        page_total = np.sum(BUBBLEARRAY * bubble_array)
        qr_data = qr.read_json_qr_robust(p, relative_rect=(0, 0.8, 0.4, 1))
        array_image = pdf.crop_to_pillow_image(p,
                                               relative_rect=(0.85, 0, 1, 0.6),
                                               zoom=2)

        stream = io.BytesIO()
        array_image.save(stream, format="pdf")
        array_pdf = fitz.open(stream=stream.getvalue(), filetype="pdf")

        out_doc = fitz.open()
        newpage = out_doc.newPage(width=200, height=200)
        pdf.paste_pdf_on(newpage, array_pdf, relative_rect=(0.4, 0, 1, 1))
        pdf.place_text(
            newpage,
            f"{qr_data['st_num']}\n{qr_data['doc_id']}",
            relative_rect=(0.05, 0.1, 0.4, 0.5),
            fontsize=10,
        )
        pdf.place_text(
            newpage,
            pprint.pformat(dict(qr_data)),
            relative_rect=(0.05, 0.4, 0.5, 1),
            fontsize=5,
        )

        out_fn = os.path.join(
            dest,
            f"{page_total:07}.{qr_data['st_num']}.{qr_data['doc_id']}.{qr_data['page_index']:05}.pdf",
        )
        out_doc.save(out_fn)
Ejemplo n.º 4
0
def worker(input_):
    fn, zoom = input_
    st_num = extract_first_st_num(fn)

    recorder = collections.defaultdict(int)

    d = pdf.open_ensuring_pdf(fn)
    for p in d.pages():
        bubble_array = bubbles.read_robust(p, (0.8, 0, 1, 0.5))
        page_total = np.sum(bubble_array * BUBBLEARRAY)

        qr_data = qr.read_json_qr_robust(p, relative_rect=(0, 0.8, 0.4, 1))

        st_num = qr_data["st_num"]
        q = qr_data["doc_id"]
        index = qr_data["page_index"]

        recorder["st_num"] = st_num
        recorder[q] += page_total
        recorder["pagecount"] += 1

    return recorder
Ejemplo n.º 5
0
        ["B", "G"],
        ["C", "H"],
        ["D", "J"],
        ["E", "I"],
    ]
)

doc = pdf.open_ensuring_pdf("input.pdf")

rows = []
for page in doc.pages():
    # Read the bubble array on top right of page
    arr = bubbles.easy.read_robust(page, relative_rect=(0.8, 0, 1, 0.5))

    # Read page if mark qr code on bottom left of page
    page_id_data = qr.read_json_qr_robust(page, relative_rect=(0, 0.8, 0.4, 1))

    # Add up the total of numeric part of bubble array
    total = np.sum(BUBBLEARRAY_NUMERIC * arr)

    # Make a string from the letter part of the bubble array
    letters_array = arr[11:, :]
    letters = BUBBLEARRAY_LETTERS[letters_array]

    # make a row for csv output
    rows.append(
        [
            f"{page_id_data.get('doc_id')} page {page_id_data.get('page_index')}",
            total,
            "".join(letters),
        ]
Ejemplo n.º 6
0
def read_page_id_mark(fitz_page, rel_rect = (0, 0.8, 0.4, 1)):
    return qr.read_json_qr_robust(fitz_page, relative_rect=rel_rect)
            
Ejemplo n.º 7
0
def test_read_json_qr_robust_fail(tmp_path, data):
    doc = qr.qr_pdf(json.dumps(data))
    with pytest.raises(ValueError):
        read_json = qr.read_json_qr_robust(doc[0],
                                           relative_rect=(0, 0, .1, .1))
Ejemplo n.º 8
0
def test_read_json_qr_robust(tmp_path, data):
    doc = qr.qr_pdf(json.dumps(data))
    read_json = qr.read_json_qr_robust(doc[0])
    # assert isinstance(read_json, dict)
    assert read_json == data