diff --git a/ImagesCopier.py b/ImagesCopier.py index f6368d3..c83b741 100644 --- a/ImagesCopier.py +++ b/ImagesCopier.py @@ -1,61 +1,84 @@ +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(file_path): + +def read_image_list(): """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.") + if not os.path.exists(CASES_FILE): + raise FileNotFoundError(f"The file {CASES_FILE} does not exist.") - with open(file_path, "r", encoding="utf-8") as file: - image_list = [line.strip() for line in file if line.strip()] + 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(directory): +def create_output_directory(): """Create the output directory if it does not exist.""" - if not os.path.exists(directory): - os.makedirs(directory) - print(f"Created directory: {directory}") + if not os.path.exists(OUTPUT_DIR): + os.makedirs(OUTPUT_DIR) + print(f"Created directory: {OUTPUT_DIR}") else: - print(f"Directory already exists: {directory}") + print(f"Directory already exists: {OUTPUT_DIR}") -def copy_images(images, output_directory): +def copy_images(images): """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" + 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)] ) - 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}") + 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: - list_file = "images.txt" - output_dir = "./output_images" - # Read the list of images - images = read_image_list(list_file) + images = read_image_list() print(f"Found {len(images)} images in the list.") # Create the output directory - create_output_directory(output_dir) + create_output_directory() # Copy images to the output directory - copy_images(images, output_dir) + copy_images(images) except Exception as e: print(f"An error occurred: {e}") diff --git a/cases.txt b/cases.txt new file mode 100644 index 0000000..4bb2a73 --- /dev/null +++ b/cases.txt @@ -0,0 +1,484 @@ +13042 +58139 +232148 +307853 +355201 +356031 +512321 +513119 +572936 +864510 +912583 +995391 +1262285 +1380037 +1614927 +1633098 +1798021 +2010377 +2083054 +2091655 +2362931 +2383662 +2507398 +2577366 +2748463 +2802800 +2859877(1-3) +2922704 +2946014 +2954019 +3012866 +3084960 +3114618 +3216877 +3238963 +3326800 +3535325 +3616811 +3742118 +3794340 +3794349 +3850523 +3850770 +3954300 +4003641 +4039277 +4158080 +4206558 +4284912 +4292946 +4433806 +4451961 +4529297 +4563222 +4615835 +4747404 +4763505 +4813271 +4894588 +4952581 +5009914 +5113558 +5158632 +5204928 +5303638 +5346598 +5364943 +5426805 +5453809 +5522066 +5539912 +5584794 +5632914 +5679599 +5798012 +5812556 +5916261 +6046936 +6053097 +6126878 +6132587 +6197490 +6306915 +6350231 +6402642 +6456391 +6468949 +6512750 +6517380 +6542577 +6553438 +6581523 +6591364 +6671596 +6705578 +6707705 +6894333 +7063233 +7065646 +7067336 +7080412 +7089783 +7110027 +7116298 +7150920 +7170573 +7179860 +7215399 +7238013 +7254231 +7255524 +7303441 +7316340 +7328871 +7332685 +7373145 +7377743 +7391457 +7405004 +7436967 +7455360 +7466016 +7489264 +7519954 +7531109 +7536175 +7556669 +7574506 +7596722 +7674453 +7682012 +7717374 +7744903 +7749953 +7756030 +7764716 +7780673 +7817061 +7836640 +7840223 +7845215 +7851168 +7898163 +7909708 +7951712 +7959790 +7964956 +8021423 +8079800 +8089377 +8097822 +8106067 +8152930 +8175481 +8186993 +8203579 +8206796 +8244097 +8259178 +8292171 +8322728 +8338381 +8345862 +8355977 +8393861 +8403881 +8426622 +8451167 +8473947 +8484680 +8493327 +8493806 +8496414 +8515849 +8516161 +8523437 +8529767 +8545549 +8552798 +8567260 +8624840 +8650926 +8666651 +8679000 +8730103 +8733069 +8752550 +8757177 +8772884 +8792932 +8796976 +8800581 +8810654 +8821599 +8822185 +8822728 +8846916 +8856207 +8856500 +8857851 +8858434 +8879497 +8895080 +8899734 +8910240 +8921299 +8985271 +8995591 +8995807 +8998428 +8999779 +10117239 +10119831 +10122772 +10126266 +10143663 +10144725 +10145294 +10161278 +10167524 +10171514 +10172126 +10177567 +10177916 +10181834 +10183801 +10186800 +10206720 +10222977 +10228311 +10244597 +10244597 校正できない +10250408 +10251762 +10254644 +10258819 +10264935 +10267761 +10272060 +10273278 +10276701 +10277948 +10308400 +10308934 +10313233 +10320600 +10327438 +10337240 +10342839 +10364680 +10373444 +10373848_ +10390274 +10391121 +10418736 +10424067 +10424706 +10438161 +10441246 +10442744_ +10444375 +10448670 +10455526_ +10458525 +10462616 +10474352 +10476550_ +10478006_ +10479227 +10482458 +10489563 +10492879 +10507487 +10507924_ +10509483 +10513095 +10515701 +10521872 +10523878 +10524480 +10534742 +10535556_ +10544701 +10547335 +10548107_ +10551976_ +10555109_ +10555268 +10558586 +10568213 +10572085 +10572665 +10574081_ +10578142 +10578650_ +10583281 +10587296 +10598548 +10599046 +10607464 +10616967 +10618165 +10622100_ +10625330 +10628267 +10629215 +10630446_ +10630466 +10631951 +10646087 +10649842 +10664458 +10670705 +10673876 +10675784_ +10676989 +10677461 +10686007 +10689048 +10689598 +10689859 +10692022 +10711705 +10712128 +10713365 +10717758 +10721386 +10727267 +10728316 +10729944 +10736021 +10738708 +10746107 +10747937 +10747937-2回目 +10748031 +10749584 +10750852 +10751594 +10759413 +10759644 +10765874 +10768192 +10770199_ +10774104 +10778496 +10782907 +10783841 +10783896 +10784251 +10793477 +10794571 +10808896_ +10809596_ +10811942 +10812280_ +10813629_ +10818217_ +10818897 +10820979 +10826704 +10832123 +10833979 +10837607 +10844553_ +10847581 +10850305_ +10855603 +10858862 +10886029 +10897225 +10900592 +10901045 +10914380 +10914436 5-6回目 +10920190 +10930582 +10934858 +10935848 +10937121 +10937990_ +10943423_ +10951297 +10966743 +10974096 +10975884_ +10978522 +10980763 +10982323 +10985540 +10990953 +10992366_ +10994157_ +10995671 +10999745_ +11002576_ +11010643_ +11011718_ +11015866 +11017253_ +11021445 +11023454 +11028257 +11028505 +11031475_ +11034953 +11035930_ +11040910_ +11046295 +11046660 +11047025 +11051507 +11052584_ +11053806_ +11055264 +11055264(1-2) +11060404 +11066965_ +11067160 +11069935 (1-4) +11078021_ +11078542 +11079457 +11092793 +11093220 +11094698 +11106816 +11108740 +11111652_ +11115380 +11122193 +11152170 +11158006 +11162272 +11162937 +11163419 +11165327 +11170769 +11173801 +11175230 +11175953 +11179551 +11180988 +11186221 +11187703 +11195362 +11195593 +11199104 +11226895 +11228309 +20210715 +202011302 +患者ごとNo.2 +患者ごとNo.20 +患者ごとNo.49 +患者ごとNo.76 +患者ごとNo.80 +患者ごとNo.107 +患者ごとNo.148 +患者ごとNo.154 +患者ごとNo.176 +患者ごとNo.199 +患者ごとNo.200 +患者ごとNo.214 +患者ごとNo.215 +患者ごとNo.216 +患者ごとNo.219 +患者ごとNo.220 +患者ごとNo.313 +患者ごとNo.321 +患者ごとNo.366 +患者ごとNo.376 +患者ごとNo.388 +患者ごとNo.394 +患者ごとNo.415 +患者ごとNo.420 +患者ごとNo.422 +患者ごとNo.423 +患者ごとNo.424 +患者ごとNo.425 +患者ごとNo.458 +患者ごとNo.466 +患者ごとNo.467 +患者ごとNo.487