Created
December 23, 2024 18:44
-
-
Save shauan1508/f130294ab389658e9a244d637536a382 to your computer and use it in GitHub Desktop.
Microscope Camera File Renamer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
from itertools import cycle | |
def create_folders(base_path, work_order, serial_numbers): | |
work_order_path = os.path.join(base_path, work_order) | |
if not os.path.exists(work_order_path): | |
os.makedirs(work_order_path) | |
for serial_number in serial_numbers: | |
serial_folder = os.path.join(work_order_path, serial_number) | |
if not os.path.exists(serial_folder): | |
os.makedirs(serial_folder) | |
def rename_and_distribute_images(base_path, work_order, serial_numbers): | |
work_order_path = os.path.join(base_path, work_order) | |
image_files = [f for f in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, f))] | |
image_files.sort() | |
images_per_serial = 6 | |
# Assuming there are exactly 36 images per work order | |
assert len(image_files) == 36, "Expected exactly 36 images in the folder." | |
for i, serial_number in enumerate(serial_numbers): | |
serial_folder = os.path.join(work_order_path, serial_number) | |
# Select 6 images for this serial number | |
start_index = i * images_per_serial | |
end_index = start_index + images_per_serial | |
selected_images = image_files[start_index:end_index] | |
for image in selected_images: | |
src_path = os.path.join(base_path, image) | |
dest_path = os.path.join(serial_folder, image) # Keep original name | |
shutil.move(src_path, dest_path) # Move the file instead of copying | |
def undo_changes(base_path, work_order, serial_numbers): | |
work_order_path = os.path.join(base_path, work_order) | |
for serial_number in serial_numbers: | |
serial_folder = os.path.join(work_order_path, serial_number) | |
if os.path.exists(serial_folder): | |
for image in os.listdir(serial_folder): | |
src_path = os.path.join(serial_folder, image) | |
dest_path = os.path.join(base_path, image) # Original location | |
shutil.move(src_path, dest_path) # Move the file back to the original location | |
# Remove the empty serial number folder | |
os.rmdir(serial_folder) | |
# Remove the work order folder if it's empty | |
if not os.listdir(work_order_path): | |
os.rmdir(work_order_path) | |
def main(): | |
action = input("Do you want to (1) organize or (2) undo? Enter 1 or 2: ").strip() | |
base_path = input("Enter the path to the folder containing images: ").strip() | |
work_order = input("Enter the work order number: ").strip() | |
serial_numbers_input = input("Enter the serial numbers separated by commas (e.g., SN001,SN002,SN003,SN004,SN005,SN006): ").strip() | |
serial_numbers = [sn.strip() for sn in serial_numbers_input.split(',')] | |
if len(serial_numbers) != 6: | |
print("Error: You must provide exactly 6 serial numbers.") | |
return | |
if action == "1": | |
create_folders(base_path, work_order, serial_numbers) | |
rename_and_distribute_images(base_path, work_order, serial_numbers) | |
print("Images have been organized.") | |
elif action == "2": | |
undo_changes(base_path, work_order, serial_numbers) | |
print("Changes have been undone.") | |
else: | |
print("Invalid action. Please enter 1 or 2.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment