Adding photos and events in the Shotwell DB through python script

I have my pictures organized by events already on my computer. When I import the pictures, events get split up in day-events, which are often shorter than the real events. I don’t want to group 10 years of pictures by hand and also I like programming. The DB is simply SQLite, so it should be possible to modify it from the outside, to import pics through python, right?

I read a bit in the shotwell wiki & wrote a python script to

  • create events in the EventTable for each parent directory of the pictures in the PhotoTable
  • set the event_id of each picture to the right new event.
    When I open Shotwell it works, I have my pics organized in events! However, if I import new pictures through shotwell & close Shotwell and want to open it again, it won’t open anymore. Without adding new pictures I can close and re-open.

Here’s the code

db_path = 'sqlite:////home/mfrick/.local/share/shotwell/data/photo.db'
dbEngine=sqlalchemy.create_engine(db_path)
df = pd.read_sql('select * from PhotoTable', dbEngine)
df['Event'] = df['filename'].apply(lambda p: pl.Path(p).parent.name)
events = df['Event'].unique()
for i_event, dir_name in enumerate(events):
    df.loc[df['Event'] == dir_name, 'event_id'] = int(i_event + 1)
df = df.drop(columns='Event')
df.to_sql(name ='PhotoTable', con=dbEngine, index=False, if_exists='replace',
          dtype=dtypes_photo)

df = pd.read_sql('select * from EventTable', dbEngine)
df = df[:0]  # Only keep the columns structure
for i_event, event_name in enumerate(events):
    new_line = np.array([np.nan]*len(df.columns)).reshape((1,-1))
    new_line = pd.DataFrame(new_line, columns=df.columns)
    new_line['time_created'] = int(time.time())
    new_line['id'] = int(i_event + 1)
    new_line['name'] = event_name
    df = pd.concat((df, new_line))
df.to_sql(name ='EventTable', con= dbEngine, index=False, if_exists='replace',
          dtype=dtypes_event)

I haven’t found out how to set the default values of the columns yet, but I tried to set them manually in a GUI based DB Browser after running my script & before opening shotwell & it didn’t help.

Any ideas?

There should be either a hearty complaining dialog popping up or something in ~/.cache/shotwell/shotwell.log

Thanks for the path to the log.

When I first open shotwell, after adding pics and reopening shotwell, after running my script and reopening shotwell, after closing it again and reopening shotwell I each time get a log looking like this:

L 4424 2022-10-29 16:01:52 [MSG] main.vala:423: Shotwell Photo Manager 0.30.10
L 4424 2022-10-29 16:01:52 [WRN] Resources.vala:63: Couldn’t load icon set from /org/gnome/Shotwell/Publishing/flickr.png: The resource at “/org/gnome/Shotwell/Publishing/flickr.png” does not exist
L 4424 2022-10-29 16:01:52 [MSG] main.vala:43: Verifying database…
L 4424 2022-10-29 16:01:53 [MSG] VideoSupport.vala:401: interpreter state has changed; video thumbnails may be out of date
L 4424 2022-10-29 16:01:54 [WRN] DirectoryMonitor.vala:922: Skipping hidden file/directory […]

After adding more pics through shotwell and reopening I get the same log, but it stays stuck at the “Verifying database…” line.

After a while I get a crash report. It states “shotwell crashed with SIGABRT in g_assertion_message_expr()”

If I look at the DB after crashing, the newly added pictures and the newly created groups added by shotwell have no id but value NULL

A friend hypothesized, that shotwell may have auto-increment-values stored for the counters and me having already attributed those values may be the issue. I will try to test that next.

I think it uses Last Insert Rowid as the row id for a new photo row, but I am not really sure. The database code is a bit… unfathomable in places

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.