Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +31 -0
- README.md +8 -6
- app.py +204 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# Install system dependencies for OpenCV
|
6 |
+
RUN apt-get update && \
|
7 |
+
apt-get install -y \
|
8 |
+
libglib2.0-0 \
|
9 |
+
libsm6 \
|
10 |
+
libxext6 \
|
11 |
+
libxrender-dev \
|
12 |
+
libgomp1 \
|
13 |
+
libglib2.0-0 \
|
14 |
+
libgtk-3-0 \
|
15 |
+
ffmpeg \
|
16 |
+
libsm6 \
|
17 |
+
libxext6 && \
|
18 |
+
rm -rf /var/lib/apt/lists/*
|
19 |
+
|
20 |
+
# Copy requirements and install Python dependencies
|
21 |
+
COPY requirements.txt .
|
22 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
23 |
+
|
24 |
+
# Copy application code
|
25 |
+
COPY app.py .
|
26 |
+
|
27 |
+
# Expose port for Gradio
|
28 |
+
EXPOSE 7860
|
29 |
+
|
30 |
+
# Run the application
|
31 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
---
|
2 |
title: Image Splitter
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk:
|
|
|
|
|
7 |
pinned: false
|
8 |
license: cc-by-3.0
|
9 |
-
short_description:
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Image Splitter
|
3 |
+
emoji: 👀
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.11.0
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
10 |
license: cc-by-3.0
|
11 |
+
short_description: This is an app that breaks down the image into tiles
|
12 |
---
|
13 |
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
this script is used to split an image into tiles and download them as a zip file.
|
3 |
+
|
4 |
+
The script defines two functions: split_image and create_zip_file. The
|
5 |
+
split_image function takes an input image and tile size as arguments and splits
|
6 |
+
the image into smaller tiles. The create_zip_file function takes a list of image
|
7 |
+
tiles and a prefix as arguments and creates a zip file containing all the tiles.
|
8 |
+
|
9 |
+
The process_image function combines the two functions to split the input image
|
10 |
+
into tiles and create a zip file of the tiles. The main function launches a Gradio
|
11 |
+
app that allows users to upload an image, specify the tile size, view the resulting
|
12 |
+
tiles, and download all tiles in a zip archive.
|
13 |
+
|
14 |
+
To run the script, simply execute it in a Python environment. The Gradio app will
|
15 |
+
open in a new tab in your web browser, allowing you to interact with the image
|
16 |
+
splitting functionality.
|
17 |
+
|
18 |
+
How to use the script:
|
19 |
+
1. Run the script in a Python environment.
|
20 |
+
```python
|
21 |
+
python split_and_zip.py
|
22 |
+
```
|
23 |
+
2. Open the provided local URL in a web browser.
|
24 |
+
3. Upload an image file.
|
25 |
+
4. Adjust the tile size using the slider.
|
26 |
+
5. Click the "Process Image" button to split the image into tiles.
|
27 |
+
6. View the resulting tiles in the gallery.
|
28 |
+
7. Click the "Download Tiles" button to download all tiles as a zip file.
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
'''
|
33 |
+
import os
|
34 |
+
import cv2
|
35 |
+
import numpy as np
|
36 |
+
import gradio as gr
|
37 |
+
import tempfile
|
38 |
+
import zipfile
|
39 |
+
import logging
|
40 |
+
from typing import List, Optional, Tuple
|
41 |
+
|
42 |
+
# Configure logging for console output
|
43 |
+
logging.basicConfig(
|
44 |
+
level=logging.INFO,
|
45 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
46 |
+
handlers=[logging.StreamHandler()]
|
47 |
+
)
|
48 |
+
|
49 |
+
|
50 |
+
def split_image(image: np.ndarray, tile_size: int) -> List[np.ndarray]:
|
51 |
+
"""
|
52 |
+
Split a large image into smaller tiles.
|
53 |
+
|
54 |
+
Parameters
|
55 |
+
----------
|
56 |
+
image : np.ndarray
|
57 |
+
Input image as a NumPy array.
|
58 |
+
tile_size : int
|
59 |
+
Size of each square tile in pixels.
|
60 |
+
|
61 |
+
Returns
|
62 |
+
-------
|
63 |
+
List[np.ndarray]
|
64 |
+
A list of image tiles as NumPy arrays.
|
65 |
+
|
66 |
+
Examples
|
67 |
+
--------
|
68 |
+
>>> image = cv2.imread('path/to/image.jpg')
|
69 |
+
>>> tiles = split_image(image, tile_size=500)
|
70 |
+
>>> len(tiles)
|
71 |
+
16
|
72 |
+
"""
|
73 |
+
if image is None:
|
74 |
+
logging.warning("No image provided for splitting")
|
75 |
+
return []
|
76 |
+
|
77 |
+
h, w = image.shape[:2]
|
78 |
+
logging.info(f"Starting image split - Image dimensions: {w}x{h}, Tile size: {tile_size}x{tile_size}")
|
79 |
+
tiles = []
|
80 |
+
|
81 |
+
for y in range(0, h, tile_size):
|
82 |
+
for x in range(0, w, tile_size):
|
83 |
+
end_y = min(y + tile_size, h)
|
84 |
+
end_x = min(x + tile_size, w)
|
85 |
+
tile = image[y:end_y, x:end_x]
|
86 |
+
if tile.shape[0] > 0 and tile.shape[1] > 0:
|
87 |
+
tiles.append(tile)
|
88 |
+
logging.debug(f"Created tile {len(tiles)}: position ({x}, {y}), size ({end_x-x}x{end_y-y})")
|
89 |
+
|
90 |
+
logging.info(f"Image splitting completed - Generated {len(tiles)} tiles")
|
91 |
+
return tiles
|
92 |
+
|
93 |
+
|
94 |
+
def create_zip_file(tiles: List[np.ndarray], prefix: str = "tile") -> str:
|
95 |
+
"""
|
96 |
+
Create a zip file containing all image tiles.
|
97 |
+
|
98 |
+
Parameters
|
99 |
+
----------
|
100 |
+
tiles : List[np.ndarray]
|
101 |
+
List of image tiles as NumPy arrays.
|
102 |
+
prefix : str, optional
|
103 |
+
Prefix for each tile filename, by default "tile".
|
104 |
+
|
105 |
+
Returns
|
106 |
+
-------
|
107 |
+
str
|
108 |
+
Path to the created zip file.
|
109 |
+
|
110 |
+
Examples
|
111 |
+
--------
|
112 |
+
>>> zip_path = create_zip_file(tiles, prefix='sample')
|
113 |
+
>>> os.path.exists(zip_path)
|
114 |
+
True
|
115 |
+
"""
|
116 |
+
if not tiles:
|
117 |
+
logging.warning("No tiles provided for zip creation")
|
118 |
+
return ""
|
119 |
+
|
120 |
+
logging.info(f"Creating zip file with {len(tiles)} tiles using prefix '{prefix}'")
|
121 |
+
temp_dir = tempfile.mkdtemp()
|
122 |
+
zip_path = os.path.join(temp_dir, "tiles.zip")
|
123 |
+
|
124 |
+
with zipfile.ZipFile(zip_path, 'w') as zf:
|
125 |
+
for i, tile in enumerate(tiles):
|
126 |
+
tile_path = os.path.join(temp_dir, f"{prefix}_{i}.png")
|
127 |
+
cv2.imwrite(tile_path, cv2.cvtColor(tile, cv2.COLOR_RGB2BGR))
|
128 |
+
zf.write(tile_path, f"{prefix}_{i}.png")
|
129 |
+
logging.debug(f"Added tile {i+1}/{len(tiles)} to zip: {prefix}_{i}.png")
|
130 |
+
|
131 |
+
logging.info(f"Zip file created successfully at: {zip_path}")
|
132 |
+
return zip_path
|
133 |
+
|
134 |
+
|
135 |
+
def process_image(image: np.ndarray, tile_size: int) -> Tuple[List[np.ndarray], str]:
|
136 |
+
"""
|
137 |
+
Split the input image into tiles and create a zip file of the tiles.
|
138 |
+
|
139 |
+
Parameters
|
140 |
+
----------
|
141 |
+
image : np.ndarray
|
142 |
+
Input image as a NumPy array.
|
143 |
+
tile_size : int
|
144 |
+
Size of each square tile in pixels.
|
145 |
+
|
146 |
+
Returns
|
147 |
+
-------
|
148 |
+
Tuple[List[np.ndarray], str]
|
149 |
+
A tuple containing the list of image tiles and the path to the zip file.
|
150 |
+
|
151 |
+
Examples
|
152 |
+
--------
|
153 |
+
>>> tiles, zip_path = process_image(image, tile_size=500)
|
154 |
+
>>> len(tiles)
|
155 |
+
16
|
156 |
+
>>> os.path.exists(zip_path)
|
157 |
+
True
|
158 |
+
"""
|
159 |
+
logging.info("=== Starting image processing ===")
|
160 |
+
tiles = split_image(image, tile_size)
|
161 |
+
zip_path = create_zip_file(tiles) if tiles else ""
|
162 |
+
logging.info("=== Image processing completed ===")
|
163 |
+
return tiles, zip_path
|
164 |
+
|
165 |
+
|
166 |
+
def main():
|
167 |
+
"""
|
168 |
+
Launch the Gradio app for splitting images into tiles and downloading them as a zip file.
|
169 |
+
|
170 |
+
The app allows users to upload an image, specify the tile size, view the resulting tiles,
|
171 |
+
and download all tiles in a zip archive.
|
172 |
+
|
173 |
+
Examples
|
174 |
+
--------
|
175 |
+
Run the script and open the provided local URL in a web browser.
|
176 |
+
"""
|
177 |
+
logging.info("Initializing Image Splitter application")
|
178 |
+
|
179 |
+
with gr.Blocks() as interface:
|
180 |
+
with gr.Row():
|
181 |
+
input_image = gr.Image(type="numpy", label="Input Image")
|
182 |
+
tile_size = gr.Slider(
|
183 |
+
minimum=100, maximum=1000, step=100, value=500, label="Tile Size"
|
184 |
+
)
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
submit_btn = gr.Button("Process Image")
|
188 |
+
|
189 |
+
with gr.Row():
|
190 |
+
gallery = gr.Gallery(label="Tiles", columns=3)
|
191 |
+
download_btn = gr.File(label="Download Tiles")
|
192 |
+
|
193 |
+
submit_btn.click(
|
194 |
+
fn=process_image,
|
195 |
+
inputs=[input_image, tile_size],
|
196 |
+
outputs=[gallery, download_btn],
|
197 |
+
)
|
198 |
+
|
199 |
+
logging.info("Starting Gradio interface")
|
200 |
+
interface.launch()
|
201 |
+
|
202 |
+
|
203 |
+
if __name__ == '__main__':
|
204 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
opencv-python
|
3 |
+
gradio
|