-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_cells.py
More file actions
38 lines (29 loc) · 1.11 KB
/
Copy pathgrid_cells.py
File metadata and controls
38 lines (29 loc) · 1.11 KB
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
import os
from PIL import Image
# Load the image
image_path = "map-more-manhattan.png" # Update with your image path
image = Image.open(image_path)
# Set the grid size
grid_size = 5
# Get image dimensions
image_width, image_height = image.size
# Calculate the dimensions of each grid cell
cell_width = image_width // grid_size
cell_height = image_height // grid_size
# Create a directory to store the grid cells
output_dir = "grid_cells" # Update with your desired output path
os.makedirs(output_dir, exist_ok=True)
# Split the image into a 10x10 grid
for row in range(grid_size):
for col in range(grid_size):
# Calculate the coordinates of the current cell
left = col * cell_width
upper = row * cell_height
right = left + cell_width
lower = upper + cell_height
# Crop the cell from the image
cell = image.crop((left, upper, right, lower))
# Save the cell as a separate file
cell_filename = f"cell_{row}_{col}.png"
cell.save(os.path.join(output_dir, cell_filename))
print("Image has been successfully split into a 10x10 grid and saved.")