Below is a Python script that will analyze the creation date of images in a specified directory, create a folder for each month (using the month number), and move the images to the corresponding month folder.
import os
import shutil
from datetime import datetime
from PIL import Image
from PIL.ExifTags import TAGS
def get_image_month(image_path):
try:
image = Image.open(image_path)
exif_data = image._getexif()
if exif_data:
for tag, value in exif_data.items():
if TAGS.get(tag) == 'DateTimeOriginal':
date_taken = datetime.strptime(value, '%Y:%m:%d %H:%M:%S')
return date_taken.month
except Exception as e:
print(f"Error getting EXIF data for {image_path}: {e}")
return None
def organize_images_by_month(source_folder):
if not os.path.exists(source_folder):
print(f"Source folder '{source_folder}' does not exist.")
return
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder, filename)
if os.path.isfile(file_path):
month = get_image_month(file_path)
if month is not None:
month_folder = os.path.join(source_folder, f'{month:02}')
if not os.path.exists(month_folder):
os.makedirs(month_folder)
shutil.move(file_path, os.path.join(month_folder, filename))
print(f"Moved '{filename}' to '{month_folder}'")
else:
print(f"Could not determine month for '{filename}'")
# Replace 'path/to/your/images' with the path to your images folder
source_folder = 'path/to/your/images'
organize_images_by_month(source_folder)
Sure! Here is a Python script that does the reverse: it will move images from month-numbered folders (01, 02, ..., 12) back to a specified main directory.
This script gonna takeout from sub folder to main folder.
import os
import shutil
def move_images_to_main_folder(main_folder):
if not os.path.exists(main_folder):
print(f"Main folder '{main_folder}' does not exist.")
return
for month in range(1, 13):
month_folder = os.path.join(main_folder, f'{month:02}')
if os.path.exists(month_folder):
for filename in os.listdir(month_folder):
file_path = os.path.join(month_folder, filename)
if os.path.isfile(file_path):
shutil.move(file_path, os.path.join(main_folder, filename))
print(f"Moved '{filename}' from '{month_folder}' to '{main_folder}'")
os.rmdir(month_folder)
else:
print(f"Month folder '{month_folder}' does not exist.")
# Replace 'path/to/your/images' with the path to your main images folder
main_folder = 'path/to/your/images'
move_images_to_main_folder(main_folder)
In java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class MonthFolder2 {
public static void main(String[] args) {
String mainFolder = "G:\\My Files\\Pictures\\2024"; // Replace with your main images folder path
moveImagesToMainFolder(mainFolder);
}
public static void moveImagesToMainFolder(String mainFolder) {
File mainDir = new File(mainFolder);
if (!mainDir.exists()) {
System.out.println("Main folder '" + mainFolder + "' does not exist.");
return;
}
for (int month = 1; month <= 12; month++) {
String monthFolderName = String.format("%02d", month);
File monthDir = new File(mainDir, monthFolderName);
if (monthDir.exists() && monthDir.isDirectory()) {
File[] files = monthDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
try {
Files.move(file.toPath(), new File(mainDir, file.getName()).toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println("Moved '" + file.getName() + "' from '" + monthFolderName + "' to '" + mainFolder + "'");
} catch (IOException e) {
System.out.println("Error moving file '" + file.getName() + "': " + e.getMessage());
}
}
}
}
if (monthDir.listFiles().length == 0) {
if (monthDir.delete()) {
System.out.println("Deleted empty month folder: " + monthFolderName);
} else {
System.out.println("Failed to delete month folder: " + monthFolderName);
}
}
} else {
System.out.println("Month folder '" + monthFolderName + "' does not exist.");
}
}
}
}
This script works fabously in java
#scripts
#pythonscripts
Comments
Post a Comment