My old genealogy program used to give all media a strange sequence of numbers as file names. I’d like to change that now.
Could I change the file name and thus the title of the image with @kku Supertool?
For people, the file name should consist of
“Last Name, First Name_Birth Date_001”
If there is already a photo with the number 001, it should be 002.
For photos stored with a family, the file name should be composed of the husband’s and wife’s names.
So,
“Husband-Last Name, First Name_Birth Date_Wife-Last Name, First Name_Birth Date_001”
I found a script that converts the file name into the title. But that’s not possible for me.
Before you change any names or move or create folders, run the Media Verify’sGenerate option. This will ensure that the checksum information agrees with the image and in the Gramps record.
Checksums may not agree if you ever manipulated/edited the original image after its Gramps record was created.
After you rename files and/or move them to new folders, MV’s Verify option will locate the renamed or moved files. Then you can use the Fix option to update the Gramps media record path field with the new information.
The Fix option only works on renamed or moved media files.
I will leave it to others to help with the update of the media record’s Title field.
The attached script might work. Run it in the People and Families categories. First select the people/families affected.
But there are gotchas, maybe they don’t affect you:
Media files can be shared between people (and families etc). So one image can correspond to several people. If the script is run for multiple people/families using the same media object then the file/media object is renamed multiple times and the last one remains.
There might be files with multiple image types (extensions) for the same person. For example, .jpg, .jpeg, .png. Then the same sequence number and title might be assigned to two different images. I assume you want to keep the original file extensions (but not include them in titles?). For example
title: “Last Name, First Name_Birth Date_001”, path: “Last Name, First Name_Birth Date_001.jpg”
title: “Last Name, First Name_Birth Date_001”, path: “Last Name, First Name_Birth Date_001.jpeg”
The dates are always in ISO format (yyyy-mm-dd). If a person does not have a name part or birth date then that part would be empty.
This script does not require using the Media Verify tool, but I guess it is still recommended. However, it seems the Media Verify tool works only with files under the “Base media path” folder.
[Gramps SuperTool script file]
version=1
[title]
fix-media-names
[description]
See https://gramps.discourse.group/t/change-media-file-name
[category]
People
[initial_statements]
from pathlib import Path
media_path = db.get_mediapath()
[statements]
seq = 1
for media in media_list:
origpath = Path(media.path)
if media_path:
origpath = media_path / origpath
parent = origpath.parent
suffix = origpath.suffix
while True:
if namespace == "Person":
newname = f"{surname}, {firstname}_{birth.date.obj}_{seq:03}"
elif namespace == "Family":
newname = f"{father.surname}, {father.firstname}_{father.birth.date.obj}"
newname += f"{mother.surname}, {mother.firstname}_{mother.birth.date.obj}_{seq:03}"
newfile = f"{newname}{suffix}"
newpath = parent / newfile
if not newpath.exists():
origpath.rename(newpath)
if media_path and newpath.is_relative_to(media_path):
newpath = newpath.relative_to(media_path)
media.obj.set_path(str(newpath))
media.obj.set_description(newname)
db.commit_media(media.obj, trans)
break
seq += 1
if seq > 999: break
[filter]
[expressions]
[scope]
selected
[unwind_lists]
False
[commit_changes]
False
[summary_only]
False
Thank you for your work.
Unfortunately, an error message is displayed.
Supertool says that the path of the name is not defined. For both the people and the families.
Would it perhaps be better to run the script in Gramps/Media?
There I can see the title and path of the image, and below I can display the reference to the image…
I tried to handle the absolute/relative paths properly: if the file path is under the base media path then the path is stored as relative path, otherwise absolute.