From cf3938b720b50feceafb0d3b124d24b5bc01c41e Mon Sep 17 00:00:00 2001 From: thomasba Date: Sun, 2 Sep 2018 02:33:04 +0200 Subject: [PATCH 1/3] Scripts for managing picture files --- bin/rename_images | 46 ++++++++++++++++++++ bin/sort_images_into_folders | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100755 bin/rename_images create mode 100755 bin/sort_images_into_folders diff --git a/bin/rename_images b/bin/rename_images new file mode 100755 index 0000000..20886a8 --- /dev/null +++ b/bin/rename_images @@ -0,0 +1,46 @@ +#!/bin/env python3 +# vim: noexpandtab ts=4 ft=python : + +import os +import re +import operator +import PIL.Image +import PIL.ExifTags + +def get_date(filename): + img = PIL.Image.open(filename) + d = re.match(r'(?P\d{4}):(?P\d{2}):(?P\d{2}) (?P\d{2}):(?P\d{2}):(?P\d{2})', img._getexif()[306]) + if d is None: + return None + img.close() + return "%04d%02d%02d_%02d%02d%02d" % (int(d["year"]), int(d["month"]), int(d["day"]), int(d["hour"]), int(d["minute"]), int(d["second"])) + +def print_new_filename(number, date, name, original_filename): + ext = original_filename.split('.')[-1] + return "%s_%s_%03d.%s" % (date, name, number, ext.lower()) + +def rename_file_and_nef(number, date, name, original_filename): + ext = original_filename.split('.')[-1].lower() + plain = ".".join(original_filename.split('.')[:-1]) + name = "%s_%s_%03d" % (date, name, number) + print("Handling file %s..." % (original_filename, )) + print(" Renaming from '%s' to '%s'" % (original_filename, name + "." + ext)) + os.rename(os.path.join(os.getcwd(), original_filename), os.path.join(os.getcwd(), name + "." + ext)) + if os.path.isfile(os.path.join(os.getcwd(), plain + ".NEF")): + print(" Renaming from '%s' to '%s'" % (plain + ".NEF", name + ".nef")) + os.rename(os.path.join(os.getcwd(), plain + ".NEF"), os.path.join(os.getcwd(), name + ".nef")) + elif os.path.isfile(os.path.join(os.getcwd(), plain + ".nef")): + print(" Renaming from '%s' to '%s'" % (plain + ".nef", name + ".nef")) + os.rename(os.path.join(os.getcwd(), plain + ".nef"), os.path.join(os.getcwd(), name + ".nef")) + +if PIL.ExifTags.TAGS[306] != "DateTime": + print("Warning: EXIF index for DateTime changed!") + exit(2) + +images = [[get_date(os.path.join(os.getcwd(), f)), f] for f in os.listdir(os.getcwd()) if os.path.isfile(os.path.join(os.getcwd(), f)) and re.search(r'\.jpg$', f, flags=re.IGNORECASE)] +images = sorted(images, key=operator.itemgetter(0, 1)) + +i = 1 +for img in images: + rename_file_and_nef(i, img[0], "thomas", img[1]) + i += 1 diff --git a/bin/sort_images_into_folders b/bin/sort_images_into_folders new file mode 100755 index 0000000..eba25a7 --- /dev/null +++ b/bin/sort_images_into_folders @@ -0,0 +1,81 @@ +#!/bin/env python3 +# vim: set noexpandtab tabstop=4 ft=python : +""" +A script moving pictures from one folder to subfolders (depending on the date they are taken) +""" + +import os +import argparse +import re +import shutil +import PIL.Image +import PIL.ExifTags + +def get_date(filename): + """ + Read EXIF data from a file and return a string containing "yyyymmdd" + + :param filename: The files name + """ + image = PIL.Image.open(filename) + image_exif = image._getexif() + if not image_exif: + return None + matches = re.match(r'(?P\d{4}):(?P\d{2}):(?P\d{2})', image_exif[306]) + image.close() + if matches is None: + return None + return "%04d%02d%02d" % (int(matches["year"]), int(matches["month"]), int(matches["day"])) + +def main(): + """ + Move files to a directory by date + + :param args: CLI arguments + """ + # verify we're getting the correct data :) + if PIL.ExifTags.TAGS[306] != "DateTime": + print("ERROR: EXIF index for DateTime changed!") + exit(2) + # parse arguments + parser = argparse.ArgumentParser(description="Move/copy pictures to subfolders") + parser.add_argument("--source", "-s", action="store", default=os.getcwd(), metavar="", \ + help="The source directory (default = current)", dest="source") + parser.add_argument("--destination", "-d", action="store", default=os.getcwd(), metavar="", \ + help="The destination directory (default = current)", dest="destination") + parser.add_argument("--copy", "-c", action="store_true", dest="copy_files", \ + help="Copy the files insted of moving them") + args = parser.parse_args() + # settings + source_directory = args.source + destination_directory = args.destination + copy_files = args.copy_files + # iterate through directory + for filename in os.listdir(source_directory): + if os.path.isfile(os.path.join(source_directory, filename)) and \ + re.search(r'\.jpg$', filename, flags=re.IGNORECASE): + date = get_date(os.path.join(source_directory, filename)) + if date is None: + print("WARNING: No EXIF data for file '%s'" % (filename)) + continue + directory = os.path.join(destination_directory, date) + # create directory if it not exists + if os.path.exists(directory): + if not os.path.isdir(directory): + print("'%s' exists but is no directory!" % (date)) + continue + else: + print("Creating directory '%s'" % (directory)) + os.mkdir(directory) + # copy or move file + if copy_files: + print("Copy file '%s' to '%s'" % (filename, date)) + shutil.copy2(os.path.join(source_directory, filename), \ + os.path.join(destination_directory, date, filename)) + else: + print("Moving file '%s' to '%s'" % (filename, date)) + os.rename(os.path.join(source_directory, filename), \ + os.path.join(destination_directory, date, filename)) + +if __name__ == "__main__": + main() From 9aa706f72b493e84bb29b7cb0cf2452100cfceab Mon Sep 17 00:00:00 2001 From: thomasba Date: Sun, 2 Sep 2018 02:33:28 +0200 Subject: [PATCH 2/3] Script checking running applications before shutting down the computer --- bin/po.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 bin/po.sh diff --git a/bin/po.sh b/bin/po.sh new file mode 100755 index 0000000..721348c --- /dev/null +++ b/bin/po.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# shut down computer after checking running applications + +read -p "Press enter to shutdown computer '$(hostname)'" + +PROGRAMS=("spotify" "firefox" "thunderbird" "ssh" "scp" "rsync" "pacman" "nemo" "vim" "gvim") + +READY=0 + +while [ $READY -eq 0 ] ; do + READY=1 + for PROGRAM in "${PROGRAMS[@]}"; do + PID=$(pidof "$PROGRAM" 2> /dev/null) + while [ -n "$PID" ]; do + echo "$PROGRAM still running! (pid: $PID)" + sleep 1 + PID=$(pidof "$PROGRAM" 2> /dev/null) + READY=0 + done + done +done + +poweroff + +if [ $? -ne 0 ] ; then + echo "Higher privileges are required!" + if type sudo > /dev/null ; then + sudo poweroff + else + su -c poweroff + fi +fi From 5507ec1c415b2f33cba21c4b8b992bab5b641f73 Mon Sep 17 00:00:00 2001 From: thomasba Date: Sun, 2 Sep 2018 02:34:32 +0200 Subject: [PATCH 3/3] Switched to network manager --- i3/autostart | 1 + 1 file changed, 1 insertion(+) diff --git a/i3/autostart b/i3/autostart index c8820bd..a801ab0 100755 --- a/i3/autostart +++ b/i3/autostart @@ -6,4 +6,5 @@ lxpolkit & start-pulseaudio-x11 & gnome-keyring-daemon --start --daemonize --components=pkcs11,secrets,ssh,gpg & xset r rate 400 30 +nm-applet & sleep 2 && xmodmap ~/.i3/speedswapper &