Spaces:
Running
Running
File size: 8,629 Bytes
bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e f95d001 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 131728e bf62248 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
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() |