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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Custom theme for professional UI design
|
| 7 |
+
custom_theme = gr.themes.Soft(
|
| 8 |
+
primary_hue="blue",
|
| 9 |
+
secondary_hue="indigo",
|
| 10 |
+
neutral_hue="slate",
|
| 11 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 12 |
+
text_size="lg",
|
| 13 |
+
spacing_size="lg",
|
| 14 |
+
radius_size="md"
|
| 15 |
+
).set(
|
| 16 |
+
button_primary_background_fill="*primary_600",
|
| 17 |
+
button_primary_background_fill_hover="*primary_700",
|
| 18 |
+
block_title_text_weight="600",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def process_single_image(image, prompt):
|
| 22 |
+
"""
|
| 23 |
+
Process a single image with the given prompt
|
| 24 |
+
"""
|
| 25 |
+
# Convert image to numpy array
|
| 26 |
+
img_array = np.array(image)
|
| 27 |
+
|
| 28 |
+
# Here you would add your actual processing logic
|
| 29 |
+
# For demonstration, we'll just return the image with a watermark
|
| 30 |
+
|
| 31 |
+
# Create a simple watermark with the prompt text
|
| 32 |
+
result_img = Image.fromarray(img_array)
|
| 33 |
+
result_img = add_watermark(result_img, prompt)
|
| 34 |
+
|
| 35 |
+
return result_img
|
| 36 |
+
|
| 37 |
+
def add_watermark(image, text):
|
| 38 |
+
"""
|
| 39 |
+
Add watermark text to an image
|
| 40 |
+
"""
|
| 41 |
+
# Create a copy of the image
|
| 42 |
+
img_copy = image.copy()
|
| 43 |
+
|
| 44 |
+
# Create a drawing context
|
| 45 |
+
draw = ImageDraw.Draw(img_copy)
|
| 46 |
+
|
| 47 |
+
# Get image dimensions
|
| 48 |
+
width, height = img_copy.size
|
| 49 |
+
|
| 50 |
+
# Set font and text position
|
| 51 |
+
try:
|
| 52 |
+
font = ImageFont.truetype("arial.ttf", 30)
|
| 53 |
+
except:
|
| 54 |
+
font = ImageFont.load_default()
|
| 55 |
+
|
| 56 |
+
# Calculate text size and position
|
| 57 |
+
text_width, text_height = draw.textsize(text, font=font)
|
| 58 |
+
position = (width - text_width - 10, height - text_height - 10)
|
| 59 |
+
|
| 60 |
+
# Add semi-transparent background for text
|
| 61 |
+
draw.rectangle(
|
| 62 |
+
[position[0] - 5, position[1] - 5,
|
| 63 |
+
position[0] + text_width + 5, position[1] + text_height + 5],
|
| 64 |
+
fill=(255, 255, 255, 150)
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Draw the text
|
| 68 |
+
draw.text(position, text, fill=(0, 0, 0), font=font)
|
| 69 |
+
|
| 70 |
+
return img_copy
|
| 71 |
+
|
| 72 |
+
def process_batch(images, prompt):
|
| 73 |
+
"""
|
| 74 |
+
Process multiple images with the same prompt
|
| 75 |
+
"""
|
| 76 |
+
results = []
|
| 77 |
+
for image in images:
|
| 78 |
+
if image is not None:
|
| 79 |
+
result = process_single_image(image, prompt)
|
| 80 |
+
results.append(result)
|
| 81 |
+
else:
|
| 82 |
+
results.append(None)
|
| 83 |
+
return results
|
| 84 |
+
|
| 85 |
+
def process_batch_files(image_files, prompt):
|
| 86 |
+
"""
|
| 87 |
+
Process multiple image files with the same prompt
|
| 88 |
+
"""
|
| 89 |
+
results = []
|
| 90 |
+
for file_path in image_files:
|
| 91 |
+
try:
|
| 92 |
+
# Open the image file
|
| 93 |
+
with Image.open(file_path) as img:
|
| 94 |
+
result = process_single_image(img, prompt)
|
| 95 |
+
results.append(result)
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Error processing {file_path}: {e}")
|
| 98 |
+
results.append(None)
|
| 99 |
+
return results
|
| 100 |
+
|
| 101 |
+
# Create the Gradio interface
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("# 🖼️ Batch Image Processor")
|
| 104 |
+
gr.Markdown("Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
|
| 105 |
+
|
| 106 |
+
with gr.Tabs():
|
| 107 |
+
# Single Image Processing Tab
|
| 108 |
+
with gr.Tab("Single Image"):
|
| 109 |
+
gr.Markdown("### Process a single image with your prompt")
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
with gr.Column():
|
| 113 |
+
single_image_input = gr.Image(label="Upload Image", type="pil")
|
| 114 |
+
single_prompt = gr.Textbox(label="Prompt", placeholder="Enter your processing prompt...")
|
| 115 |
+
single_process_btn = gr.Button("Process Image", variant="primary")
|
| 116 |
+
|
| 117 |
+
with gr.Column():
|
| 118 |
+
single_output = gr.Image(label="Processed Image")
|
| 119 |
+
|
| 120 |
+
single_process_btn.click(
|
| 121 |
+
fn=process_single_image,
|
| 122 |
+
inputs=[single_image_input, single_prompt],
|
| 123 |
+
outputs=single_output,
|
| 124 |
+
api_visibility="public"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Batch Processing Tab
|
| 128 |
+
with gr.Tab("Batch Processing"):
|
| 129 |
+
gr.Markdown("### Process multiple images with the same prompt")
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
with gr.Column():
|
| 133 |
+
batch_images_input = gr.Gallery(
|
| 134 |
+
label="Upload Multiple Images",
|
| 135 |
+
type="pil",
|
| 136 |
+
height="auto"
|
| 137 |
+
)
|
| 138 |
+
batch_prompt = gr.Textbox(label="Prompt", placeholder="Enter your processing prompt...")
|
| 139 |
+
batch_process_btn = gr.Button("Process All Images", variant="primary")
|
| 140 |
+
|
| 141 |
+
with gr.Column():
|
| 142 |
+
batch_output = gr.Gallery(label="Processed Images")
|
| 143 |
+
|
| 144 |
+
batch_process_btn.click(
|
| 145 |
+
fn=process_batch,
|
| 146 |
+
inputs=[batch_images_input, batch_prompt],
|
| 147 |
+
outputs=batch_output,
|
| 148 |
+
api_visibility="public"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# File Batch Processing Tab
|
| 152 |
+
with gr.Tab("File Batch Processing"):
|
| 153 |
+
gr.Markdown("### Process multiple image files with the same prompt")
|
| 154 |
+
|
| 155 |
+
with gr.Row():
|
| 156 |
+
with gr.Column():
|
| 157 |
+
file_batch_input = gr.File(
|
| 158 |
+
label="Upload Image Files",
|
| 159 |
+
file_count="multiple",
|
| 160 |
+
file_types=["image"]
|
| 161 |
+
)
|
| 162 |
+
file_batch_prompt = gr.Textbox(label="Prompt", placeholder="Enter your processing prompt...")
|
| 163 |
+
file_batch_process_btn = gr.Button("Process All Files", variant="primary")
|
| 164 |
+
|
| 165 |
+
with gr.Column():
|
| 166 |
+
file_batch_output = gr.Gallery(label="Processed Images")
|
| 167 |
+
|
| 168 |
+
file_batch_process_btn.click(
|
| 169 |
+
fn=process_batch_files,
|
| 170 |
+
inputs=[file_batch_input, file_batch_prompt],
|
| 171 |
+
outputs=file_batch_output,
|
| 172 |
+
api_visibility="public"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# Examples section
|
| 176 |
+
gr.Markdown("## 📚 Examples")
|
| 177 |
+
examples = gr.Examples(
|
| 178 |
+
examples=[
|
| 179 |
+
["https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg", "Wildlife processing"],
|
| 180 |
+
["https://gradio-builds.s3.amazonaws.com/assets/TheCheethcat.jpg", "Animal detection"],
|
| 181 |
+
["https://gradio-static-files.s3.amazonaws.com/world.mp4", "Video frame processing"]
|
| 182 |
+
],
|
| 183 |
+
inputs=[single_image_input, single_prompt],
|
| 184 |
+
outputs=single_output,
|
| 185 |
+
cache_examples=True
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Launch the application with custom theme and settings
|
| 189 |
+
demo.launch(
|
| 190 |
+
theme=custom_theme,
|
| 191 |
+
footer_links=[
|
| 192 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 193 |
+
{"label": "Gradio Documentation", "url": "https://gradio.app/docs"}
|
| 194 |
+
],
|
| 195 |
+
show_error=True,
|
| 196 |
+
debug=False
|
| 197 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow
|
| 2 |
+
gradio>=6.0
|
| 3 |
+
numpy
|
| 4 |
+
requests
|
| 5 |
+
scipy
|
| 6 |
+
pandas
|