Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ with open("emojis.json") as f:
|
|
| 16 |
# Directory where emoji PNGs are stored
|
| 17 |
emoji_dir = "emoji_pngs"
|
| 18 |
|
| 19 |
-
def preprocess_image(image, emoji_name=None):
|
| 20 |
# Ensure the image is square by padding it
|
| 21 |
size = max(image.size)
|
| 22 |
new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
|
|
@@ -26,21 +26,25 @@ def preprocess_image(image, emoji_name=None):
|
|
| 26 |
new_image_gif = new_image.resize((256, 256), Image.LANCZOS) # Resize for GIF
|
| 27 |
new_image_video = new_image.resize((720, 720), Image.LANCZOS) # Resize for Video
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
if
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return new_image_gif, new_image_video, emoji_name
|
| 42 |
|
| 43 |
-
def generate_media(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness):
|
| 44 |
frames = []
|
| 45 |
gif_duration = 100 # Duration for each frame in the GIF in milliseconds
|
| 46 |
total_frames_gif = 18 # Total number of frames for GIF
|
|
@@ -52,8 +56,8 @@ def generate_media(editor1_output, editor2_output, transition_type, fps, transit
|
|
| 52 |
img2 = editor2_output["composite"].convert('RGBA')
|
| 53 |
|
| 54 |
# Preprocess images to make them square and same size, select emojis once
|
| 55 |
-
img1_gif, img1_video, random_emoji1 = preprocess_image(img1)
|
| 56 |
-
img2_gif, img2_video, random_emoji2 = preprocess_image(img2)
|
| 57 |
|
| 58 |
# Set size for the GIF
|
| 59 |
gif_size = (256, 256)
|
|
@@ -233,6 +237,7 @@ with gr.Blocks() as iface:
|
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
transition_type = gr.Radio(["slide", "rotate"], label="Transition Type", value="slide")
|
|
|
|
| 236 |
generate_button = gr.Button("Generate GIF & MP4")
|
| 237 |
|
| 238 |
with gr.Accordion("Advanced Settings", open=False):
|
|
@@ -247,9 +252,9 @@ with gr.Blocks() as iface:
|
|
| 247 |
gif_download = gr.File(label="Download GIF", elem_id="gif_download", visible=True)
|
| 248 |
mp4_download = gr.File(label="Download MP4", elem_id="mp4_download", visible=True)
|
| 249 |
|
| 250 |
-
def handle_generation(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness):
|
| 251 |
try:
|
| 252 |
-
gif_path, mp4_path = generate_media(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness)
|
| 253 |
return gif_path, gif_path, mp4_path
|
| 254 |
except Exception as e:
|
| 255 |
print(f"Error in handle_generation: {e}")
|
|
@@ -257,9 +262,9 @@ with gr.Blocks() as iface:
|
|
| 257 |
|
| 258 |
generate_button.click(
|
| 259 |
handle_generation,
|
| 260 |
-
inputs=[image_editor1, image_editor2, transition_type, fps, transition_speed, color, thickness],
|
| 261 |
outputs=[gif_display, gif_download, mp4_download]
|
| 262 |
)
|
| 263 |
|
| 264 |
# Launch the interface
|
| 265 |
-
iface.launch(share=False)
|
|
|
|
| 16 |
# Directory where emoji PNGs are stored
|
| 17 |
emoji_dir = "emoji_pngs"
|
| 18 |
|
| 19 |
+
def preprocess_image(image, emoji_name=None, enable_emoji=True):
|
| 20 |
# Ensure the image is square by padding it
|
| 21 |
size = max(image.size)
|
| 22 |
new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
|
|
|
|
| 26 |
new_image_gif = new_image.resize((256, 256), Image.LANCZOS) # Resize for GIF
|
| 27 |
new_image_video = new_image.resize((720, 720), Image.LANCZOS) # Resize for Video
|
| 28 |
|
| 29 |
+
# Only add emoji if enabled
|
| 30 |
+
if enable_emoji:
|
| 31 |
+
# Use the provided emoji or select a random one
|
| 32 |
+
if not emoji_name:
|
| 33 |
+
emoji_name = random.choice(list(emojis.keys()))
|
| 34 |
+
emoji_path = os.path.join(emoji_dir, f"{emoji_name}.png")
|
| 35 |
+
if os.path.exists(emoji_path):
|
| 36 |
+
emoji_image = Image.open(emoji_path).convert("RGBA")
|
| 37 |
+
emoji_image_gif = emoji_image.resize((256, 256), Image.LANCZOS)
|
| 38 |
+
emoji_image_video = emoji_image.resize((720, 720), Image.LANCZOS)
|
| 39 |
+
|
| 40 |
+
new_image_gif.paste(emoji_image_gif, (0, 0), emoji_image_gif)
|
| 41 |
+
new_image_video.paste(emoji_image_video, (0, 0), emoji_image_video)
|
| 42 |
+
else:
|
| 43 |
+
emoji_name = "none" # Use a placeholder when emojis are disabled
|
| 44 |
|
| 45 |
return new_image_gif, new_image_video, emoji_name
|
| 46 |
|
| 47 |
+
def generate_media(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness, enable_emoji):
|
| 48 |
frames = []
|
| 49 |
gif_duration = 100 # Duration for each frame in the GIF in milliseconds
|
| 50 |
total_frames_gif = 18 # Total number of frames for GIF
|
|
|
|
| 56 |
img2 = editor2_output["composite"].convert('RGBA')
|
| 57 |
|
| 58 |
# Preprocess images to make them square and same size, select emojis once
|
| 59 |
+
img1_gif, img1_video, random_emoji1 = preprocess_image(img1, enable_emoji=enable_emoji)
|
| 60 |
+
img2_gif, img2_video, random_emoji2 = preprocess_image(img2, enable_emoji=enable_emoji)
|
| 61 |
|
| 62 |
# Set size for the GIF
|
| 63 |
gif_size = (256, 256)
|
|
|
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
transition_type = gr.Radio(["slide", "rotate"], label="Transition Type", value="slide")
|
| 240 |
+
enable_emoji = gr.Checkbox(label="Enable Emojis", value=True, info="Toggle emoji overlays on/off")
|
| 241 |
generate_button = gr.Button("Generate GIF & MP4")
|
| 242 |
|
| 243 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 252 |
gif_download = gr.File(label="Download GIF", elem_id="gif_download", visible=True)
|
| 253 |
mp4_download = gr.File(label="Download MP4", elem_id="mp4_download", visible=True)
|
| 254 |
|
| 255 |
+
def handle_generation(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness, enable_emoji):
|
| 256 |
try:
|
| 257 |
+
gif_path, mp4_path = generate_media(editor1_output, editor2_output, transition_type, fps, transition_speed, color, thickness, enable_emoji)
|
| 258 |
return gif_path, gif_path, mp4_path
|
| 259 |
except Exception as e:
|
| 260 |
print(f"Error in handle_generation: {e}")
|
|
|
|
| 262 |
|
| 263 |
generate_button.click(
|
| 264 |
handle_generation,
|
| 265 |
+
inputs=[image_editor1, image_editor2, transition_type, fps, transition_speed, color, thickness, enable_emoji],
|
| 266 |
outputs=[gif_display, gif_download, mp4_download]
|
| 267 |
)
|
| 268 |
|
| 269 |
# Launch the interface
|
| 270 |
+
iface.launch(share=False)
|