Sorting pictures with Python

During our last vacation 🏖️, my girlfriend and I took a lot of pictures using our phones and another camera. Back home, it was my task to look through the images, sort them and delete possible duplicates. As all our devices use different schemes for naming their files, I needed some kind of tool to rename our pictures with a consistent naming pattern. Hence, I wrote a Python package, simply called picsorter to rename my files to their respective creation dates. Sure, on a Windows machine I could have just sorted the files based on their creation dates using the explorer, however, having a consistent naming pattern achieves this sorting by default. Moreover, the pictures remain sorted on different operating systems and software.

Picture from our vacation – Temple at the marble mountains in Đà Nẵng (Vietnam)

picsorter – the tool

The Python package (including unit tests) can be found here on my GitHub. To install picsorter simply use the package manager poetry. Clone the repository and type

Bash
poetry install

in your shell, in order to set up a new virtual environment and install all necessary dependencies. After that you are good to go. For example, I created a new Python script 🐍 with the following content to rename all my .jpg files.

Python
from pathlib import Path
from picsorter import rename_multiple_files

rename_multiple_files(
    path=Path("vietnam-pictures"),
    file_extension="jpg"
)

The main function rename_multiple_files() takes two required arguments, the path as pathlib.Path object and the file_extension which is just a string. By default, the function will only rename files that are within the given path and that match the file_extension. Solely files within the folder are renamed, subfolders remain untouched. The code snippet above yields these files renamed to their creation date including hours, minutes and seconds.

Renamed files using picsorter

Although, the tool was written with pictures in mind, you can pass any file_extension and rename .xlsx, .txt and many more files.

As always, if you have any ideas/suggestions for the project or you found some bugs, feel free to message me or open an issue/pull request on GitHub. You can find my contact details on the Contact page. I’d love to hear from you! 😀

-Jakob