From ba69ea154f16a7e9898afcd0f376ec22ff2b459c Mon Sep 17 00:00:00 2001 From: flll <2844835+flll@users.noreply.github.com> Date: Thu, 9 Jan 2025 17:19:26 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=BB=E5=83=8F=E5=87=A6=E7=90=86=E3=81=AE?= =?UTF-8?q?=E4=B8=A6=E5=88=97=E5=8C=96=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97?= =?UTF-8?q?=E3=80=81`main.py`=E3=82=92=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=80=82`process=5Fimage`=E9=96=A2=E6=95=B0=E3=82=92?= =?UTF-8?q?=E7=B0=A1=E7=B4=A0=E5=8C=96=E3=81=97=E3=80=81`process=5Fimages`?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=80=81=E6=9C=80=E5=88=9D=E3=81=AE=E7=94=BB=E5=83=8F=E3=82=92?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=97=E3=81=9F=E5=BE=8C=E3=81=AB=E6=AE=8B?= =?UTF-8?q?=E3=82=8A=E3=81=AE=E7=94=BB=E5=83=8F=E3=82=92=E3=82=B9=E3=83=AC?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=83=97=E3=83=BC=E3=83=AB=E3=81=A7=E4=B8=A6?= =?UTF-8?q?=E5=88=97=E5=87=A6=E7=90=86=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=80=82=E3=81=BE=E3=81=9F=E3=80=81=E6=9C=80=E5=A4=A7=E3=83=AF?= =?UTF-8?q?=E3=83=BC=E3=82=AB=E3=83=BC=E6=95=B0=E3=82=92=E5=AE=9A=E7=BE=A9?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=AE=9A=E6=95=B0`MAX=5FWORKERS`=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 39 ++++++++++++++++++++++++++++++++------- rename.sh | 17 +++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100755 rename.sh diff --git a/main.py b/main.py index 1ec35ca..60cad1f 100644 --- a/main.py +++ b/main.py @@ -11,11 +11,13 @@ from resize import resize_image import configparser import sys +import concurrent.futures # 処理対象のディレクトリを指定 source_dir = "./images" target_dir = "./batched" batches_enabled = True +MAX_WORKERS = 10 config = configparser.ConfigParser() @@ -60,9 +62,7 @@ def create_message_params(system_prompt, base64_image): ] } -def process_image(image_path, is_first=False): - if is_first: - time.sleep(5) +def process_image(image_path): custom_id = os.path.basename(image_path).replace('.', '_').replace('/', '_') with Image.open(image_path) as img: @@ -92,6 +92,34 @@ def process_image(image_path, is_first=False): message = client.beta.prompt_caching.messages.create(**message_params) print(message.content) +def process_images(jpg_files): + if not jpg_files: + return + + # 最初の1件を処理 + first_file = jpg_files[0] + print(f"最初の画像を処理中: {first_file}") + process_image(first_file) + time.sleep(5) + + # 残りの画像を並列処理 + remaining_files = jpg_files[1:] + if not remaining_files: + return + + print("残りの画像を並列処理開始") + with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: + futures = [] + for jpg_file in remaining_files: + future = executor.submit(process_image, jpg_file) + futures.append(future) + + for future in concurrent.futures.as_completed(futures): + try: + future.result() + except Exception as e: + print(f"エラーが発生しました: {str(e)}") + if len(sys.argv) > 1: jpg_file = os.path.join(source_dir, sys.argv[1]) if os.path.exists(jpg_file): @@ -102,7 +130,4 @@ def process_image(image_path, is_first=False): print(f"エラー: ファイル {jpg_file} が見つかりません") else: jpg_files = glob.glob(os.path.join(source_dir, "*.jpg")) - for i, jpg_file in enumerate(jpg_files): - print(f"Processing {jpg_file}...") - process_image(jpg_file, is_first=(i==1)) - print("-" * 80) + process_images(jpg_files) diff --git a/rename.sh b/rename.sh new file mode 100755 index 0000000..0aeed96 --- /dev/null +++ b/rename.sh @@ -0,0 +1,17 @@ +#!/bin/sh -eu + +echo "imagesディレクトリ内のjpgファイルを最適なファイル名にリネームします" +echo "Enterで開始します" +read dummy +cd images +for file in *.jpg; do + if [ ! "$(echo "$file" | grep -E "^[0-9]{8}(_[0-9]{6}[a-z]{2})?\.jpg$")" ]; then + random_suffix=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 2 | head -n 1) + new_name=$(date -r "$file" +%Y%m%d_%H%M%S${random_suffix}.jpg) + + echo "$file" "$new_name" + mv "$file" "$new_name" + fi +done + +echo "完了しました" \ No newline at end of file