Open Zeuswaed1937 opened 2 days ago
Based on the rest of the code and the relevant information on the number of images, I guess the split rules for the Chundata dataset are as follows: Split 1 image into 54 images on average, with 6 rows and 9 columns. The original image size is 5184 3456, and the split size is 576 576 Naming convention for split images: c98.jpg → c98_1_2.jpg bw98.bmp → c98_1_2.bmp Among them, “_1” represents the number of rows, and “_2” represents the number of columns
Here is the Code:
import os
from PIL import Image
def split_image(image_path,
save_dir,
rows=6, # Split into 6 rows
cols=9, # Split into 9 columns
tile_size=(576, 576)): # Size of the split images
# Get the original image name (without extension)
image_name = os.path.splitext(os.path.basename(image_path))[0]
# Open the image
image = Image.open(image_path)
# Get the width and height of the image
img_width, img_height = image.size
# Calculate the width and height of each tile
tile_width, tile_height = tile_size
# Ensure the save directory exists
os.makedirs(save_dir, exist_ok=True)
# Loop through rows and columns to save each tile
for row in range(rows):
for col in range(cols):
# Calculate the position of the tile
left = col * tile_width
upper = row * tile_height
right = left + tile_width
lower = upper + tile_height
# Crop the image
tile = image.crop((left, upper, right, lower))
# Generate the filename, row-column order can be swapped
# tile_name = f"{image_name}_{row+1}_{col+1}.jpg" # For original images
tile_name = f"{image_name}_{row+1}_{col+1}.bmp" # For Mask images
tile_path = os.path.join(save_dir, tile_name)
# Save the tile
tile.save(tile_path)
# Set the input folder path and output folder path
input_folder = r"path\to\original\image\folder" # Replace with the path to the original image folder
output_folder = r"path\to\splited\image\folder" # Replace with the path to save split images
# Loop through all image files in the folder
for filename in os.listdir(input_folder):
# Check if the file is an image
if filename.lower().endswith(('.bmp')): # For original images
# if filename.lower().endswith(('.jpg')): # For Mask images
image_path = os.path.join(input_folder, filename)
# Define the output folder path (using the structure of the input folder)
image_output_folder = os.path.join(output_folder, os.path.splitext(filename)[0])
os.makedirs(image_output_folder, exist_ok=True)
# Split the image and save to the specified folder, further variables can be specified
split_image(image_path, image_output_folder)
print('Done for ' + filename)
print('All done')
Thank you very much for sharing the code!
During the process of running your code, I noticed multiple mentions of the path *'data/original_stplit_resized/'**
However, I am unable to obtain eligible datasets from the shared dataset https://sites.google.com/view/pchun/ I tried to find the code to split the image, but was unsuccessful
I noticed that you shared two dataset links in readme.md: _https://drive.google.com/drive/folders/1W_jDSnJvRNCwBB9KAty0JzsL8dC0QZr7?usp=drive_link https://drive.google.com/drive/folders/1xTfMC7yiWWi2BFbdcG9ByMX4XWdW5cIM?usp=drive_link_ None of them can be accessed
I would appreciate that if you could: Update the sharing link for Google Drive Provide code for splitting images
Thank you!