77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import json
|
|
import random
|
|
from copy import deepcopy
|
|
from itertools import chain
|
|
|
|
import fpdf
|
|
import pytest
|
|
from funcy import juxt, merge, rpartial
|
|
|
|
from image_prediction.formatter.formatters.enum import EnumFormatter
|
|
from image_prediction.image_extractor.extractor import ImageMetadataPair
|
|
from image_prediction.info import Info
|
|
from test.conftest import get_base_position_metadata, add_image, random_single_color_image_from_metadata
|
|
|
|
|
|
def test_image_stitcher(partial_image_metadata_pairs):
|
|
pass
|
|
|
|
|
|
@pytest.mark.parametrize("width", [100])
|
|
@pytest.mark.parametrize("height", [300])
|
|
@pytest.mark.parametrize("page_width", [500])
|
|
@pytest.mark.parametrize("page_height", [500])
|
|
def test_partial_image_metadata_pairs(patches_metadata, page_width, page_height):
|
|
|
|
pdf = fpdf.FPDF(unit="pt", format=(page_width, page_height))
|
|
|
|
for patch in patches_metadata:
|
|
image = random_single_color_image_from_metadata(patch)
|
|
add_image(pdf, ImageMetadataPair(image, patch))
|
|
|
|
pdf.output("/tmp/bla.pdf")
|
|
|
|
|
|
def split_box(box, max_step=5):
|
|
def split_recursively(box, step):
|
|
def split_horizontal():
|
|
return split(Info.WIDTH, Info.X1, Info.X2)
|
|
|
|
def split_vertical():
|
|
return split(Info.HEIGHT, Info.Y1, Info.Y2)
|
|
|
|
def split(dim, coord1, coord2):
|
|
|
|
if box[dim] >= 10:
|
|
split_len = random.randint(5, box[dim] - 5)
|
|
split_point = box[coord1] + split_len
|
|
|
|
box_left, box_right = juxt(deepcopy, deepcopy)(box)
|
|
|
|
box_left[dim] = split_len
|
|
box_right[dim] = box[dim] - split_len
|
|
|
|
box_left[coord2] = split_point
|
|
box_right[coord1] = split_point
|
|
|
|
return box_left, box_right
|
|
else:
|
|
return [box]
|
|
|
|
if step < max_step:
|
|
new_boxes = random.choice([split_horizontal, split_vertical])()
|
|
|
|
return chain.from_iterable(map(rpartial(split_recursively, step + 1), new_boxes))
|
|
else:
|
|
return [box]
|
|
|
|
return split_recursively(box, 0)
|
|
|
|
|
|
@pytest.fixture()
|
|
def patches_metadata(width, height, page_width, page_height):
|
|
box = get_base_position_metadata(width, height, page_width, page_height)
|
|
box = merge(box, {Info.X1: 0, Info.Y1: 0, Info.X2: width, Info.Y2: height})
|
|
boxes = split_box(box)
|
|
return boxes
|