Exemple #1
0
def test_nonempty_texts():
    body = 'Hello, I am the body text.'
    header = 'Hello, I am the header text.'
    sys.argv = [root, '-c', 'de', '-w', 'space', '-b', body, '-s', header]
    params = ParamsParser()
    assert params.head_text == header
    assert params.main_text == body
Exemple #2
0
def test_standard_fonts():
    sys.argv = [root, '-c', 'de', '-w', 'space']
    params = ParamsParser()

    assert os.path.isfile(params.head_font_path)
    assert os.path.isfile(params.main_font_path)

    ImageFont.truetype(params.head_font_path)
    ImageFont.truetype(params.main_font_path)
Exemple #3
0
def test_skip_on_nonexistent_town():
    sys.argv = [
        root, '-c', 'de', '-w', 'space', '-t',
        '"Wanzleben, Idonotexistatalltowm"'
    ]
    params = ParamsParser()

    correct_locations = [Coordinates('Wanzleben')]
    assert len(correct_locations) == len(params.locations)
    for parsed, correct in zip(params.locations, correct_locations):
        assert parsed == correct
Exemple #4
0
def test_town_names_parsing():
    sys.argv = [
        root, '-c', 'de', '-w', 'space', '-t',
        '"Wanzleben,  Bortfeld   ,  Berlin,Garmisch,Frankfurt am Main,Frankfurt Oder"'
    ]
    params = ParamsParser()

    correct_names = [
        'Wanzleben', 'Bortfeld', 'Berlin', 'Garmisch', 'Frankfurt am Main',
        'Frankfurt Oder'
    ]
    correct_locations = [Coordinates(name) for name in correct_names]
    for parsed, correct in zip(params.locations, correct_locations):
        assert parsed == correct
def main() -> None:
    params = ParamsParser()
    create_output_dir()

    # --- Map creation and pin setting ----------------------------------------
    img_transforms = [BackgroundDeletion(), Cutout(), Scale(110), AddShadow()]
    # TODO Cropping und Koordination des Kartenausschnitts in die config.json
    germany = Map('de-neg.shp', 'old-topo.png', [5.32, 15.55, 47.2, 56.2])

    for location in params.locations:
        logging.info('Creating pin: ' + location.name)
        specific_transforms = img_transforms + [Ribbon(
            location.name)] if params.ribbons else img_transforms
        try:
            pin = Pin(location, params.marker_symbol, specific_transforms)
        except (ConnectionRefusedError, LookupError) as e:
            logging.warn(
                f'Had to skip pin at position {str(location)} due to {str(e)}.'
            )
            continue

        germany.add_pin(pin)

    raw_img_name = f'raw-{round(time())}.png'
    germany.save(raw_img_name)

    # --- Map cropping --------------------------------------------------------
    # TODO Cropping und Koordination des Kartenausschnitts in die config.json
    cropping = (300, 650, 1760, 2525)  # (left, top, right, bottom)
    img = Image.open(os.path.join('output', raw_img_name))
    img = crop_map(img, cropping)

    # --- Embedding into larger image and setting of heading ------------------
    _, height_map = img.size
    img = add_text_space(img, params.height_text_space)

    font_heading = get_sized_font(params.head_font_path, params.heading,
                                  img.width)
    end_y_heading = write_header(img, params.heading, font_heading, height_map,
                                 params.added_frame_px)

    # --- Embeds main text ----------------------------------------------------
    font_height_heading = font_heading.getsize(params.heading)[1]

    main_text_font = ImageFont.truetype(params.main_font_path, 70)
    # start_y_undertitles = calc_start_y_undertitles(img, params.body, main_text_font, end_y_heading, params.undertitle_line_spacing, params.text_coats)
    if params.text_coats:
        town_names = [location.name.lower() for location in params.locations]
        write_main_text_with_heraldry(img, params.body, main_text_font,
                                      params.undertitle_line_spacing,
                                      town_names, end_y_heading)
    else:
        write_main_text(img, params.body, main_text_font, end_y_heading,
                        params.undertitle_line_spacing)

    # --- Edits of the complete image -----------------------------------------
    complete_img_transforms = get_complete_img_transforms(params)
    for transform in complete_img_transforms:
        img = transform(img)

    img.save(os.path.join(os.getcwd(), 'output', 'written.png'))
Exemple #6
0
def test_fail_on_nonexistent_fonts():
    sys.argv = [root, '-c', 'de', '-w', 'space', '-f', 'testeins', 'testzwei']
    with pytest.raises(ValueError):
        ParamsParser()
Exemple #7
0
def test_error_on_nonexistent_wallpaper():
    sys.argv = [root, '-c', 'de', '-w', 'I should not be here']
    with pytest.raises(ValueError):
        params = ParamsParser()
Exemple #8
0
def test_error_on_nonexistent_country():
    sys.argv = [root, '-c', 'I should not be here', '-w', 'space']
    with pytest.raises(ValueError):
        ParamsParser()
Exemple #9
0
def test_no_towns_allowed():
    sys.argv = [root, '-c', 'de', '-w', 'space']
    params = ParamsParser()

    assert type(params.locations) is list and len(params.locations) == 0
Exemple #10
0
def test_on_missing_required_param():
    sys.argv = [root]
    with pytest.raises(SystemExit):
        params = ParamsParser()
Exemple #11
0
def test_different_marker():
    sys.argv = [root, '-c', 'de', '-w', 'space', '-m', 'white']
    params = ParamsParser()
    assert params.marker_symbol == 'white-shade.png'
Exemple #12
0
def test_standard_marker():
    sys.argv = [root, '-c', 'de', '-w', 'space']
    params = ParamsParser()
    assert params.marker_symbol == 'heraldry'
Exemple #13
0
def test_empty_standard_texts():
    sys.argv = [root, '-c', 'de', '-w', 'space']
    params = ParamsParser()
    assert params.head_text is None
    assert params.main_text is None
Exemple #14
0
def test_fail_on_wrong_font_input_format():
    sys.argv = [root, '-c', 'de', '-w', 'space', '-f', 'nureine']
    with pytest.raises(SystemExit):
        ParamsParser()