Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import PIL
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
import tempfile
|
6 |
+
import zipfile
|
7 |
+
import shutil
|
8 |
+
from pathlib import Path
|
9 |
+
|
10 |
+
# Define target sizes for different viewports
|
11 |
+
SIZES = [
|
12 |
+
(480, 423),
|
13 |
+
(696, 613),
|
14 |
+
(960, 846),
|
15 |
+
(1095, 965),
|
16 |
+
(1280, 1128),
|
17 |
+
(1440, 1269),
|
18 |
+
(1670, 1472),
|
19 |
+
(1870, 1648),
|
20 |
+
(2048, 1805)
|
21 |
+
]
|
22 |
+
|
23 |
+
def resize_image(img, target_size):
|
24 |
+
"""Resize image maintaining aspect ratio"""
|
25 |
+
img_copy = img.copy()
|
26 |
+
return img_copy.resize(target_size, PIL.Image.Resampling.LANCZOS)
|
27 |
+
|
28 |
+
def generate_html_snippet(image_path_prefix):
|
29 |
+
"""Generate HTML code snippet with srcset"""
|
30 |
+
srcset_items = [f"{image_path_prefix}-{w}x{h}.jpg {w}w" for w, h in SIZES]
|
31 |
+
srcset_str = ",\n ".join(srcset_items)
|
32 |
+
|
33 |
+
sizes_items = [
|
34 |
+
f"(max-width: {w}px) {w}px" for w, _ in SIZES[:-1]
|
35 |
+
] + [f"{SIZES[-1][0]}px"]
|
36 |
+
sizes_str = ",\n ".join(sizes_items)
|
37 |
+
|
38 |
+
# Use the middle size as default src
|
39 |
+
default_size = SIZES[4] # 1280x1128
|
40 |
+
|
41 |
+
html = f'''<!-- Responsive Image -->
|
42 |
+
<img
|
43 |
+
src="{image_path_prefix}-{default_size[0]}x{default_size[1]}.jpg"
|
44 |
+
srcset="
|
45 |
+
{srcset_str}
|
46 |
+
"
|
47 |
+
sizes="
|
48 |
+
{sizes_str}
|
49 |
+
"
|
50 |
+
alt="Your image description"
|
51 |
+
style="max-width: 100%; height: auto;"
|
52 |
+
>'''
|
53 |
+
|
54 |
+
return html
|
55 |
+
|
56 |
+
def process_image(input_image):
|
57 |
+
"""Main processing function"""
|
58 |
+
if input_image is None:
|
59 |
+
return None, None, "Please upload an image"
|
60 |
+
|
61 |
+
# Create temporary directory for processed images
|
62 |
+
temp_dir = tempfile.mkdtemp()
|
63 |
+
zip_path = os.path.join(temp_dir, "responsive_images.zip")
|
64 |
+
|
65 |
+
try:
|
66 |
+
# Open and process image
|
67 |
+
img = Image.open(input_image.name)
|
68 |
+
|
69 |
+
# Convert to RGB if necessary
|
70 |
+
if img.mode != 'RGB':
|
71 |
+
img = img.convert('RGB')
|
72 |
+
|
73 |
+
# Process each size
|
74 |
+
processed_paths = []
|
75 |
+
for width, height in SIZES:
|
76 |
+
resized = resize_image(img, (width, height))
|
77 |
+
output_path = os.path.join(temp_dir, f"image-{width}x{height}.jpg")
|
78 |
+
resized.save(output_path, "JPEG", quality=90)
|
79 |
+
processed_paths.append(output_path)
|
80 |
+
|
81 |
+
# Create zip file
|
82 |
+
with zipfile.ZipFile(zip_path, 'w') as zf:
|
83 |
+
for path in processed_paths:
|
84 |
+
zf.write(path, os.path.basename(path))
|
85 |
+
|
86 |
+
# Generate HTML snippet
|
87 |
+
html_snippet = generate_html_snippet("images/image")
|
88 |
+
|
89 |
+
return zip_path, html_snippet, "Processing completed successfully!"
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
return None, None, f"Error processing image: {str(e)}"
|
93 |
+
|
94 |
+
finally:
|
95 |
+
# Clean up temporary files except zip
|
96 |
+
for path in processed_paths:
|
97 |
+
try:
|
98 |
+
os.remove(path)
|
99 |
+
except:
|
100 |
+
pass
|
101 |
+
|
102 |
+
# Create Gradio interface
|
103 |
+
with gr.Blocks(title="Responsive Image Generator") as app:
|
104 |
+
gr.Markdown("""
|
105 |
+
# Responsive Image Generator
|
106 |
+
|
107 |
+
Upload an image to generate optimized versions for different viewport sizes.
|
108 |
+
You'll receive:
|
109 |
+
1. A ZIP file containing all sized versions
|
110 |
+
2. HTML code snippet with proper srcset attributes
|
111 |
+
""")
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
with gr.Column():
|
115 |
+
input_image = gr.Image(
|
116 |
+
label="Upload Original Image",
|
117 |
+
type="filepath"
|
118 |
+
)
|
119 |
+
process_btn = gr.Button("Process Image")
|
120 |
+
|
121 |
+
with gr.Column():
|
122 |
+
output_zip = gr.File(label="Download Processed Images")
|
123 |
+
output_html = gr.Code(
|
124 |
+
label="HTML Code Snippet",
|
125 |
+
language="html"
|
126 |
+
)
|
127 |
+
output_message = gr.Textbox(label="Status")
|
128 |
+
|
129 |
+
process_btn.click(
|
130 |
+
fn=process_image,
|
131 |
+
inputs=[input_image],
|
132 |
+
outputs=[output_zip, output_html, output_message]
|
133 |
+
)
|
134 |
+
|
135 |
+
# Launch the app
|
136 |
+
app.launch()
|