groueix's picture
fr + reco taille canvas fr
f95d001
import argparse
from copaint.copaint import image_to_pdf
# from copaint import image_to_copaint_pdf
from PIL import Image
import numpy as np
import random
actual_participants_over_participants = 0.7 # how many people actually show up compared to the number of participants
# Default list of identifiers
default_identifiers_en = [
"sunshine",
"bliss",
"smile",
"serenity",
"laughter",
"breeze",
"harmony",
"glee",
"cheer",
"delight",
"hope",
"sparkle",
"kindness",
"charm",
"grace",
"radiance",
"jubilee",
"flutter",
"playful",
"whimsy",
"gleam",
"glow",
"twinkle",
"love",
"joy",
"peace",
"cheeky",
"amity",
"blissful",
"grateful"
]
default_identifiers_fr = [
"soleil", # sunshine
"joie", # joy
"bise", # soft breeze / kiss
"charmant", # charming
"éclat", # sparkle / brilliance
"rêve", # dream
"douceur", # softness / sweetness
"espoir", # hope
"lueur", # glow
"gaieté", # cheerfulness
"plume", # feather
"bisou", # kiss
"amour", # love
"paix", # peace
"rosée", # dew
"bulle", # bubble
"câlin", # cuddle
"grâce", # grace
"mistral", # southern wind
"lumière", # light
"frisson", # shiver (pleasant)
"azur", # blue sky
"rire", # laughter
]
# Function to get a default identifier (random or specified)
def default_identifier(index=None, language="en"):
"""
Get a default identifier from the list.
Args:
index: Optional index to get a specific identifier.
If None, returns a random identifier.
Returns:
str: A default identifier
"""
if language == "en":
if index is not None and 0 <= index < len(default_identifiers_fr):
return default_identifiers_fr[index]
return random.choice(default_identifiers_fr)
elif language == "fr":
if index is not None and 0 <= index < len(default_identifiers_en):
return default_identifiers_en[index]
return random.choice(default_identifiers_en)
def get_grid_size(nparticipants, input_image):
""" Takes the number of participants and the input image and returns the grid size, with the objective of making each cell as square as possible."""
# get the dimensions of the input image, load with PIL
input_image = Image.open(input_image)
w, h = input_image.size
n_cell = nparticipants * actual_participants_over_participants
aspect_ratio = w/h
h_cells = np.sqrt(aspect_ratio * n_cell)
w_cells = aspect_ratio * h_cells
"""
We have the following equations:
(1) w_cells/h_cells = aspect_ratio (as close as possible up to w_cells and h_cells being integers)
(2) h_cells * w_cells = n_cell
Solving to
(1) w_cells = aspect_ratio * h_cells
# replace in (2)
(2) h_cells * aspect_ratio * h_cells = n_cell
Leads to (3) h_cells^2 * aspect_ratio = n_cell
Leads to h_cells = sqrt(n_cell / aspect_ratio)
"""
h_cells = np.round(np.sqrt(n_cell / aspect_ratio))
w_cells = np.round(aspect_ratio * h_cells)
# convert to integers
h_cells = int(h_cells)
w_cells = int(w_cells)
print(f"Using {h_cells} x {w_cells} = {h_cells*w_cells} grid size for a canvas of size {h}*{w} and {nparticipants} participants (actual {n_cell})")
return int(h_cells), int(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')
# done adding arguments
args = parser.parse_args()
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]]
presets = [
[2, 3], # 6 people
[3, 3], # 9 people
[3, 4], # 12 people
[4, 4], # 16 people
[4, 5], # 20 people
[4, 6], # 24 people
[5, 6], # 30 people
[6, 8], # 48 people
[7, 9], # 63 people
[7, 10], # 70 people
[8, 10], # 80 people
[8, 12], # 96 people
]
preset_number_of_guests = [presets[i][0]*presets[i][1] for i in range(len(presets))]
# generate all presets
if args.use_presets:
# disregard other parameters and use the presets
# assert other parameters are not set
assert(args.h_cells is None), "When using presets, the number of H cells can't be set"
assert(args.w_cells is None), "When using presets, the number of W cells can't be set"
assert(args.nparticipants is None), "When using presets, the number of participants can't be set"
for preset in presets:
image_to_pdf(args.input_image, args.copaint_logo, args.outputfolder, preset[0], preset[1],
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 a copaint pdf based on the number of participants
elif 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)
# # Depracated find the first preset that can accomodate the number of participants
# preset_number_of_guests_inflated_by_losers = actual_participants_over_participants*args.nparticipants
# for i, preset in enumerate(presets):
# if preset_number_of_guests_inflated_by_losers <= preset_number_of_guests[i]:
# print(f"Using preset {preset} for {args.nparticipants} participants")
# image_to_copaint_pdf(args.input_image, args.copaint_logo, args.outputfolder, preset[0], preset[1])
# break
# 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()