Skip to content

Instantly share code, notes, and snippets.

@JMalysiak
Last active March 8, 2024 09:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JMalysiak/fe81777e83a50426c9ec54e17290684e to your computer and use it in GitHub Desktop.
Save JMalysiak/fe81777e83a50426c9ec54e17290684e to your computer and use it in GitHub Desktop.
Import Picasa face tags into Digikam database
# This is a script to import face tags from Picasa into Digikam's SQLite database.
# First you need to export Picasa database into XML using this tool:
# https://sourceforge.net/projects/exportpicasa/
# Later you need to adjust this script to fit your needs and then just run it.
import xml.etree.ElementTree as ET
import sqlite3
# Adjust the values below:
XML_FILE_PATH = '/path/to/file.xml'
SQLITE_DB_PATH = '/path/to/sqlite.db'
# In order to find out what is the parent tag ID in your database, just tag some person in Digikam, open the database using any SQLite client and see what is the pid value in Tags table for the tag you've just created.
PARENT_TAG_ID = 30
tree = ET.parse(XML_FILE_PATH)
root = tree.getroot()
conn = sqlite3.connect(SQLITE_DB_PATH)
cur = conn.cursor()
for folder in root:
folderName = folder.get('name')
cur.execute("SELECT a.id FROM AlbumRoots r INNER JOIN Albums a ON r.id = a.albumRoot WHERE r.specificPath || a.relativePath = :folderName", {'folderName': folderName})
row = cur.fetchone()
if not row:
continue
albumId = row[0]
for file in folder:
fileName = file.get('name')
cur.execute("SELECT i.id FROM Images i WHERE i.name = :name AND i.album = :albumId", {'name': fileName, 'albumId': albumId})
row = cur.fetchone()
imageId = row[0]
for face in file:
personName = face.get('contact_name')
rectLeft = float(face.get('rect_left'))
rectRight = float(face.get('rect_right'))
rectTop = float(face.get('rect_top'))
rectBottom = float(face.get('rect_bottom'))
if not personName:
continue
cur.execute("SELECT i.width, i.height FROM ImageInformation i WHERE i.imageId = :imageId", {'imageId': imageId})
row = cur.fetchone()
imageWidth = row[0]
imageHeight = row[1]
x = int(imageWidth * rectLeft)
y = int(imageHeight * rectTop)
width = int(imageWidth * (rectRight - rectLeft))
height = int(imageWidth * (rectBottom - rectTop))
rectValue = '<rect x="' + str(x) + '" y="' + str(y) + '" width="' + str(width) + '" height="' + str(height) + '"/>'
cur.execute("SELECT t.id FROM Tags t WHERE t.name = :name AND t.pid = :tagId", {'name': personName, 'tagId': PARENT_TAG_ID})
row = cur.fetchone()
if row:
tagId = row[0]
else:
cur.execute("INSERT INTO Tags (pid, name, icon) VALUES (:tagId, :name, 0)", {'tagId': PARENT_TAG_ID, 'name': personName})
tagId = cur.lastrowid
cur.execute("INSERT INTO TagProperties (tagid, property, value) VALUES (:tagId, 'person', :value)", {'tagId': tagId, 'value': personName})
cur.execute("INSERT INTO TagProperties (tagid, property, value) VALUES (:tagId, 'kfaceId', :value)", {'tagId': tagId, 'value': personName})
try:
cur.execute("INSERT INTO ImageTags (imageid, tagid) VALUES (:imageId, :tagId)", {'imageId': imageId, 'tagId': tagId})
except sqlite3.IntegrityError as ex:
print 'WARNING: ' + str(ex) + '. Image: ' + folderName + '/' + fileName + ', person: ' + personName
cur.execute("INSERT INTO ImageTagProperties (imageid, tagid, property, value) VALUES (:imageId, :tagId, 'tagRegion', :rectValue)", {'imageId': imageId, 'tagId': tagId, 'rectValue': rectValue})
conn.commit()
conn.close()
@Philipp91
Copy link

Here is a solution that reads straight from Picasa's hidden .picasa.ini files in each directory, so no need to parse the PMP database or use the XML exporter.

@Lukmhee
Copy link

Lukmhee commented Apr 6, 2022

Digicam Album date, Migration from Picasa3 to Digicam

I need some help with a problem concerning Digicam Album sort by date order.
I have more than 800 GB of photo (images) organise in +1100 album (dossier/folder).
Unfortunately, in most of the album there is some photo/image/files with a wrong date.
Because of that when I sort the Album by Date the desired order is not produced. Due to the huge number of Album/folders correcting the date of each album one by one will be a nightmare.
To save this problem I see only 2 solutions:

1 Is there a way to change the way initially Digicam date each album to make the most recent image date instead of the older/or average as he does now when building the database.

2 Before this photo/image was managed with Picasa 3 and all the Album (Folders/Dossiers in Picasa 3) was correctly Date.
I need a comprehensive tutorial to copy all the (files/dossier) dates/propriety from the Picasa 3 databases to the Digicam Album date/propriety database.

Thanks for your help.

@Philipp91
Copy link

Philipp91 commented Apr 6, 2022

So why wasn't this a problem in Picasa? Does Picasa use a different date (e.g. file creation vs. file modification vs. photography date)? Or did you manually reorder the photos in Picasa (and it stores the order in its internal database)?

In other words, my question is: Where does the order "come from" that you want?

@Lukmhee
Copy link

Lukmhee commented Apr 6, 2022

Hi,
Thanks to take interest in my problem...
I just realise that I need to check about folder date and picasa folder dating process....
right now I don’t have access to my mum computer so it will take me a few days before I can come back to you with a better explanation of my problem.
Best regards,

@Lukmhee
Copy link

Lukmhee commented Apr 8, 2022

Hi,
I change a folder(dossier) date in Picasa and check with Window Files explorer the different Dates associate with this folder (date created, date modified ........) Unfortunately, no change, that mean Picasa keep the folder dating in his own database like Digikam (The dating process looks similar in both software : during the importation (building their database) process they date the folder "picasa"/Album "Digikam" using the older images/files.
So what I need its something like you did with the face tag but this time for the folder date. Something to extract the folder date from picasa database and write it in the Digikam database with some step by step explanation as i have limited computer knowledge.
It will be a challenge for me... but compared to manually modifier 1150 folder/album date in digikam so the date/presentation order match the picasa presentation that my mum use to and love....
Again thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment