Maud Doum
commited on
Commit
Β·
a645b1b
1
Parent(s):
0fcbfeb
improve canvas size tip
Browse files- README.md +10 -1
- copaint/gradio_ui.py +24 -11
README.md
CHANGED
@@ -7,7 +7,16 @@ TLDR: From generated Image -> Copaint PDF
|
|
7 |
## Usage and Install
|
8 |
```
|
9 |
# Install dependencies
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Run
|
13 |
python copaint.py --input_image data/bear.jpg --copaint_logo data/logo_copaint.jpg --outputfolder output
|
|
|
7 |
## Usage and Install
|
8 |
```
|
9 |
# Install dependencies
|
10 |
+
poetry install --with ui
|
11 |
+
|
12 |
+
# Run Gradio App on local server
|
13 |
+
poetry run copaint-app
|
14 |
+
```
|
15 |
+
|
16 |
+
## Alternative Usage and Install (CLI)
|
17 |
+
```
|
18 |
+
# Install dependencies
|
19 |
+
pip install torch torchvision reportlab PyPDF2 Pillow argparse gradio_pdf
|
20 |
|
21 |
# Run
|
22 |
python copaint.py --input_image data/bear.jpg --copaint_logo data/logo_copaint.jpg --outputfolder output
|
copaint/gradio_ui.py
CHANGED
@@ -36,7 +36,7 @@ def add_grid_to_image(image, h_cells, w_cells):
|
|
36 |
return image
|
37 |
|
38 |
|
39 |
-
def canvas_ratio(image):
|
40 |
w,h = image.size
|
41 |
aspect_ratio = w/h
|
42 |
if aspect_ratio > 1:
|
@@ -44,7 +44,7 @@ def canvas_ratio(image):
|
|
44 |
|
45 |
# find nearest aspect ratio in the list of predefined ones
|
46 |
predefined_aspect_ratios = [2/3, 1/2, 1/1, 5/6, 4/5, 5/7]
|
47 |
-
predefined_aspect_ratios_str = ["2
|
48 |
min_diff = float('inf')
|
49 |
closest_ratio_idx = None
|
50 |
for idx, ratio in enumerate(predefined_aspect_ratios):
|
@@ -57,13 +57,31 @@ def canvas_ratio(image):
|
|
57 |
if min_diff > 0.1:
|
58 |
return None
|
59 |
else:
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
|
63 |
def add_grid_and_display_ratio(image, h_cells, w_cells):
|
64 |
if image is None:
|
65 |
return None, gr.update(visible=False)
|
66 |
-
return add_grid_to_image(image, h_cells, w_cells), gr.update(visible=True, value=canvas_ratio(image))
|
67 |
|
68 |
|
69 |
def process_copaint(
|
@@ -214,17 +232,12 @@ def build_gradio_ui():
|
|
214 |
output_pdf = PDF(label="PDF Preview")
|
215 |
|
216 |
# Update output_image: trigger update when any input changes
|
217 |
-
input_image
|
|
|
218 |
fn=add_grid_and_display_ratio,
|
219 |
inputs=[input_image, h_cells, w_cells],
|
220 |
outputs=[output_image, canvas_msg]
|
221 |
)
|
222 |
-
for component in [h_cells, w_cells]:
|
223 |
-
component.change(
|
224 |
-
fn=add_grid_to_image,
|
225 |
-
inputs=[input_image, h_cells, w_cells],
|
226 |
-
outputs=output_image
|
227 |
-
)
|
228 |
|
229 |
# Submit function: generate pdf
|
230 |
def on_submit(input_image, h_cells, w_cells, use_a4, high_res, cell_size, copaint_name, copaint_logo):
|
|
|
36 |
return image
|
37 |
|
38 |
|
39 |
+
def canvas_ratio(image, h_cells, w_cells):
|
40 |
w,h = image.size
|
41 |
aspect_ratio = w/h
|
42 |
if aspect_ratio > 1:
|
|
|
44 |
|
45 |
# find nearest aspect ratio in the list of predefined ones
|
46 |
predefined_aspect_ratios = [2/3, 1/2, 1/1, 5/6, 4/5, 5/7]
|
47 |
+
predefined_aspect_ratios_str = ["2:3", "1:2", "1:1", "5:6", "4:5", "5:7"]
|
48 |
min_diff = float('inf')
|
49 |
closest_ratio_idx = None
|
50 |
for idx, ratio in enumerate(predefined_aspect_ratios):
|
|
|
57 |
if min_diff > 0.1:
|
58 |
return None
|
59 |
else:
|
60 |
+
|
61 |
+
example_str = ""
|
62 |
+
|
63 |
+
if closest_ratio_str == "1:1":
|
64 |
+
if h_cells == 2 and h_cells == 2:
|
65 |
+
example_str = " (for example, a 6β by 6β canvas)"
|
66 |
+
if h_cells == 3 and h_cells == 3:
|
67 |
+
example_str = " (for example, an 8β by 8β canvas)"
|
68 |
+
elif closest_ratio_str == "5:6":
|
69 |
+
example_str = " (for example, a 10β by 12β canvas)"
|
70 |
+
elif closest_ratio_str == "3:4":
|
71 |
+
if (h_cells == 3 and w_cells == 4) or (h_cells == 4 and w_cells == 3):
|
72 |
+
example_str = " (for example, a 6β by 8β canvas)"
|
73 |
+
if (h_cells == 4 and w_cells == 6) or (h_cells == 6 and w_cells == 4):
|
74 |
+
example_str = " (for example, an 18β by 24β, or a 12β by 16β canvas)"
|
75 |
+
elif closest_ratio_str == "2:3" and ((h_cells == 6 and w_cells == 9) or (h_cells == 9 and w_cells == 6)):
|
76 |
+
example_str = " (for example, a 24β by 36β canvas)"
|
77 |
+
|
78 |
+
return f"Preparing your canvas: *choose a canvas with a {closest_ratio_str} ratio to respect your designβs size{example_str}.*"
|
79 |
|
80 |
|
81 |
def add_grid_and_display_ratio(image, h_cells, w_cells):
|
82 |
if image is None:
|
83 |
return None, gr.update(visible=False)
|
84 |
+
return add_grid_to_image(image, h_cells, w_cells), gr.update(visible=True, value=canvas_ratio(image, h_cells, w_cells))
|
85 |
|
86 |
|
87 |
def process_copaint(
|
|
|
232 |
output_pdf = PDF(label="PDF Preview")
|
233 |
|
234 |
# Update output_image: trigger update when any input changes
|
235 |
+
for component in [input_image, h_cells, w_cells]:
|
236 |
+
component.change(
|
237 |
fn=add_grid_and_display_ratio,
|
238 |
inputs=[input_image, h_cells, w_cells],
|
239 |
outputs=[output_image, canvas_msg]
|
240 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
# Submit function: generate pdf
|
243 |
def on_submit(input_image, h_cells, w_cells, use_a4, high_res, cell_size, copaint_name, copaint_logo):
|