|
| 1 | +#!/usr/bin/env python2 |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from fpdf import FPDF |
| 5 | +import cv2 |
| 6 | +import os |
| 7 | +import numpy as np |
| 8 | +import click |
| 9 | + |
| 10 | + |
| 11 | +ARUCO_DICTIONARIES = ( |
| 12 | + 'DICT_4X4_50', |
| 13 | + 'DICT_4X4_100', |
| 14 | + 'DICT_4X4_250', |
| 15 | + 'DICT_4X4_1000', |
| 16 | + |
| 17 | + 'DICT_5X5_50', |
| 18 | + 'DICT_5X5_100', |
| 19 | + 'DICT_5X5_250', |
| 20 | + 'DICT_5X5_1000', |
| 21 | + |
| 22 | + 'DICT_6X6_50', |
| 23 | + 'DICT_6X6_100', |
| 24 | + 'DICT_6X6_250', |
| 25 | + 'DICT_6X6_1000', |
| 26 | + |
| 27 | + 'DICT_ARUCO_ORIGINAL', |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +@click.command() |
| 32 | +@click.option('--dictionary', prompt=True, type=click.Choice(ARUCO_DICTIONARIES), help='ArUco dictionary to be used to create the board') |
| 33 | +@click.option('--marker_id', prompt='Marker ID', type=click.INT, help='The marker ID') |
| 34 | +@click.option('--marker_size', prompt='Marker size (mm)', type=click.FLOAT, help='The size of the marker') |
| 35 | +@click.option('--output_path', prompt=True, type=click.Path(writable=True), help='The path in which the PDF with the marker should be saved') |
| 36 | +def generate_marker(dictionary, marker_id, marker_size, output_path): |
| 37 | + marker_size_m = marker_size / 1000 |
| 38 | + |
| 39 | + complete_path = os.path.abspath(os.path.expanduser(output_path)) |
| 40 | + |
| 41 | + A4_SIZE_m = (0.297, 0.21) |
| 42 | + PAGE_RESOLUTION = (3508, 2480) |
| 43 | + PAGE_PIXELS_PER_METER = np.array(PAGE_RESOLUTION)/np.array(A4_SIZE_m) |
| 44 | + marker_size_pixels = np.around(PAGE_PIXELS_PER_METER * marker_size_m).astype('int') |
| 45 | + |
| 46 | + if marker_size_m > A4_SIZE_m[0] or marker_size_m > A4_SIZE_m[1]: |
| 47 | + raise ValueError('given size exceeds A4') |
| 48 | + |
| 49 | + aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_6X6_250) |
| 50 | + imboard = cv2.aruco.drawMarker(aruco_dict, marker_id, marker_size_pixels[0]) |
| 51 | + |
| 52 | + f = tempfile.NamedTemporaryFile(suffix='.png', delete=False) |
| 53 | + |
| 54 | + cv2.imwrite(f.name, imboard) |
| 55 | + |
| 56 | + marker_size_x_mm = marker_size_m * 1000 |
| 57 | + marker_size_y_mm = marker_size_m * 1000 |
| 58 | + |
| 59 | + pdf = FPDF('P', 'mm', 'A4') |
| 60 | + |
| 61 | + pdf.add_page('P') |
| 62 | + |
| 63 | + pdf.image(f.name, x=(A4_SIZE_m[1]/2*1000)-marker_size_x_mm/2, y=(A4_SIZE_m[0]/2*1000)-marker_size_y_mm/2, w=marker_size_x_mm, h=marker_size_y_mm) |
| 64 | + |
| 65 | + print('Configuration:') |
| 66 | + print('Marker size in millimeters: '+str(marker_size_x_mm)) |
| 67 | + print('Marker ID: ' + str(marker_id)) |
| 68 | + print('ArUco Dictionary name: ' + dictionary) |
| 69 | + |
| 70 | + print('Saving the marker as an A4 sized PDF in '+os.path.abspath(os.path.expanduser(complete_path))) |
| 71 | + |
| 72 | + pdf.output(complete_path) |
| 73 | + |
| 74 | + print('REMEMBER TO TURN OFF THE AUTOMATIC RESCALING OF THE PRINTER!') |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + generate_marker() |
0 commit comments