Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,54 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from PIL import ImageOps
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
def create_cereal_box(input_image):
|
7 |
+
# Convert the input numpy array to PIL Image
|
8 |
+
cover_img = Image.fromarray((input_image.astype(np.uint8)))
|
9 |
+
|
10 |
+
# Load the template image
|
11 |
+
template_img = Image.open('CerealBoxMaker/template.jpeg')
|
12 |
+
|
13 |
+
# Define scaling factor for diagonal resizing
|
14 |
+
scaling_factor = 1.5
|
15 |
+
|
16 |
+
# Resize cover image
|
17 |
+
rect_height = int(template_img.height * 0.32)
|
18 |
+
new_width = int(rect_height * 0.70)
|
19 |
+
cover_resized = cover_img.resize((new_width, rect_height), Image.LANCZOS)
|
20 |
+
|
21 |
+
# Apply diagonal scaling
|
22 |
+
new_width_scaled = int(new_width * scaling_factor)
|
23 |
+
new_height_scaled = int(rect_height * scaling_factor)
|
24 |
+
cover_resized_scaled = cover_resized.resize((new_width_scaled, new_height_scaled), Image.LANCZOS)
|
25 |
+
|
26 |
+
# Positioning the resized cover image on the template
|
27 |
+
left_x = int(template_img.width * 0.085)
|
28 |
+
left_y = int((template_img.height - new_height_scaled) // 2 + template_img.height * 0.012)
|
29 |
+
left_position = (left_x, left_y)
|
30 |
+
|
31 |
+
right_x = int(template_img.width * 0.82) - new_width_scaled
|
32 |
+
right_y = left_y
|
33 |
+
right_position = (right_x, right_y)
|
34 |
+
|
35 |
+
# Create a copy of the template to paste on
|
36 |
+
template_copy = template_img.copy()
|
37 |
+
|
38 |
+
# Paste the resized and scaled cover image
|
39 |
+
template_copy.paste(cover_resized_scaled, left_position)
|
40 |
+
template_copy.paste(cover_resized_scaled, right_position)
|
41 |
+
|
42 |
+
# Convert the PIL Image back to a numpy array
|
43 |
+
template_copy_array = np.array(template_copy)
|
44 |
+
|
45 |
+
return template_copy_array
|
46 |
+
|
47 |
+
# Your existing Gr.Interface for the model that takes text and returns an image
|
48 |
+
iface = gr.Interface.load("models/ostris/super-cereal-sdxl-lora")
|
49 |
+
|
50 |
+
# Chain the existing interface with your new cereal box creation function
|
51 |
+
chained_iface = gr.Interface(create_cereal_box, inputs=iface.outputs, outputs="image")
|
52 |
+
|
53 |
+
# Launch the chained interface
|
54 |
+
chained_iface.launch()
|