Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
import tempfile
|
| 5 |
-
from moviepy.editor import VideoFileClip
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
# Function to convert image
|
| 9 |
def convert_image(image_path, output_format):
|
|
@@ -17,60 +15,19 @@ def convert_image(image_path, output_format):
|
|
| 17 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{save_format.lower()}")
|
| 18 |
img.save(temp_file.name, format=save_format)
|
| 19 |
|
| 20 |
-
return temp_file.name # Returning the file path
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return str(e)
|
| 23 |
-
|
| 24 |
-
# Function to convert MP4 to GIF
|
| 25 |
-
def convert_mp4_to_gif(mp4_path, quality):
|
| 26 |
-
try:
|
| 27 |
-
# Load the video clip
|
| 28 |
-
clip = VideoFileClip(mp4_path)
|
| 29 |
-
|
| 30 |
-
# Quality settings
|
| 31 |
-
if quality == "High Quality":
|
| 32 |
-
fps = clip.fps # Use original frame rate
|
| 33 |
-
resize_factor = 1.0 # Keep original resolution
|
| 34 |
-
else: # Preserve Default Quality
|
| 35 |
-
fps = min(clip.fps, 15) # Cap at 15fps to reduce file size
|
| 36 |
-
resize_factor = 0.5 # Reduce resolution by half for efficiency
|
| 37 |
-
|
| 38 |
-
# Resize the clip
|
| 39 |
-
clip = clip.resize(resize_factor)
|
| 40 |
-
|
| 41 |
-
# Save as GIF to a temporary file
|
| 42 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".gif")
|
| 43 |
-
clip.write_gif(temp_file.name, fps=fps)
|
| 44 |
-
|
| 45 |
-
# Close the clip to free resources
|
| 46 |
-
clip.close()
|
| 47 |
-
|
| 48 |
-
return temp_file.name
|
| 49 |
except Exception as e:
|
| 50 |
return str(e)
|
| 51 |
|
| 52 |
# Gradio UI
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("# Simple Image
|
| 55 |
-
|
| 56 |
-
# Image Converter Section
|
| 57 |
-
gr.Markdown("## Image Converter")
|
| 58 |
with gr.Row():
|
| 59 |
input_image = gr.File(label="Upload Image", type="filepath")
|
| 60 |
output_format = gr.Radio(["JPG", "PNG"], label="Convert To")
|
| 61 |
-
|
| 62 |
output_image = gr.File(label="Download Converted Image")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# MP4 to GIF Converter Section
|
| 67 |
-
gr.Markdown("## MP4 to GIF Converter")
|
| 68 |
-
with gr.Row():
|
| 69 |
-
input_video = gr.File(label="Upload MP4", type="filepath")
|
| 70 |
-
quality_setting = gr.Radio(["High Quality", "Preserve Default Quality"], label="Quality")
|
| 71 |
-
convert_video_button = gr.Button("Convert to GIF")
|
| 72 |
-
output_gif = gr.File(label="Download Converted GIF")
|
| 73 |
-
|
| 74 |
-
convert_video_button.click(convert_mp4_to_gif, inputs=[input_video, quality_setting], outputs=output_gif)
|
| 75 |
|
| 76 |
-
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import io
|
| 4 |
import tempfile
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Function to convert image
|
| 7 |
def convert_image(image_path, output_format):
|
|
|
|
| 15 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{save_format.lower()}")
|
| 16 |
img.save(temp_file.name, format=save_format)
|
| 17 |
|
| 18 |
+
return temp_file.name # Returning the file path instead of BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
return str(e)
|
| 21 |
|
| 22 |
# Gradio UI
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown("# Simple Image Converter")
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
input_image = gr.File(label="Upload Image", type="filepath")
|
| 27 |
output_format = gr.Radio(["JPG", "PNG"], label="Convert To")
|
| 28 |
+
convert_button = gr.Button("Convert")
|
| 29 |
output_image = gr.File(label="Download Converted Image")
|
| 30 |
|
| 31 |
+
convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
demo.launch()
|