mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-24 10:39:19 +02:00
another one
This commit is contained in:
parent
f510ee0f1b
commit
29dd49c3b0
1 changed files with 36 additions and 0 deletions
36
split_image.py
Normal file
36
split_image.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from PIL import Image
|
||||
import os
|
||||
|
||||
def split_image(image_path, output_folder, square_size):
|
||||
# Open the image
|
||||
image = Image.open(image_path)
|
||||
|
||||
# Ensure the output folder exists
|
||||
if not os.path.exists(output_folder):
|
||||
os.makedirs(output_folder)
|
||||
|
||||
# Get image dimensions
|
||||
width, height = image.size
|
||||
|
||||
# Calculate the number of rows and columns
|
||||
rows = height // square_size
|
||||
columns = width // square_size
|
||||
|
||||
# Iterate over rows and columns to crop and save squares
|
||||
for row in range(rows):
|
||||
for col in range(columns):
|
||||
left = col * square_size
|
||||
upper = row * square_size
|
||||
right = left + square_size
|
||||
lower = upper + square_size
|
||||
square = image.crop((left, upper, right, lower))
|
||||
|
||||
# Save the cropped square
|
||||
square.save(os.path.join(output_folder, f"image_{row}_{col}.jpg"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_image_path = "path/to/your/input/image.jpg"
|
||||
output_folder = "path/to/your/output/folder"
|
||||
square_size = 200 # Adjust this to the desired size of each square
|
||||
|
||||
split_image(input_image_path, output_folder, square_size)
|
||||
Loading…
Add table
Add a link
Reference in a new issue