import os
from PIL import Image
def read_image_list(file_path):
"""Read the list of image file names from a text file."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
with open(file_path, "r", encoding="utf-8") as file:
image_list = [line.strip() for line in file if line.strip()]
return image_list
def create_output_directory(directory):
"""Create the output directory if it does not exist."""
if not os.path.exists(directory):
os.makedirs(directory)
print(f"Created directory: {directory}")
else:
print(f"Directory already exists: {directory}")
def copy_images(images, output_directory):
"""Copy images to the output directory."""
for idx, image in enumerate(images):
if os.path.exists(image):
try:
with Image.open(image) as img:
rgb_img = img.convert("RGB") # JPEGはRGBのみ
destination = os.path.join(
output_directory, f"tongue{idx + 1:04}.jpg"
)
rgb_img.save(destination, "JPEG")
print(f"Converted {image} to {destination}")
except IOError as e:
print(f"Error copying {image}: {e}")
else:
print(f"Image not found: {image}")
def main():
"""Main function to read image list and create output directory."""
try:
list_file = "images.txt"
output_dir = "./output_images"
# Read the list of images
images = read_image_list(list_file)
print(f"Found {len(images)} images in the list.")
# Create the output directory
create_output_directory(output_dir)
# Copy images to the output directory
copy_images(images, output_dir)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
# picker.py - A script to read a list of image files and prepare an output directory
# This script reads a list of image file names from a text file and creates an output directory
# for further processing of those images.