File size: 9,368 Bytes
7b49e90 a941c51 9b1db12 a941c51 d4174bb 2d2877d efbd483 72565dd 705282e 7b49e90 a941c51 7b49e90 43b3cef 7b49e90 43b3cef 7b49e90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import gradio as gr
from src.utils.change_format import change_format
from src.utils.remove_background import remove_background_from_url
from src.utils.visualize_image import visualize_base64_image
from src.utils.generate_image import generate_image
from src.utils.apply_filter import apply_filter
from src.utils.add_text import add_text_to_image
from src.utils.resize_image import resize_image
from src.utils.watermark import add_watermark, remove_watermark
from src.utils.describe import describe_image
from src.utils.compress import compress_image
import base64
from PIL import Image
import io
import requests
def image_to_base64(image):
if image is None:
return None
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
def base64_to_image(base64_str):
if not base64_str:
return None
image_data = base64.b64decode(base64_str)
return Image.open(io.BytesIO(image_data))
def url_to_base64(url):
response = requests.get(url)
return base64.b64encode(response.content).decode()
def gradio_remove_background(image):
if image is None:
return None
base64_img = image_to_base64(image)
result = remove_background_from_url(f"data:image/png;base64,{base64_img}")
return base64_to_image(result)
def gradio_describe_image(image):
if image is None:
return "No image provided"
base64_img = image_to_base64(image)
return describe_image(base64_img)
def gradio_change_format(image, format_type):
if image is None:
return None
base64_img = image_to_base64(image)
result = change_format(base64_img, format_type)
return base64_to_image(result)
def gradio_generate_image(prompt, width=512, height=512):
result = generate_image(prompt, width, height)
return base64_to_image(result)
def gradio_apply_filter(image, filter_type):
if image is None:
return None
base64_img = image_to_base64(image)
result = apply_filter(base64_img, filter_type)
return base64_to_image(result)
def gradio_add_text(image, text, x=50, y=50, font_size=20, color="white"):
if image is None:
return None
base64_img = image_to_base64(image)
result = add_text_to_image(base64_img, text, x, y, font_size, color)
return base64_to_image(result)
def gradio_add_watermark(image, watermark_text, opacity=0.5):
if image is None:
return None
base64_img = image_to_base64(image)
result = add_watermark(base64_img, watermark_text, opacity)
return base64_to_image(result)
def gradio_remove_watermark(image):
if image is None:
return None
base64_img = image_to_base64(image)
result = remove_watermark(base64_img)
return base64_to_image(result)
def gradio_compress_image(image, quality=80):
if image is None:
return None
base64_img = image_to_base64(image)
result = compress_image(base64_img, quality)
return base64_to_image(result)
def create_gradio_interface():
with gr.Blocks(title="Image Processing Service", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🖼️ Image Processing Service")
gr.Markdown("Servicio completo de procesamiento de imágenes")
with gr.Tabs():
with gr.Tab("🎨 Generate Image"):
with gr.Row():
prompt_input = gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate")
with gr.Column():
width_input = gr.Slider(256, 1024, 512, label="Width")
height_input = gr.Slider(256, 1024, 512, label="Height")
generate_btn = gr.Button("Generate", variant="primary")
generated_output = gr.Image(label="Generated Image")
generate_btn.click(
gradio_generate_image,
[prompt_input, width_input, height_input],
generated_output
)
with gr.Tab("🔍 Describe Image"):
with gr.Row():
describe_input = gr.Image(label="Upload Image", type="pil")
description_output = gr.Textbox(label="Description", lines=4)
describe_input.change(gradio_describe_image, describe_input, description_output)
with gr.Tab("✂️ Remove Background"):
with gr.Row():
bg_input = gr.Image(label="Upload Image", type="pil")
bg_output = gr.Image(label="Background Removed")
bg_input.change(gradio_remove_background, bg_input, bg_output)
with gr.Tab("🎭 Apply Filters"):
with gr.Row():
filter_input = gr.Image(label="Upload Image", type="pil")
with gr.Column():
filter_type = gr.Dropdown(
["blur", "sharpen", "vintage", "black_white", "sepia"],
label="Filter Type",
value="blur"
)
filter_output = gr.Image(label="Filtered Image")
filter_input.change(gradio_apply_filter, [filter_input, filter_type], filter_output)
filter_type.change(gradio_apply_filter, [filter_input, filter_type], filter_output)
with gr.Tab("📝 Add Text"):
with gr.Row():
text_input = gr.Image(label="Upload Image", type="pil")
with gr.Column():
text_content = gr.Textbox(label="Text", placeholder="Enter text to add")
with gr.Row():
text_x = gr.Number(label="X Position", value=50)
text_y = gr.Number(label="Y Position", value=50)
with gr.Row():
font_size = gr.Slider(10, 100, 20, label="Font Size")
text_color = gr.ColorPicker(label="Color", value="#FFFFFF")
text_output = gr.Image(label="Image with Text")
inputs = [text_input, text_content, text_x, text_y, font_size, text_color]
for inp in inputs:
inp.change(gradio_add_text, inputs, text_output)
with gr.Tab("💧 Watermark"):
with gr.Tabs():
with gr.Tab("Add Watermark"):
with gr.Row():
watermark_input = gr.Image(label="Upload Image", type="pil")
with gr.Column():
watermark_text = gr.Textbox(label="Watermark Text")
watermark_opacity = gr.Slider(0.1, 1.0, 0.5, label="Opacity")
watermark_output = gr.Image(label="Watermarked Image")
inputs = [watermark_input, watermark_text, watermark_opacity]
for inp in inputs:
inp.change(gradio_add_watermark, inputs, watermark_output)
with gr.Tab("Remove Watermark"):
with gr.Row():
unwatermark_input = gr.Image(label="Upload Image", type="pil")
unwatermark_output = gr.Image(label="Watermark Removed")
unwatermark_input.change(gradio_remove_watermark, unwatermark_input, unwatermark_output)
with gr.Tab("🗜️ Compress"):
with gr.Row():
compress_input = gr.Image(label="Upload Image", type="pil")
with gr.Column():
quality_slider = gr.Slider(10, 100, 80, label="Quality %")
compress_output = gr.Image(label="Compressed Image")
compress_input.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
quality_slider.change(gradio_compress_image, [compress_input, quality_slider], compress_output)
with gr.Tab("🔄 Change Format"):
with gr.Row():
format_input = gr.Image(label="Upload Image", type="pil")
with gr.Column():
format_type = gr.Dropdown(
["PNG", "JPEG", "WEBP", "BMP"],
label="Output Format",
value="PNG"
)
format_output = gr.Image(label="Converted Image")
format_input.change(gradio_change_format, [format_input, format_type], format_output)
format_type.change(gradio_change_format, [format_input, format_type], format_output)
gr.Markdown("---")
gr.Markdown("💡 **Status**: Active | Procesamiento de imágenes en tiempo real")
return demo
if __name__ == "__main__":
demo = create_gradio_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True
) |