基于Python从大量JPG图片中提取iPhone拍摄照片的评论信息
Hey there! I see you're trying to pull those Finder comments from your iPhone photos, and ran into the issue where PIL's EXIF extraction doesn't pick them up—let me break this down for you.
The problem here is that the comments you see in Mac's "Get Info" window aren't stored in the standard EXIF metadata of the image file. Instead, they're saved as Mac-specific extended file attributes (system-level tags attached to the file, not part of the image data itself). That's why your PIL code isn't seeing them.
Here are two reliable ways to extract these comments using Python, tailored for your MacBook setup:
Method 1: Using the xattr Library
This library lets you directly access extended attributes on macOS. First, install it via pip:
pip install xattr
Then use this code to extract comments from all JPGs in your folder and store them in a DataFrame:
import xattr import os import pandas as pd import plistlib # Path to your folder of JPGs photo_folder = "/path/to/your/photos/folder" # List to store photo details and comments photo_comments = [] for filename in os.listdir(photo_folder): if filename.lower().endswith(".jpg"): file_path = os.path.join(photo_folder, filename) try: # Fetch the Finder comment attribute (stored as a binary plist) comment_data = xattr.getxattr(file_path, "com.apple.metadata:kMDItemFinderComment") # Parse the binary plist to get the actual comment text comment = plistlib.loads(comment_data)[0] if comment_data else None except (xattr.XAttrError, IndexError): # Handle cases where no comment exists comment = None photo_comments.append({ "filename": filename, "file_path": file_path, "comment": comment }) # Convert to a pandas DataFrame for easy manipulation comments_df = pd.DataFrame(photo_comments) print(comments_df)
Method 2: Using AppleScript (No Extra Installs)
Since macOS comes with AppleScript built-in, we can use subprocess to call an AppleScript command that fetches the Finder comment. This is perfect if you want to avoid installing additional libraries:
import subprocess import os import pandas as pd photo_folder = "/path/to/your/photos/folder" photo_comments = [] for filename in os.listdir(photo_folder): if filename.lower().endswith(".jpg"): file_path = os.path.join(photo_folder, filename) # AppleScript to retrieve the comment from Finder apple_script = f''' tell application "Finder" set targetFile to POSIX file "{file_path}" as alias get comment of targetFile end tell ''' try: # Run the script and capture the output result = subprocess.run( ["osascript", "-e", apple_script], capture_output=True, text=True, check=True ) # Clean up the output (remove extra newlines/whitespace) comment = result.stdout.strip() if result.stdout else None except subprocess.CalledProcessError: comment = None photo_comments.append({ "filename": filename, "file_path": file_path, "comment": comment }) # Convert to a DataFrame comments_df = pd.DataFrame(photo_comments) print(comments_df)
Quick Tips
- Both methods handle photos without comments by setting the
commentvalue toNone, so your DataFrame remains consistent. - The
xattrmethod is faster for large batches (1000+ photos), while the AppleScript method relies on Finder and may be slightly slower but requires zero setup. - Always use absolute file paths (like
/Users/YourName/Photos/WildWork) to avoid path-related errors.
内容的提问来源于stack exchange,提问作者Hedera-helix

