ユースケース

/

パーソナル・タスク・オートメーション

カスタムスクリプトとツールを作成して、ルーチンタスクを合理化し、効率を高めましょう

モデル

クロード 3.7 ソネット

Downloads フォルダーを 1 時間おきにスキャンし、画像ファイルの名前を自動的に変更して日付スタンプ付きのサブフォルダーに移動する、クロスプラットフォームの Python スクリプトを作成してください。

クエリを試す
クロード 3.7 ソネット
/
コーダー

自動画像ファイルオーガナイザー

これは、Downloadsフォルダーを1時間ごとにスキャンし、画像ファイルを識別し、作成日/変更日に基づいて日付スタンプ付きのサブフォルダーに整理するクロスプラットフォームのPythonスクリプトです。

1import os
2import shutil
3import time
4import datetime
5import schedule
6import platform
7import logging
8from pathlib import Path
9from PIL import Image
10from PIL.ExifTags import TAGS
11
12# Set up logging
13logging.basicConfig(
14    level=logging.INFO,
15    format='%(asctime)s - %(levelname)s - %(message)s',
16    handlers=[
17        logging.FileHandler("image_organizer.log"),
18        logging.StreamHandler()
19    ]
20)
21
22def get_downloads_folder():
23    """Get the downloads folder path based on the operating system"""
24    if platform.system() == "Windows":
25        return os.path.join(os.path.expanduser("~"), "Downloads")
26    elif platform.system() == "Darwin":  # macOS
27        return os.path.join(os.path.expanduser("~"), "Downloads")
28    else:  # Linux and other Unix-like
29        return os.path.join(os.path.expanduser("~"), "Downloads")
30
31def get_image_date(file_path):
32    """Extract date from image metadata or use file modification date"""
33    try:
34        # Try to get date from EXIF data
35        with Image.open(file_path) as img:
36            exif_data = img._getexif()
37            if exif_data:
38                for tag_id, value in exif_data.items():
39                    tag = TAGS.get(tag_id, tag_id)
40                    if tag == "DateTimeOriginal":
41                        # Parse the date string (format: YYYY:MM:DD HH:MM:SS)
42                        date_str = value.split()[0].replace(":", "-")
43                        return date_str
44    except (AttributeError, KeyError, IndexError, TypeError, OSError):
45        pass
46    
47    # Fall back to file modification time
48    mod_time = os.path.getmtime(file_path)
49    date_str = datetime.datetime.fromtimestamp(mod_time).strftime('%Y-%m-%d')
50    return date_str
51
52def is_image_file(file_path):
53    """Check if the file is an image based on extension"""
54    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.heic'}
55    return os.path.splitext(file_path.lower())[1] in image_extensions
56
57def organize_images():
58    """Scan downloads folder and organize images into date-based folders"""
59    downloads_folder = get_downloads_folder()
60    logging.info(f"Scanning {downloads_folder} for images...")
61    
62    # Create a base folder for organized images
63    organized_folder = os.path.join(downloads_folder, "Organized_Images")
64    if not os.path.exists(organized_folder):
65        os.makedirs(organized_folder)
66    
67    # Get all files in downloads folder
68    files = [f for f in os.listdir(downloads_folder) if os.path.isfile(os.path.join(downloads_folder, f))]
69    
70    # Counter for renamed files
71    moved_count = 0
72    
73    for file_name in files:
74        file_path = os.path.join(downloads_folder, file_name)
75        
76        # Skip the script itself and non-image files
77        if not is_image_file(file_path):
78            continue
79            
80        try:
81            # Get date from image
82            date_str = get_image_date(file_path)
83            
84            # Create date folder if it doesn't exist
85            date_folder = os.path.join(organized_folder, date_str)
86            if not os.path.exists(date_folder):
87                os.makedirs(date_folder)
88            
89            # Generate a unique name for the file
90            file_ext = os.path.splitext(file_name)[1]
91            new_file_name = f"{date_str}_{int(time.time())}_{moved_count}{file_ext}"
92            new_file_path = os.path.join(date_folder, new_file_name)
93            
94            # Move and rename the file
95            shutil.move(file_path, new_file_path)
96            logging.info(f"Moved: {file_name} -> {new_file_path}")
97            moved_count += 1
98            
99        except Exception as e:
100            logging.error(f"Error processing {file_name}: {str(e)}")
101    
102    logging.info(f"Organized {moved_count} images")
103
104def main():
105    """Main function to schedule and run the image organization task"""
106    logging.info("Image Organizer started")
107    
108    # Run once at startup
109    organize_images()
110    
111    # Schedule to run every hour
112    schedule.every(1).hour.do(organize_images)
113    
114    # Keep the script running
115    while True:
116        schedule.run_pending()
117        time.sleep(60)  # Check every minute
118
119if __name__ == "__main__":
120    main()

このスクリプトの使用方法

  1. 必要なパッケージをインストール:
1pip install pillow schedule
  1. スクリプトを実行する:
    • オペレーティングシステムに基づいてダウンロードフォルダを自動的に検出します
    • 画像はダウンロードフォルダ内の「Organized_Images」というフォルダに整理されます
    • 各画像は、作成日 (YYYY-MM-DD) の名前の付いたサブフォルダーに配置されます
    • 画像の名前は「日付_タイムスタンプ_カウンター.拡張子」というパターンに変更されます
  2. 特徴:
    • クロスプラットフォーム (ウィンドウズ、MacOS、Linuxで動作します)
    • 可能な場合は EXIF データを使用して画像の日付を決定します
    • EXIF データが使用できない場合はファイル変更日にフォールバックします
    • すべてのアクションをコンソールとログファイルの両方に記録します
    • 起動時に 1 回、その後は 1 時間おきに実行
    • エラーを正常に処理します
  3. スクリプトをバックグラウンドで実行するには:
    • Windows の場合:スケジュールされたタスクを作成する
    • macOS/Linux の場合:cron ジョブを使用するか、サービスを作成してください

スクリプトは停止するまで実行され続け、1時間ごとにダウンロードフォルダをチェックし、新しい画像ファイルを整理します。

忍者の AI アシスタントを体験

今すぐ無料でお試しください。プランは月額19ドルから。