Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line234565677 unmodified lines15161720212223242526272829303132333435363738394041424344454647481819202122235455565724252627612829306465666768697071727374313276331 unmodified linefrom PIL import Imageimport ioimport tempfilefrom moviepy.editor import VideoFileClipimport os
# Function to convert imagedef convert_image(image_path, output_format):7 unmodified lines temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f".{save_format.lower()}") img.save(temp_file.name, format=save_format) return temp_file.name # Returning the file path except Exception as e: return str(e)
# Function to convert MP4 to GIFdef convert_mp4_to_gif(mp4_path, quality): try: # Load the video clip clip = VideoFileClip(mp4_path) # Quality settings if quality == "High Quality": fps = clip.fps # Use original frame rate resize_factor = 1.0 # Keep original resolution else: # Preserve Default Quality fps = min(clip.fps, 15) # Cap at 15fps to reduce file size resize_factor = 0.5 # Reduce resolution by half for efficiency # Resize the clip clip = clip.resize(resize_factor) # Save as GIF to a temporary file temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".gif") clip.write_gif(temp_file.name, fps=fps) # Close the clip to free resources clip.close() return temp_file.name return temp_file.name # Returning the file path instead of BytesIO except Exception as e: return str(e)
# Gradio UIwith gr.Blocks() as demo: gr.Markdown("# Simple Image and Video Converter") # Image Converter Section gr.Markdown("## Image Converter") gr.Markdown("# Simple Image Converter") with gr.Row(): input_image = gr.File(label="Upload Image", type="filepath") output_format = gr.Radio(["JPG", "PNG"], label="Convert To") convert_image_button = gr.Button("Convert Image") convert_button = gr.Button("Convert") output_image = gr.File(label="Download Converted Image") convert_image_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image) # MP4 to GIF Converter Section gr.Markdown("## MP4 to GIF Converter") with gr.Row(): input_video = gr.File(label="Upload MP4", type="filepath") quality_setting = gr.Radio(["High Quality", "Preserve Default Quality"], label="Quality") convert_video_button = gr.Button("Convert to GIF") output_gif = gr.File(label="Download Converted GIF") convert_video_button.click(convert_mp4_to_gif, inputs=[input_video, quality_setting], outputs=output_gif) convert_button.click(convert_image, inputs=[input_image, output_format], outputs=output_image)
demo.launch()demo.launch()app.py+5-48