Cleaning Librem 5 Pictures

I noticed Pictures loads very slow, so it seems more preferable to use a directory structure.

Long time ago I wrote a sort script or maybe had an AI do it, but I had been forgetting to run it. I added support for Screenshots to it, and ran it again today, and must say I really enjoy the feeling of a clean pictures folder on my phone.

So I figured I would attach the script below for anyone interested to use:


#!/bin/bash

# Define the directory containing the images
IMAGE_DIR="${HOME}/Pictures"

# Change to the image directory
cd "$IMAGE_DIR" || { echo "Directory not found"; exit 1; }

# Loop through each image file in the directory
for file in IMG*.*; do
    # Check if the file exists
    [ -e "$file" ] || continue

    # Check if the filename follows the expected pattern
    if [[ "$file" =~ IMG([0-9]{14})(.+)$ ]]; then
        timestamp="${BASH_REMATCH[1]}" # Gets the "YYYYMMDDHHMMSS" part
        extension="${BASH_REMATCH[2]}"  # Gets the file extension
        year="${timestamp:0:4}"         # Gets the "YYYY"
        month="${timestamp:4:2}"        # Gets the "MM"
        day="${timestamp:6:2}"          # Gets the "DD"

        # Create the directory structure
        mkdir -p "$year/$month/$day"

        # Move the file to the new location
        mv -i "$file" "$year/$month/$day/"
    else
        echo "Skipping: $file (does not match expected pattern)"
    fi
done

# Loop through each screenshot file in the directory
for file in *-*-*-*.*; do
    # Check if the file exists
    [ -e "$file" ] || continue

    # Check if the filename follows the expected pattern
    if [[ "$file" =~ ([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9]{6}).png ]]; then
        year="${BASH_REMATCH[1]}"         # Gets the "YYYY"
        month="${BASH_REMATCH[2]}"        # Gets the "MM"
        day="${BASH_REMATCH[3]}"          # Gets the "DD"

        # Create the directory structure
        mkdir -p "Screenshots/$year/$month/$day"

        # Move the file to the new location
        mv -i "$file" "Screenshots/$year/$month/$day/"
    else
        echo "Skipping: $file (does not match expected pattern)"
    fi
done

echo "Files have been organized into directories."

5 Likes

Maybe set it up as a cron job.

Also, in the case of photos, it should work to use the postprocess script to file each photo as it is taken, and you could then remove that part of the processing from the above script.

1 Like

Nice script! Throw it into a git repo, so sharing within the wider community is easier. If you don’t want to feed your code to the M$, I can very much recommend Codeberg as a hoster for git repos.

3 Likes

Do you have a link to information about postprocess?

1 Like

It looks like you are pulling image timestamps from the timestamp of the file, which is often inaccurate. Instead, extract EXIF metadata from the images and then place them into a directory hierarchy. You can use the libimage-exiftool-perl package for a single command to do this.

Run the following command within the root directory of your images. Images with identical timestamps will contain a counter in the filename.

exiftool -r -d ${OUTPUT_PATH}/%Y/%m/%Y%m%d_%H%M%S%%-c.%%e "-FileName<DateTimeOriginal" -ext ${FILETYPE} ${INPUT_PATH}

For example, to sort jpg files within the working directory:

exiftool -r -d ./%Y/%m/%Y%m%d_%H%M%S%%-c.%%e "-FileName<DateTimeOriginal" -ext jpg .

Replace jpg with another filetype e.g. arw and it will process those filetypes in the same way.


Reference

5 Likes

I’ve certainly had the issue where removing and reinserting the battery reset the phone’s clock, and if I had automatic network time turned off (or had no network), it might take a few pictures but save them with the incorrect time information because the system clock was wrong.

I could check later, but is the exif metadata also wrong in that case? I guess I can’t really picture how it would be correct.

2 Likes

(assuming that you are using millipixels) There is a script at
/usr/share/millipixels/postprocess.sh
which you can presumably hack away at in order to customise in such additional behaviour as

  • automatically deleting .dng files (if you need the disk space more than the possibility of postprocessing again later on)
  • automatically offloading all photo-related files to the µSD card in order to save disk space
  • renaming the files / implementing your own preferred directory structure
  • advanced QR code processing of a type that is not available out-of-the-box
  • … limited only by your imagination.

I too am confused by this. You appear to be getting the date/time from the name of the file, not from a file system timestamp of the file. It is certainly correct that the file system timestamp of the file can be wrong e.g. much later than the true timestamp if the file is touched in some way subsequent to taking the photo.

(For example, I have photos that I saved around the reflash upgrade from amber to byzantium. I did this through the low-tech approach of just copying the files away somewhere before the reflash and copying back afterwards. The name of the file and the image metadata will typically be happily preserved by this process but the file system timestamps may or may not be preserved, depending on what exact process was followed. In my case the result is that the file system timestamps are wildly wrong and not of any use.)

1 Like