import csv
import os
from PIL import Image
BASE_DIR = r"\\gabor\Data\TongueImaging\臨床データ\2025 村上先生整理データ\DeepTIAS後_亥鼻_村上_2025.08修正"
DEEPTIAS_DIR = r"3-imgProc\cvtColor\LabImg"
CASES_FILE = "cases.txt"
OUTPUT_DIR = "./output_images"
OUTPUT_CSV = os.path.join(OUTPUT_DIR, "mappings.csv")
def read_image_list():
"""Read the list of image file names from a text file."""
if not os.path.exists(CASES_FILE):
raise FileNotFoundError(f"The file {CASES_FILE} does not exist.")
with open(CASES_FILE, "r", encoding="utf-8") as file:
image_list = [
os.path.join(BASE_DIR, line.strip(), DEEPTIAS_DIR)
for line in file
if line.strip()
]
return image_list
def create_output_directory():
"""Create the output directory if it does not exist."""
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
print(f"Created directory: {OUTPUT_DIR}")
else:
print(f"Directory already exists: {OUTPUT_DIR}")
def copy_images(images):
"""Copy images to the output directory."""
count = 500
# Write CSV mapping of source -> destination
with open(OUTPUT_CSV, "w", newline="", encoding="utf-8-sig") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["source", "destination"])
for idx, image in enumerate(images):
if not os.path.exists(image):
print(f"Image directory does not exist: {image}")
continue
files = [
f
for f in os.listdir(image)
if f.lower().endswith(".png") and f.lower() != "1.png"
]
for file in files:
image_path = os.path.join(image, file)
try:
destination = os.path.join(OUTPUT_DIR, f"tongue{count:04}.jpg")
with Image.open(image_path) as img:
rgb_img = img.convert("RGB") # JPEGはRGBのみ
rgb_img.save(destination, "JPEG")
print(f"Converted {image_path} to {destination}")
writer.writerow(
[os.path.abspath(image_path), os.path.abspath(destination)]
)
count += 1
except IOError as e:
print(f"Error copying {image_path}: {e}")
def main():
"""Main function to read image list and create output directory."""
try:
# Read the list of images
images = read_image_list()
print(f"Found {len(images)} images in the list.")
# Create the output directory
create_output_directory()
# Copy images to the output directory
copy_images(images)
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.