Spaces:
Sleeping
Sleeping
Update app.py
Browse files123454 unmodified lines101112131415162 unmodified lines19202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import cv2import numpy as npimport osimport subprocessfrom pathlib import Path4 unmodified linesreturn cv2.xphoto.oilPainting(img, 7, 1) if hasattr(cv2, 'xphoto') else cv2.detailEnhance(img, sigma_s=10, sigma_r=0.15)elif style == "Cyberpunk":img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV).astype(np.float32)img_hsv[:,:,0] = (img_hsv[:,:,0] + 20) % 180 # Shift to purples/pinksimg_hsv[:,:,1] = np.clip(img_hsv[:,:,1] * 1.5, 0, 255)return cv2.cvtColor(img_hsv.astype(np.uint8), cv2.COLOR_HSV2RGB)elif style == "Vivid Anime":2 unmodified lineselif style == "Blueprint":gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)edges = cv2.Canny(gray, 50, 150)blue_bg = np.zeros_like(img)blue_bg[:] = [20, 50, 150]white_edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)return cv2.addWeighted(blue_bg, 0.7, white_edges, 0.3, 0)elif style == "Vintage Film":sepia = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])return cv2.transform(img, sepia)# Original filters here...return imgexcept: return imgdef process_batch(input_folder="input_images", output_folder="output_videos", duration=5, style="Oil Painting"):Path(output_folder).mkdir(parents=True, exist_ok=True)valid_exts = (".jpg", ".jpeg", ".png", ".webp")for filename in os.listdir(input_folder):if filename.lower().endswith(valid_exts):img_path = os.path.join(input_folder, filename)video_name = Path(filename).stem + ".mp4"output_path = os.path.join(output_folder, video_name)print(f"Processing: {filename} -> {video_name}")generate_speedpaint(img_path, output_path, duration, style)def generate_speedpaint(img_path, final_path, duration, style):img = cv2.imread(img_path)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)h, w = 1080, 1920 # Defaulting to 1080pimg = cv2.resize(img, (w, h), interpolation=cv2.INTER_LANCZOS4)target_img = apply_artistic_filter(img, style)fps = 30total_frames = duration * fpstemp_raw = "temp_render.mp4"video = cv2.VideoWriter(temp_raw, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))for i in range(total_frames):progress = i / total_frames# 3D Animation: Subtle Zoom-in effectzoom = 1 + (0.05 * progress)M = cv2.getRotationMatrix2D((w/2, h/2), 0, zoom)animated_target = cv2.warpAffine(target_img, M, (w, h))frame = np.ones((h, w, 3), dtype=np.uint8) * 255reveal_w = int(progress * w)# Horizontal Revealframe[:, 0:reveal_w] = animated_target[:, 0:reveal_w]video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))video.release()# High-quality encode with FFmpegsubprocess.call(['ffmpeg', '-y', '-i', temp_raw, '-vcodec', 'libx264', '-crf', '18', '-pix_fmt', 'yuv420p', final_path])os.remove(temp_raw)# RUN THE BATCHif __name__ == "__main__":# 1. Create a folder named 'input_images'# 2. Put all your photos inside# 3. Run this scriptprocess_batch(style="Cyberpunk")1234564 unmodified lines111213141516172 unmodified lines2021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import cv2import numpy as npimport gradio as grimport osimport subprocessfrom pathlib import Path4 unmodified linesreturn cv2.xphoto.oilPainting(img, 7, 1) if hasattr(cv2, 'xphoto') else cv2.detailEnhance(img, sigma_s=10, sigma_r=0.15)elif style == "Cyberpunk":img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV).astype(np.float32)img_hsv[:,:,0] = (img_hsv[:,:,0] + 20) % 180img_hsv[:,:,1] = np.clip(img_hsv[:,:,1] * 1.5, 0, 255)return cv2.cvtColor(img_hsv.astype(np.uint8), cv2.COLOR_HSV2RGB)elif style == "Vivid Anime":2 unmodified lineselif style == "Blueprint":gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)edges = cv2.Canny(gray, 50, 150)blue_bg = np.full(img.shape, (255, 0, 0), dtype=np.uint8)white_edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)return cv2.addWeighted(blue_bg, 0.7, white_edges, 0.3, 0)elif style == "Vintage Film":sepia = np.array([[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]])return cv2.transform(img, sepia)return imgexcept: return imgdef generate_single_video(img_path, style, duration):img = cv2.imread(img_path)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)h, w = 1080, 1920img = cv2.resize(img, (w, h), interpolation=cv2.INTER_LANCZOS4)target_img = apply_artistic_filter(img, style)fps = 30total_frames = int(duration * fps)# Matching name: image_name.jpg -> image_name.mp4video_name = Path(img_path).stem + ".mp4"temp_raw = f"temp_{video_name}"video = cv2.VideoWriter(temp_raw, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))for i in range(total_frames):progress = i / total_frameszoom = 1 + (0.05 * progress)M = cv2.getRotationMatrix2D((w/2, h/2), 0, zoom)animated_target = cv2.warpAffine(target_img, M, (w, h))frame = np.ones((h, w, 3), dtype=np.uint8) * 255reveal_w = int(progress * w)frame[:, 0:reveal_w] = animated_target[:, 0:reveal_w]video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))video.release()# Final Encodefinal_output = f"finished_{video_name}"subprocess.call(['ffmpeg', '-y', '-i', temp_raw, '-vcodec', 'libx264', '-crf', '18', '-pix_fmt', 'yuv420p', final_output])if os.path.exists(temp_raw): os.remove(temp_raw)return final_outputdef batch_ui_handler(files, style, duration):output_paths = []for file in files:output_paths.append(generate_single_video(file.name, style, duration))return output_paths# Gradio Interfacewith gr.Blocks() as demo:gr.Markdown("# 🎨 Batch 4K 3D SpeedPaint Generator")with gr.Row():with gr.Column():file_input = gr.File(file_count="multiple", label="Upload Multiple Images")style_choice = gr.Dropdown(["Oil Painting", "Cyberpunk", "Vivid Anime", "Blueprint", "Vintage Film"], value="Oil Painting", label="Choose Style")dur_slider = gr.Slider(1, 15, value=5, step=1, label="Duration (Seconds)")run_btn = gr.Button("Generate All Videos")with gr.Column():file_output = gr.File(label="Download Processed Videos")run_btn.click(fn=batch_ui_handler, inputs=[file_input, style_choice, dur_slider], outputs=file_output)demo.launch()
app.py+38-33