Claude)Python)写真のEXIF情報を表示するWindowsアプリ(ExifTool)を作成。

2025-09-20 · 写真・カメラ <AI> コンピュータ

Pythonで「写真のEXIF情報を表示する」Windowsアプリを作成。「Claude」で作成。

【機能概略】
【Pythonプログラム】

作成されたプログラムを、「ExifTool.pyw」(名前は任意)という名前にして、デスクトップに置いておき、ダブルクリックで実行する。

JPEGファイルを画面にドラッグ&ドロップする。

[Ctrl]+[F]キーで文字列の検索が出来る。

【Pythonライブラリインストール】

次のライブラリが必要なので、インストールする。

pip install Pillow tkinterdnd2 exifread
【Pythonプログラムコード】
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk, ExifTags
import tkinterdnd2 as tkdnd
import os
import re
try:
    from exifread import process_file
    EXIFREAD_AVAILABLE = True
except ImportError:
    EXIFREAD_AVAILABLE = False

class ExifTool:
    def __init__(self):
        self.root = tkdnd.Tk()
        self.root.title("Exif Tool")
        self.root.geometry("600x800")
        self.root.configure(bg='white')
        
        # 検索関連の変数
        self.search_dialog = None
        self.search_index = 0
        self.search_results = []
        self.current_text = ""
        
        self.setup_ui()
        self.setup_bindings()
        self.root.drop_target_register(tkdnd.DND_FILES)
        self.root.dnd_bind('<<Drop>>', self.drop_file)
        
    def setup_ui(self):
        # メインフレーム
        main_frame = tk.Frame(self.root, bg='white')
        main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 画像表示エリア(ドラッグ&ドロップエリア)
        self.image_frame = tk.Frame(main_frame, width=320, height=240, 
                                   bg='lightgray', relief=tk.SUNKEN, bd=2)
        self.image_frame.pack(pady=(0, 10))
        self.image_frame.pack_propagate(False)
        
        # ドラッグ&ドロップのヒント
        self.hint_label = tk.Label(self.image_frame, 
                                  text="JPEGファイルをここにドラッグ&ドロップ",
                                  bg='lightgray', fg='gray')
        self.hint_label.pack(expand=True)
        
        # 画像表示用ラベル
        self.image_label = tk.Label(self.image_frame, bg='lightgray')
        
        # ファイル名表示
        self.filename_label = tk.Label(main_frame, text="", 
                                     font=("Arial", 10, "bold"), bg='white')
        self.filename_label.pack(anchor=tk.W, pady=(0, 5))
        
        # EXIF情報表示エリア用のフレーム
        text_frame = tk.Frame(main_frame, bg='white')
        text_frame.pack(fill=tk.BOTH, expand=True)
        
        # EXIF情報表示エリア(スクロールバー付き)
        self.exif_text = tk.Text(text_frame, height=30, width=70,
                                font=("Consolas", 9), bg='white', 
                                relief=tk.FLAT, bd=0, wrap=tk.WORD)
        
        # 垂直スクロールバー
        scrollbar = tk.Scrollbar(text_frame, orient="vertical", command=self.exif_text.yview)
        self.exif_text.configure(yscrollcommand=scrollbar.set)
        
        # テキストエリアとスクロールバーを配置
        self.exif_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        
    def setup_bindings(self):
        """キーバインドの設定"""
        self.root.bind('<Control-f>', self.show_search_dialog)
        self.root.bind('<Control-F>', self.show_search_dialog)
        
    def show_search_dialog(self, event=None):
        """検索ダイアログを表示"""
        if self.search_dialog is not None and self.search_dialog.winfo_exists():
            self.search_dialog.lift()
            self.search_dialog.focus()
            return
            
        # 検索ダイアログを作成
        self.search_dialog = tk.Toplevel(self.root)
        self.search_dialog.title("検索")
        self.search_dialog.geometry("350x120")
        self.search_dialog.resizable(False, False)
        self.search_dialog.transient(self.root)
        
        # ダイアログの中央配置
        self.search_dialog.geometry("+%d+%d" % (
            self.root.winfo_rootx() + 125,
            self.root.winfo_rooty() + 100))
        
        # 検索フレーム
        search_frame = tk.Frame(self.search_dialog)
        search_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 検索入力
        tk.Label(search_frame, text="検索文字列:").pack(anchor=tk.W)
        self.search_entry = tk.Entry(search_frame, width=40)
        self.search_entry.pack(fill=tk.X, pady=(2, 5))
        self.search_entry.focus()
        
        # ボタンフレーム
        button_frame = tk.Frame(search_frame)
        button_frame.pack(fill=tk.X, pady=5)
        
        tk.Button(button_frame, text="検索", command=self.search_text, width=8).pack(side=tk.LEFT, padx=(0, 5))
        tk.Button(button_frame, text="次を検索", command=self.search_next, width=10).pack(side=tk.LEFT, padx=(0, 5))
        tk.Button(button_frame, text="前を検索", command=self.search_prev, width=10).pack(side=tk.LEFT, padx=(0, 5))
        tk.Button(button_frame, text="閉じる", command=self.close_search_dialog, width=8).pack(side=tk.RIGHT)
        
        # キーバインド
        self.search_entry.bind('<Return>', lambda e: self.search_text())
        self.search_entry.bind('<F3>', lambda e: self.search_next())
        self.search_entry.bind('<Shift-F3>', lambda e: self.search_prev())
        self.search_dialog.bind('<Escape>', lambda e: self.close_search_dialog())
        
    def search_text(self):
        """テキストを検索"""
        search_term = self.search_entry.get().strip()
        if not search_term:
            return
            
        # 現在のテキストを取得
        self.current_text = self.exif_text.get(1.0, tk.END)
        if not self.current_text.strip():
            return
        
        # 既存のハイライトをクリア
        self.exif_text.tag_remove("search_highlight", 1.0, tk.END)
        
        # 検索結果を取得(大文字小文字を区別しない)
        self.search_results = []
        pattern = re.compile(re.escape(search_term), re.IGNORECASE)
        
        for match in pattern.finditer(self.current_text):
            start_pos = f"1.0+{match.start()}c"
            end_pos = f"1.0+{match.end()}c"
            self.search_results.append((start_pos, end_pos))
        
        if self.search_results:
            # 検索結果をハイライト
            for start_pos, end_pos in self.search_results:
                self.exif_text.tag_add("search_highlight", start_pos, end_pos)
            
            # ハイライトの色設定
            self.exif_text.tag_configure("search_highlight", background="yellow", foreground="black")
            
            # 最初の結果にジャンプ
            self.search_index = 0
            self.highlight_current_result()
            
            # 検索結果の数を表示
            self.search_dialog.title(f"検索 - {len(self.search_results)}件見つかりました")
        else:
            self.search_dialog.title("検索 - 見つかりませんでした")
            
    def search_next(self):
        """次の検索結果に移動"""
        if not self.search_results:
            return
            
        self.search_index = (self.search_index + 1) % len(self.search_results)
        self.highlight_current_result()
        
    def search_prev(self):
        """前の検索結果に移動"""
        if not self.search_results:
            return
            
        self.search_index = (self.search_index - 1) % len(self.search_results)
        self.highlight_current_result()
        
    def highlight_current_result(self):
        """現在の検索結果をハイライト"""
        if not self.search_results:
            return
            
        # 既存の現在位置ハイライトをクリア
        self.exif_text.tag_remove("current_search", 1.0, tk.END)
        
        # 現在の結果をハイライト
        start_pos, end_pos = self.search_results[self.search_index]
        self.exif_text.tag_add("current_search", start_pos, end_pos)
        self.exif_text.tag_configure("current_search", background="orange", foreground="black")
        
        # 該当箇所にスクロール
        self.exif_text.see(start_pos)
        
        # ダイアログタイトルを更新
        self.search_dialog.title(f"検索 - {self.search_index + 1}/{len(self.search_results)}")
        
    def close_search_dialog(self):
        """検索ダイアログを閉じる"""
        if self.search_dialog and self.search_dialog.winfo_exists():
            # ハイライトをクリア
            self.exif_text.tag_remove("search_highlight", 1.0, tk.END)
            self.exif_text.tag_remove("current_search", 1.0, tk.END)
            
            self.search_dialog.destroy()
            self.search_dialog = None
            self.search_results = []
            self.search_index = 0
            
    def drop_file(self, event):
        files = self.root.tk.splitlist(event.data)
        if files:
            file_path = files[0]
            if file_path.lower().endswith(('.jpg', '.jpeg')):
                self.load_image(file_path)
                
    def load_image(self, file_path):
        try:
            # 画像を開く
            image = Image.open(file_path)
            
            # 画像を300px幅にリサイズ
            original_width, original_height = image.size
            new_width = 300
            new_height = int((original_height * new_width) / original_width)
            resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
            
            # tkinter用に変換
            photo = ImageTk.PhotoImage(resized_image)
            
            # ヒントラベルを隠して画像を表示
            self.hint_label.pack_forget()
            self.image_label.configure(image=photo)
            self.image_label.image = photo  # 参照を保持
            self.image_label.pack(expand=True)
            
            # フレームサイズを画像に合わせて調整
            self.image_frame.configure(width=new_width + 20, height=new_height + 20)
            
            # ファイル名を表示
            filename = os.path.basename(file_path)
            self.filename_label.configure(text=f"ファイル名: {filename}")
            
            # EXIF情報を表示
            self.display_exif_info(image, file_path)
            
        except Exception as e:
            self.exif_text.delete(1.0, tk.END)
            self.exif_text.insert(tk.END, f"エラー: {str(e)}")
            
    def display_exif_info(self, image, file_path):
        self.exif_text.delete(1.0, tk.END)
        
        try:
            # ファイル情報
            file_stat = os.stat(file_path)
            file_size = file_stat.st_size
            
            info_text = f"=== ファイル情報 ===\n"
            info_text += f"ファイルパス: {file_path}\n"
            info_text += f"ファイルサイズ: {file_size:,} bytes\n"
            info_text += f"画像サイズ: {image.size[0]} x {image.size[1]} pixels\n"
            info_text += f"画像モード: {image.mode}\n"
            info_text += f"画像フォーマット: {image.format}\n\n"
            
            # ExifReadを使用してより詳細な情報を取得
            if EXIFREAD_AVAILABLE:
                info_text += self.get_detailed_exif_with_exifread(file_path)
            else:
                info_text += "より詳細なExif情報を表示するには 'pip install exifread' を実行してください。\n\n"
                info_text += self.get_basic_exif_info(image)
                
        except Exception as e:
            info_text = f"EXIF情報の読み取り中にエラーが発生しました: {str(e)}"
            
        self.exif_text.insert(tk.END, info_text)
        
    def get_detailed_exif_with_exifread(self, file_path):
        """ExifReadを使用してメーカー独自情報を含む詳細なExif情報を取得"""
        info_text = ""
        
        try:
            with open(file_path, 'rb') as f:
                tags = process_file(f, details=True, debug=False)
                
            if tags:
                info_text += "=== 詳細EXIF情報(メーカー独自情報含む) ===\n"
                
                # カテゴリ別に分類
                categories = {
                    'Image': [],
                    'EXIF': [],
                    'GPS': [],
                    'Interoperability': [],
                    'Thumbnail': [],
                    'Canon': [],
                    'Nikon': [],
                    'Sony': [],
                    'Panasonic': [],
                    'Olympus': [],
                    'Fujifilm': [],
                    'Others': []
                }
                
                # タグを分類
                for tag, value in tags.items():
                    tag_str = str(tag)
                    if tag_str.startswith('Image '):
                        categories['Image'].append((tag, value))
                    elif tag_str.startswith('EXIF '):
                        categories['EXIF'].append((tag, value))
                    elif tag_str.startswith('GPS '):
                        categories['GPS'].append((tag, value))
                    elif tag_str.startswith('Interoperability '):
                        categories['Interoperability'].append((tag, value))
                    elif tag_str.startswith('Thumbnail '):
                        categories['Thumbnail'].append((tag, value))
                    elif 'Canon' in tag_str or 'MakerNote Canon' in tag_str:
                        categories['Canon'].append((tag, value))
                    elif 'Nikon' in tag_str or 'MakerNote Nikon' in tag_str:
                        categories['Nikon'].append((tag, value))
                    elif 'Sony' in tag_str or 'MakerNote Sony' in tag_str:
                        categories['Sony'].append((tag, value))
                    elif 'Panasonic' in tag_str or 'MakerNote Panasonic' in tag_str:
                        categories['Panasonic'].append((tag, value))
                    elif 'Olympus' in tag_str or 'MakerNote Olympus' in tag_str:
                        categories['Olympus'].append((tag, value))
                    elif 'Fujifilm' in tag_str or 'MakerNote Fujifilm' in tag_str:
                        categories['Fujifilm'].append((tag, value))
                    else:
                        categories['Others'].append((tag, value))
                
                # カテゴリ別に表示
                for category, items in categories.items():
                    if items:
                        info_text += f"\n--- {category} ---\n"
                        items.sort(key=lambda x: str(x[0]))
                        
                        for tag, value in items:
                            value_str = str(value)
                            # 長すぎる値は切り詰め
                            if len(value_str) > 200:
                                value_str = value_str[:197] + "..."
                            # バイナリデータの場合
                            if hasattr(value, 'values') and len(str(value.values)) > 50:
                                value_str = f"<binary data: {len(str(value.values))} chars>"
                            
                            info_text += f"{str(tag).ljust(35)}: {value_str}\n"
                            
            else:
                info_text += "詳細EXIF情報が見つかりません。\n"
                
        except Exception as e:
            info_text += f"詳細EXIF情報の読み取りエラー: {str(e)}\n"
            
        return info_text
        
    def get_basic_exif_info(self, image):
        """Pillowを使用した基本的なExif情報取得(フォールバック)"""
        info_text = ""
        
        try:
            # EXIF情報を取得
            exifdata = image.getexif()
            
            if exifdata:
                info_text += "=== 基本EXIF情報 ===\n"
                
                # 全てのEXIFタグを表示(未知のタグも含む)
                all_tags = {}
                
                # 標準的なEXIFタグ
                for tag_id, value in exifdata.items():
                    tag_name = ExifTags.TAGS.get(tag_id, f"Unknown_{tag_id:04X}")
                    all_tags[tag_name] = value
                
                # IFDデータも取得
                try:
                    # Exif IFD
                    exif_ifd = exifdata.get_ifd(0x8769)
                    if exif_ifd:
                        for tag_id, value in exif_ifd.items():
                            tag_name = ExifTags.TAGS.get(tag_id, f"Exif_Unknown_{tag_id:04X}")
                            all_tags[f"Exif {tag_name}"] = value
                    
                    # GPS IFD
                    gps_ifd = exifdata.get_ifd(0x8825)
                    if gps_ifd:
                        for tag_id, value in gps_ifd.items():
                            tag_name = ExifTags.GPSTAGS.get(tag_id, f"GPS_Unknown_{tag_id:04X}")
                            all_tags[f"GPS {tag_name}"] = value
                            
                    # Interoperability IFD
                    try:
                        interop_ifd = exif_ifd.get_ifd(0xA005) if exif_ifd else None
                        if interop_ifd:
                            for tag_id, value in interop_ifd.items():
                                tag_name = ExifTags.TAGS.get(tag_id, f"Interop_Unknown_{tag_id:04X}")
                                all_tags[f"Interop {tag_name}"] = value
                    except:
                        pass
                        
                except AttributeError:
                    # get_ifdメソッドが利用できない場合
                    pass
                
                # ソートして表示
                sorted_tags = sorted(all_tags.items(), key=lambda x: x[0])
                
                for tag_name, value in sorted_tags:
                    # 値の型によって表示方法を調整
                    if isinstance(value, bytes):
                        try:
                            value_str = value.decode('utf-8', errors='ignore')
                            if len(value_str) > 100:
                                value_str = value_str[:97] + "..."
                        except:
                            value_str = f"<bytes: {len(value)} bytes>"
                    elif isinstance(value, (tuple, list)):
                        if len(value) > 10:
                            value_str = f"<{type(value).__name__}: {len(value)} items>"
                        else:
                            value_str = str(value)
                    else:
                        value_str = str(value)
                    
                    # 長すぎる値は切り詰める
                    if len(value_str) > 150:
                        value_str = value_str[:147] + "..."
                    
                    info_text += f"{tag_name.ljust(35)}: {value_str}\n"
                        
            else:
                info_text += "EXIF情報が見つかりません。\n"
                
        except Exception as e:
            info_text += f"基本EXIF情報の読み取りエラー: {str(e)}\n"
            
        return info_text
        
    def run(self):
        self.root.mainloop()

if __name__ == "__main__":
    app = ExifTool()
    app.run()

Claude)Python)「写真をEXIF機種毎のフォルダーに分類」Windowsアプリ作成。Pythonで「写真をEXIF機種毎のフォルダーに分類」Windowsアプリを作成。 生成AI「 Claude 」「 Gemini 」「 C…晴歩雨描 ·

ソニーデジカメ)コントラストやシャープネスなどの設定値は、EXIFの標準項目には書かれず、メーカー独自の項目に書かれる。「SONY VLOGCAM ZV-1 II」では、「クリエイティブルック」で、コントラストやシャープネスなどを微調整できることになっているが…晴歩雨描 ·

ソニーのデジカメ写真のEXIFから、「クリエイティブルック」情報を抜き出して、画像ファイル名に「クリエイティブルック名」を付け足す。(ExifTool/JScript)以前、「 ソニー のデジカメ写真の EXIF から、クリエイティブスタイル、ピクチャーエフェクト情報を抜き出して、画像ファイル名に EXIF…晴歩雨描 ·

PowerShot SX70 HSの35mm判換算焦点距離をExifToolで調べる。先日、キヤノン PowerShot SX70 HS を購入した。 SX70 HS では、撮影した写真の35mm判換算焦点距離が知りたくても、…晴歩雨描 ·

パナソニックのデジカメ写真のEXIFから、フォトスタイル、クリエイティブコントロール情報を抜き出して、画像ファイル名にEXIF情報を付け足す。(ExifTool/JScript)先日、 ソニー のデジカメ写真の EXIF から、クリエイティブスタイル、ピクチャーエフェクト情報を抜き出して、画像ファイル名に EXIF …晴歩雨描 ·

ソニーのデジカメ写真のEXIFから、クリエイティブスタイル、ピクチャーエフェクト情報を抜き出して、画像ファイル名にEXIF情報を付け足す。(ExifTool/JScript)先日、 価格.com 掲示 板で、DSC-RX100M6でクリエイティブスタイルやピクチャーエフェクトを設定して撮影した写真の EXIF 情…晴歩雨描 ·