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.
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:
NAUTILUS_SCRIPT_SELECTED_FILE_PATHSenvironment variable that contain the list of selected 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.
Save this as
resize-image.py, make it executable (
chmod +x resize-image.py) 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) try: os.mkdir(target_directory) except FileExistsError as e: # The directory already exists and yes, this exception is poorly named pass 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) subprocess.run(["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 resize-image.py main()