I help my clients acquire new users and make more money with their web businesses. I have ten years of experience with SaaS projects. If that’s something you need help with, we should get in touch!
< Back to article list

Batch resizing images using a Nautilus extension

Something I do very often is resizing images. I wrote about it before, and for a long time I’ve settled on a custom bash script.

I’ve recently found a somewhat better option: integrating a button in Nautilus, the Ubuntu file explorer.

Possible but nope

There a many ways to integrate custom code inside Nautilus in order to extend it. For example:


Nautilus proposes a simple way to create scripts on files:

This way, you can write an executable file in whatever language you like (bash, python, rust…) in order to perform tasks on said files.

Our script

Save this as, make it executable (chmod +x and copy it to ~/.local/share/nautilus/scripts. You can now select a few image files in Nautilus, right click on them -> scripts -> resize image.

#! /usr/bin/env python3

# resize an image to 25% of its original size, and store the result in
# the `small` subdirectory
# select the image files you want to resize, right click, and under Script
# choose resize-image

import os
import subprocess

def new_filename(filewithpath, subdirectory='small'):
    directory = os.path.dirname(filewithpath)
    filename = os.path.basename(filewithpath)
    target_directory = os.path.join(directory, subdirectory)
    except FileExistsError as e:
        # The directory already exists and yes, this exception is poorly named
    target_filename = os.path.join(target_directory, filename)
    return target_filename

def main():
    for filepath in os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS', '').splitlines():
        small_filename = new_filename(filepath)["convert", "-resize", "25%", filepath, small_filename])

if __name__ == '__main__':
    # alternatively you can test this script by hand by setting the env var yourself:
    # export NAUTILUS_SCRIPT_SELECTED_FILE_PATHS='/home/some/full/path/test.png'; python