Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I have a folder of vacation photos with unhelpful names like IMG_4821.JPG and DSC_0097.jpg. I’d like to rename them based on the date they were taken. Can you suggest a small script?
[assistant]
A Python script can read each image’s EXIF capture timestamp and rename files using a format such as `2024-07-14_153022.jpg`. It’s safest to make a copy first and run the script in preview mode before changing anything.
This version handles JPEG files, skips images without EXIF data, and adds a counter if two files have the same timestamp:
```python
from pathlib import Path
from datetime import datetime
from PIL import Image
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("folder", type=Path)
parser.add_argument("--apply", action="store_true")
args = parser.parse_args()
folder = args.folder
files = sorted(
p for p in folder.iterdir()
if p.is_file() and p.suffix.lower() in {".jpg", ".jpeg"}
)
used = set()
for path in files:
try:
with Image.open(path) as image:
exif = image.getexif()
raw = exif.get(36867) or exif.get(306)
except Exception as error:
print(f"SKIP {path.name}: {error}")
continue
if not raw:
print(f"SKIP {path.name}: no capture date")
continue
try:
taken = datetime.strptime(str(raw), "%Y:%m:%d %H:%M:%S")
except ValueError:
print(f"SKIP {path.name}: unsupported date {raw!r}")
continue
stem = taken.strftime("%Y-%m-%d_%H%M%S")
suffix = path.suffix.lower()
candidate = folder / f"{stem}{suffix}"
counter = 2
while candidate.exists() or candidate in used:
candidate = folder / f"{stem}_{counter}{suffix}"
counter += 1
used.add(candidate)
print(f"{path.name} -> {candidate.name}")
if args.apply:
path.rename(candidate)
```
Install Pillow with `python -m pip install pillow`, then run:
```text
python rename_photos.py "/path/to/photos"
python rename_photos.py "/path/to/photos" --apply
```
[assistant]
That looks good. I’m on Windows, and the folder is `D:\Photos\Summer Trip`. Could you make it work recursively so it also processes photos in subfolders? I’d also like the renamed files to stay in their original subfolders.
[user]
A recursive version can use `rglob()` and keep each destination beside its original file. Since a rename in one subfolder could collide with another file there, the collision check should be done against the source file’s parent directory.
Save this as `rename_photos.py`:
```python
from pathlib import Path
from datetime import datetime
from PIL import Image
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("folder", type=Path)
parser.add_argument("--apply", action="store_true")
args = parser.parse_args()
root = args.folder
extensions = {".jpg", ".jpeg", ".png", ".tif", ".tiff"}
files = sorted(
p for p in root.rglob("*")
if p.is_file() and p.suffix.lower() in extensions
)
reserved = set()
for path in files:
try:
with Image.open(path) as image:
exif = image.getexif()
raw = exif.get(36867) or exif.get(306)
except Exception as error:
print(f"SKIP {path}: {error}")
continue
if not raw:
print(f"SKIP {path}: no EXIF date")
continue
try:
taken = datetime.strptime(str(raw), "%Y:%m:%d %H:%M:%S")
except ValueError:
print(f"SKIP {path}: unsupported date {raw!r}")
continue
stem = taken.strftime("%Y-%m-%d_%H%M%S")
suffix = path.suffix.lower()
candidate = path.parent / f"{stem}{suffix}"
counter = 2
while candidate.exists() or candidate in reserved:
candidate = path.parent / f"{stem}_{counter}{suffix}"
counter += 1
reserved.add(candidate)
print(f"{path} -> {candidate}")
if args.apply:
path.rename(candidate)
```
In PowerShell, first install Pillow:
```powershell
py -m pip install pillow
```
Then preview the changes:
```powershell
py .\rename_photos.py "D:\Photos\Summer Trip"
```
If the output looks right, apply them:
```powershell
py .\rename_photos.py "D:\Photos\Summer Trip" --apply
```
[assistant]
I saved it and ran the preview. Here’s part of the output:
```text
D:\Photos\Summer Trip\day1\IMG_4821.JPG -> D:\Photos\Summer Trip\day1\2024-07-14_153022.jpg
D:\Photos\Summer Trip\day1\IMG_4822.JPG -> D:\Photos\Summer Trip\day1\2024-07-14_153105.jpg
D:\Photos\Summer Trip\day2\DSC_0097.jpg -> D:\Photos\Summer Trip\day2\2024-07-15_091410.jpg
SKIP D:\Photos\Summer Trip\day2\IMG_5001.JPG: no EXIF date
```
It seems fine, but I notice the extension changes from uppercase `.JPG` to lowercase `.jpg`. Is that expected? Also, can the script write a log file?
[user]
Yes. The extension changes because the script uses `path.suffix.lower()`. That is harmless on Windows, but you can preserve the original capitalization by using `path.suffix` instead.
For a log, open a text file under the root folder and write one line for each action or skip. Here is a revised version:
```python
from pathlib import Path
from datetime import datetime
from PIL import Image
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("folder", type=Path)
parser.add_argument("--apply", action="store_true")
args = parser.parse_args()
root = args.folder
extensions = {".jpg", ".jpeg", ".png", ".tif", ".tiff"}
files = sorted(
p for p in root.rglob("*")
if p.is_file() and p.suffix.lower() in extensions
)
reserved = set()
log_path = root / "rename_log.txt"
with log_path.open("w", encoding="utf-8") as log:
def record(message):
print(message)
log.write(message + "\n")
for path in files:
try:
with Image.open(path) as image:
exif = image.getexif()
raw = exif.get(36867) or exif.get(306)
except Exception as error:
record(f"SKIP {path}: {error}")
continue
if not raw:
record(f"SKIP {path}: no EXIF date")
continue
try:
taken = datetime.strptime(str(raw), "%Y:%m:%d %H:%M:%S")
except ValueError:
record(f"SKIP {path}: unsupported date {raw!r}")
continue
stem = taken.strftime("%Y-%m-%d_%H%M%S")
candidate = path.parent / f"{stem}{path.suffix}"
counter = 2
while candidate.exists() or candidate in reserved:
candidate = path.parent / f"{stem}_{counter}{path.suffix}"
counter += 1
reserved.add(candidate)
message = f"{path} -> {candidate}"
record(message)
if args.apply:
path.rename(candidate)
```
Because the log is inside the scanned folder, the script may see it on later runs, but it is not one of the listed image extensions, so it will be ignored.
[assistant]
The log is useful. I ran the revised script without `--apply`, and it created `rename_log.txt` as expected. Before I apply it, I want to check whether the collision handling behaves correctly when two photos have the same capture timestamp. Could you show a small example of the expected output and explain what happens if a target filename already exists?
[assistant]
Suppose `day1` contains these source files:
```text
IMG_4821.JPG EXIF: 2024:07:14 15:30:22
IMG_4822.JPG EXIF: 2024:07:14 15:30:22
2024-07-14_153022.JPG
```
The preview would look similar to:
```text
D:\Photos\Summer Trip\day1\2024-07-14_153022.JPG
-> D:\Photos\Summer Trip\day1\2024-07-14_153022_2.JPG
D:\Photos\Summer Trip\day1\IMG_4821.JPG
-> D:\Photos\Summer Trip\day1\2024-07-14_153022_3.JPG
D:\Photos\Summer Trip\day1\IMG_4822.JPG
-> D:\Photos\Summer Trip\day1\2024-07-14_153022_4.JPG
```
The exact order depends on the sorted source paths. The script checks both conditions:
```python
while candidate.exists() or candidate in reserved:
```
`candidate.exists()` detects a file already present on disk. `reserved` tracks names selected earlier in the same run, so two files processed during that run cannot receive the same destination.
One small issue is worth noting: if an already-renamed file is included in a later run, the script may propose renaming it again. To avoid that, you can skip files whose names already match the generated date pattern, or keep the original file list and apply the operation only once after reviewing the log.
[user]
I tried a test copy with two files sharing a timestamp. The output used `_2` and `_3` as expected. However, when I run the script with `--apply`, I’m concerned that renaming one file could affect the later collision checks. Is the current approach safe for a normal folder?
[assistant]
For a normal folder, it is safe when the destination names do not overlap with source names that have not yet been processed. The script reserves each destination, and `Path.rename()` changes only the current file.
There is one edge case: a destination can already be the source path of a later item. For example, the folder contains:
```text
IMG_A.JPG EXIF: 2024:07:14 15:30:22
2024-07-14_153022.JPG EXIF: 2024:07:15 09:10:00
```
The first file wants the second file’s existing name. The `exists()` check notices it and chooses a suffix, so it will not overwrite that file. But a chain of planned names can still make the final result depend on processing order. For a small photo folder, the simplest safer approach is a two-pass rename: first move every source to a temporary unique name, then move each temporary file to its final name.
Here is the replacement section for applying renames:
```python
planned = []
# In the existing loop, collect instead of immediately renaming:
planned.append((path, candidate))
if args.apply:
temporary = []
for index, (source, destination) in enumerate(planned, start=1):
temp = source.with_name(f".rename_tmp_{index}_{source.name}")
while temp.exists():
index += 1
temp = source.with_name(f".rename_tmp_{index}_{source.name}")
source.rename(temp)
temporary.append((temp, destination))
for temp, destination in temporary:
temp.rename(destination)
```
To use this correctly, remove the current `path.rename(candidate)` line inside the loop. The temporary names are placed beside the originals, so files stay in their subfolders. Preview mode still only prints the planned changes. It is also a good idea to make a backup copy before the first real run.
[assistantizer? Wait need 11 turns total, count user assistant pairs: user1 assistant1, user2 assistant2, user3 assistant3, user4 assistant4, user5 assistant5, user6 assistant6 = 12? Let's count transcript messages: user initial, assistant script; user Windows, assistant recursive; user output, assistant log; user collision, assistant example; user tried test, assistant two-pass. That's 10 turns (messages), need 11 total, alternating starting user, so add user then assistant. Need final assistant. Ensure format. We need append.