Download app.py from Sangwon78/Example2: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/spaces/Sangwon78/Example2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sangwon78/Example2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sangwon78/Example2/resolve/main/app.py
2.94 kB
| import gradio as gr | |
| from PIL import Image | |
| import cv2 | |
| import numpy as np | |
| # νν° μ μ© ν¨μ | |
| def apply_filter(image, filter_type, intensity): | |
| img = np.array(image) | |
| if filter_type == "Soft Glow": | |
| gaussian = cv2.GaussianBlur(img, (15, 15), 0) | |
| img = cv2.addWeighted(img, 1 - intensity, gaussian, intensity, 0) | |
| elif filter_type == "Portrait Enhancer": | |
| img = cv2.bilateralFilter(img, 9, 75, 75) | |
| elif filter_type == "Warm Tone": | |
| img[:, :, 0] = cv2.add(img[:, :, 0], int(50 * intensity)) # Red μ¦κ° | |
| elif filter_type == "Cold Tone": | |
| img[:, :, 2] = cv2.add(img[:, :, 2], int(50 * intensity)) # Blue μ¦κ° | |
| elif filter_type == "High-Key": | |
| img = cv2.convertScaleAbs(img, alpha=1 + intensity, beta=int(30 * intensity)) | |
| elif filter_type == "Low-Key": | |
| img = cv2.convertScaleAbs(img, alpha=1 - intensity, beta=int(-30 * intensity)) | |
| elif filter_type == "Haze": | |
| img = cv2.addWeighted(img, 1 - intensity, np.full(img.shape, 255, dtype=np.uint8), intensity, 0) | |
| elif filter_type == "Monochrome": | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) | |
| return Image.fromarray(img) | |
| # νμΌ μ μ₯ ν¨μ | |
| def save_file(image, format): | |
| # 'JPG'λ₯Ό 'JPEG'λ‘ λ³ν | |
| format = "JPEG" if format.upper() == "JPG" else format.upper() | |
| file_path = f"processed_image.{format.lower()}" | |
| image.save(file_path, format=format) | |
| return file_path | |
| # μ΄λ―Έμ§ λ³ν ν λ°ν | |
| def process_image(image, filter_type, intensity, file_format): | |
| filtered_image = apply_filter(image, filter_type, intensity) | |
| download_path = save_file(filtered_image, file_format) | |
| return filtered_image, download_path | |
| # Gradio UI κ΅¬μ± | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π· μ΄λ―Έμ§ νν° λ³νκΈ°") | |
| gr.Markdown("μ΄λ―Έμ§λ₯Ό μ λ‘λνκ³ μνλ νν°λ₯Ό ν΄λ¦ν΄ λ³νν΄ λ³΄μΈμ!") | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil", label="μ΄λ―Έμ§ μ ν") | |
| with gr.Column(): | |
| filter_selector = gr.Dropdown( | |
| ["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", | |
| "High-Key", "Low-Key", "Haze", "Monochrome"], | |
| label="νν° μ ν" | |
| ) | |
| intensity_slider = gr.Slider(0.0, 1.0, value=0.5, label="νν° κ°λ") | |
| file_format_selector = gr.Radio(["JPG", "PNG", "PDF"], label="νμΌ νμ") | |
| submit_button = gr.Button("λ³ν μ μ©") | |
| output_image = gr.Image(label="λ³νλ μ΄λ―Έμ§") | |
| download_button = gr.File(label="λ€μ΄λ‘λ λ§ν¬") | |
| # λ²νΌ ν΄λ¦ μ λμ μ°κ²° | |
| submit_button.click( | |
| process_image, | |
| inputs=[image_input, filter_selector, intensity_slider, file_format_selector], | |
| outputs=[output_image, download_button] | |
| ) | |
| # μ€ν | |
| if __name__ == "__main__": | |
| demo.launch() | |