Spaces:
Sleeping
Sleeping
"""Command line interface for Copaint pdf generator. | |
""" | |
import argparse | |
import numpy as np | |
import logging | |
from copaint.copaint import image_to_pdf | |
# Configure logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S' | |
) | |
logger = logging.getLogger(__name__) | |
# Default identifiers for unique_identifier parameter | |
default_identifiers = [ | |
"Mauricette", "Gertrude", "Bernadette", "Henriette", "Georgette", "Antoinette", "Colette", "Suzette", "Yvette", "Paulette", | |
"Juliette", "Odette", "Marinette", "Lucette", "Pierrette", "Cosette", "Rosette", "Claudette", "Violette", "Josette" | |
] | |
def default_identifier(): | |
"""Return a random default identifier.""" | |
return np.random.choice(default_identifiers) | |
def get_grid_size(nparticipants, input_image): | |
"""Get grid size based on number of participants.""" | |
# Compute the grid size based on the number of participants | |
# We want to find h_cells and w_cells such that h_cells * w_cells >= nparticipants | |
# and h_cells/w_cells is close to the aspect ratio of the image | |
from PIL import Image | |
image = Image.open(input_image) | |
w, h = image.size | |
aspect_ratio = w/h | |
# Find the smallest grid that can accommodate nparticipants | |
min_cells = int(np.ceil(np.sqrt(nparticipants))) | |
for i in range(min_cells, min_cells+10): | |
for j in range(min_cells, min_cells+10): | |
if i*j >= nparticipants: | |
ratio = j/i | |
if abs(ratio - aspect_ratio) < 0.2: | |
logger.info(f"Found grid size {i}x{j} for {nparticipants} participants") | |
return i, j | |
# If no good ratio is found, just use the smallest grid that can accommodate nparticipants | |
h_cells = min_cells | |
w_cells = int(np.ceil(nparticipants/min_cells)) | |
logger.warning(f"No good aspect ratio found. Using {h_cells}x{w_cells} grid for {nparticipants} participants") | |
return h_cells, w_cells | |
def main(): | |
parser = argparse.ArgumentParser(description='CoPaint') | |
parser.add_argument('--input_image', type=str, default='./data/bear.png', help='input image') | |
parser.add_argument('--copaint_logo', type=str, default='./data/logo_copaint.png', help='copaint logo') | |
parser.add_argument('--outputfolder', type=str, default='output/', help='output image') | |
parser.add_argument('--nparticipants', type=int, help='number of participants', default=None) | |
parser.add_argument('--h_cells', type=int, help='number of cells in height', default=None) | |
parser.add_argument('--w_cells', type=int, help='number of cells in width', default=None) | |
parser.add_argument('--unique_identifier', type=str, help='unique identifier like Mauricette, in case users are doing multiple copaint at the same time, to avoid mixing the tiles', default=None) | |
parser.add_argument('--cell_size_in_cm', type=float, default=None, help='size of a cell in cm, for printing purposes') | |
parser.add_argument('--use_presets', action='store_true', help='use a couple of presets cuts based on the size of the number of people attending') | |
parser.add_argument('--a4', action='store_true', help='use A4 format for the pdf') | |
parser.add_argument('--high_res', action='store_true', help='save in high resolution mode (PNG format without resizing)') | |
parser.add_argument('--min_cell_size_in_cm', type=int, default=2, help='minimum size of cells in cm') | |
parser.add_argument('--debug', action='store_true', help='debug mode') | |
args = parser.parse_args() | |
if args.debug: | |
logger.setLevel(logging.DEBUG) | |
if args.unique_identifier is None: | |
# select one at random | |
idx = np.random.choice(len(default_identifiers), 1) | |
args.unique_identifier = default_identifiers[idx[0]] | |
logger.info(f"Using random identifier: {args.unique_identifier}") | |
# generate a copaint pdf based on the number of participants | |
if args.nparticipants: | |
# assert other parameters are not set | |
assert(args.h_cells is None), "When choosing via number of participants, the number of H cells can't be set" | |
assert(args.w_cells is None ), "When choosing via number of participants, the number of W cells can't be set" | |
# get the grid size based on the number of participants | |
h_cells, w_cells = get_grid_size(args.nparticipants, args.input_image) | |
image_to_pdf(args.input_image, args.copaint_logo, args.outputfolder, h_cells, w_cells, | |
unique_identifier=args.unique_identifier, cell_size_in_cm=args.cell_size_in_cm, | |
a4=args.a4, high_res=args.high_res, min_cell_size_in_cm=args.min_cell_size_in_cm, debug=args.debug) | |
# Generate the copaint pdf using the specified number of cells | |
else: | |
image_to_pdf(args.input_image, args.copaint_logo, args.outputfolder, args.h_cells, args.w_cells, | |
unique_identifier=args.unique_identifier, cell_size_in_cm=args.cell_size_in_cm, | |
a4=args.a4, high_res=args.high_res, min_cell_size_in_cm=args.min_cell_size_in_cm, debug=args.debug) | |
if __name__ == '__main__': | |
main() |