File size: 6,220 Bytes
ad9c33b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fea1536
ad9c33b
 
 
 
 
 
 
 
90bccbf
ad9c33b
 
 
 
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
'''
this script is used to split an image into tiles and download them as a zip file.

The script defines two functions: split_image and create_zip_file. The
split_image function takes an input image and tile size as arguments and splits
the image into smaller tiles. The create_zip_file function takes a list of image
tiles and a prefix as arguments and creates a zip file containing all the tiles.

The process_image function combines the two functions to split the input image
into tiles and create a zip file of the tiles. The main function launches a Gradio
app that allows users to upload an image, specify the tile size, view the resulting
tiles, and download all tiles in a zip archive.

To run the script, simply execute it in a Python environment. The Gradio app will
open in a new tab in your web browser, allowing you to interact with the image
splitting functionality.

How to use the script:
1. Run the script in a Python environment.
```python
python split_and_zip.py
```
2. Open the provided local URL in a web browser.
3. Upload an image file.
4. Adjust the tile size using the slider.
5. Click the "Process Image" button to split the image into tiles.
6. View the resulting tiles in the gallery.
7. Click the "Download Tiles" button to download all tiles as a zip file.



'''
import os
import cv2
import numpy as np
import gradio as gr
import tempfile
import zipfile
import logging
from typing import List, Optional, Tuple

# Configure logging for console output
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[logging.StreamHandler()]
)


def split_image(image: np.ndarray, tile_size: int) -> List[np.ndarray]:
    """
    Split a large image into smaller tiles.

    Parameters
    ----------
    image : np.ndarray
        Input image as a NumPy array.
    tile_size : int
        Size of each square tile in pixels.

    Returns
    -------
    List[np.ndarray]
        A list of image tiles as NumPy arrays.

    Examples
    --------
    >>> image = cv2.imread('path/to/image.jpg')
    >>> tiles = split_image(image, tile_size=500)
    >>> len(tiles)
    16
    """
    if image is None:
        logging.warning("No image provided for splitting")
        return []

    h, w = image.shape[:2]
    logging.info(f"Starting image split - Image dimensions: {w}x{h}, Tile size: {tile_size}x{tile_size}")
    tiles = []

    for y in range(0, h, tile_size):
        for x in range(0, w, tile_size):
            end_y = min(y + tile_size, h)
            end_x = min(x + tile_size, w)
            tile = image[y:end_y, x:end_x]
            if tile.shape[0] > 0 and tile.shape[1] > 0:
                tiles.append(tile)
                logging.debug(f"Created tile {len(tiles)}: position ({x}, {y}), size ({end_x-x}x{end_y-y})")
    
    logging.info(f"Image splitting completed - Generated {len(tiles)} tiles")
    return tiles


def create_zip_file(tiles: List[np.ndarray], prefix: str = "tile") -> str:
    """
    Create a zip file containing all image tiles.

    Parameters
    ----------
    tiles : List[np.ndarray]
        List of image tiles as NumPy arrays.
    prefix : str, optional
        Prefix for each tile filename, by default "tile".

    Returns
    -------
    str
        Path to the created zip file.

    Examples
    --------
    >>> zip_path = create_zip_file(tiles, prefix='sample')
    >>> os.path.exists(zip_path)
    True
    """
    if not tiles:
        logging.warning("No tiles provided for zip creation")
        return ""
    
    logging.info(f"Creating zip file with {len(tiles)} tiles using prefix '{prefix}'")
    temp_dir = tempfile.mkdtemp()
    zip_path = os.path.join(temp_dir, "tiles.zip")

    with zipfile.ZipFile(zip_path, 'w') as zf:
        for i, tile in enumerate(tiles):
            tile_path = os.path.join(temp_dir, f"{prefix}_{i}.png")
            cv2.imwrite(tile_path, cv2.cvtColor(tile, cv2.COLOR_RGB2BGR))
            zf.write(tile_path, f"{prefix}_{i}.png")
            logging.debug(f"Added tile {i+1}/{len(tiles)} to zip: {prefix}_{i}.png")

    logging.info(f"Zip file created successfully at: {zip_path}")
    return zip_path


def process_image(image: np.ndarray, tile_size: int) -> Tuple[List[np.ndarray], str]:
    """
    Split the input image into tiles and create a zip file of the tiles.

    Parameters
    ----------
    image : np.ndarray
        Input image as a NumPy array.
    tile_size : int
        Size of each square tile in pixels.

    Returns
    -------
    Tuple[List[np.ndarray], str]
        A tuple containing the list of image tiles and the path to the zip file.

    Examples
    --------
    >>> tiles, zip_path = process_image(image, tile_size=500)
    >>> len(tiles)
    16
    >>> os.path.exists(zip_path)
    True
    """
    logging.info("=== Starting image processing ===")
    tiles = split_image(image, tile_size)
    zip_path = create_zip_file(tiles) if tiles else ""
    logging.info("=== Image processing completed ===")
    return tiles, zip_path


def main():
    """
    Launch the Gradio app for splitting images into tiles and downloading them as a zip file.

    The app allows users to upload an image, specify the tile size, view the resulting tiles,
    and download all tiles in a zip archive.

    Examples
    --------
    Run the script and open the provided local URL in a web browser.
    """
    logging.info("Initializing Image Splitter application")
    
    with gr.Blocks() as interface:
        with gr.Row():
            input_image = gr.Image(type="numpy", label="Input Image")
            tile_size = gr.Slider(
                minimum=100, maximum=1000, step=100, value=500, label="Tile Size"
            )

        with gr.Row():
            submit_btn = gr.Button("Process Image")

        with gr.Row():
            gallery = gr.Gallery(label="Tiles", columns=3)
            download_btn = gr.File(label="Download Tiles", visible=False)

        submit_btn.click(
            fn=process_image,
            inputs=[input_image, tile_size],
            outputs=[gallery, download_btn],
        )

    logging.info("Starting Gradio interface")
    interface.launch()


if __name__ == '__main__':
    main()