THUNAR - Add 'Resize Image' to context menu

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
rossdv8
Level 7
Level 7
Posts: 1736
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

THUNAR - Add 'Resize Image' to context menu

Post by rossdv8 »

The Default installation of Thunar lacks a simple method to quickly Resize Images.

However, Thunar has a function called Custom Actions, where you can add a command to the Context Menu to perform tasks on files.

A Note:

There are probably other ways to do this, and the script at the end of this post is only slightly modified from one that Cesare Riva posted some years ago online somewhere.

The main changes are:
There are more size options for convenience (originally only 3)
The script now produces a resized png AND a jpg if you resize a png image

The script works on a single selected image, or it will work on multiple selected images.
It is fairly quick, depending on the size of the images.
It keeps the Originals unchanged.

IMPORTANT:
You will need imagemagick (for the convert command) and zenity (to create a GUI for the shell script). These should already be in MINT, but if the script fails, try:

sudo apt install imagemagick
sudo apt install zenity

STEP 1 :-

In Thunar choose Edit > Configure Custome Actions

A Dialog window will open
Choose the + at Top Right
Fill in the fields thus:

Name: RESIZE Image
Description: Resize Image
Command: /home/your-user-name/.config/Thunar/custom_actions/picture_resize.sh %N
Keyboard Shortcut: (I left this Empty)
[ ] Use startup notification: (I left this Empty
Icon: (I left this Empty)






NEXT STEP 2 :-

Open fhe Directory: /home/your-user-name/.config/Thunar/custom_actions

Create a new file called "picture_resize.sh"

Open it and paste everything between the **** lines at the botom of this file,
then make the file EXECUTABLE.

Once that is done, right click on a jpeg or png file and you 'should' have a menu item that says:

RESIZE Image

Click that and you 'should' be able to choose a percentage to resize to.
50% will be about half the original size,
35% will be smaller,
75% will be bigger (about 3/4 the original size)

The images will be in a new folder called 'resized' inside the directory where you are working.

You can easily resize the images in the 'resized' folder to make them progressively smaller. They will be in a new 'resized' folder, inside the 'resized' folder.



#########################################

NEXT STEP 3 :-
Ok, now the code Copy and paste the stuff in the code box:

Code: Select all

#!/bin/bash

# Title : Thunar Custom Action to resize a picture
# Author : Cesare Riva
# Version : 1.0
# Date : 17-03-2016

DEST="./resized"
WIDTH=140
HEIGHT=450
QUALITY=75


# check required software
CHECK=("convert" "zenity")
for i in "${CHECK[@]}"
do
if ! command -v $i >/dev/null 2>&1 ; then
echo "Error: '$i' not found (install the appropriate package)" 1>&2
exit 1
fi
done

resize=$(zenity --width=$WIDTH --height=$HEIGHT --list \
--text "Choose resize level" --radiolist --column "%" --column "Resize"\
FALSE 15 FALSE 20 FALSE 25 FALSE 35 FALSE 45 TRUE 50 FALSE 60 FALSE 75 FALSE 90);

# check if "Cancel" button
if ! [[ $resize ]];
then
exit 1
fi

mkdir -p $DEST

for file
do
if [ ! -e $file ]
then
continue
fi
to_name="$DEST/"$(echo $file | cut -f1 -d.)".jpg"
convert -resize $resize% -quality $QUALITY "${file}" "${to_name}"
to_name="$DEST/"$(echo $file | cut -f1 -d.)".png"
convert -resize $resize% -quality $QUALITY "${file}" "${to_name}"
done

exit 0
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
ajgreeny
Level 7
Level 7
Posts: 1662
Joined: Mon Nov 19, 2007 3:27 pm

Re: THUNAR - Add 'Resize Image' to context menu

Post by ajgreeny »

I use a similar resize system in my Xfce DE but I chose size more simply using pixel count.

I can't remember the exact shell script I point to but in the context menu I have 3 size options available, 400, 600 and 800 pixel, used mainly for ďownsizing images to add to adverts on a local town website.

It saves so much time and hassle that it is one of my most used custom actions, though I have lots of them for many activities.
User avatar
rossdv8
Level 7
Level 7
Posts: 1736
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: THUNAR - Add 'Resize Image' to context menu

Post by rossdv8 »

Agreed.
I had the same thing set up before to choose a target Height or Width, and Resize by pixel size, but It was a pain because I would never know what the finished file size (in kilobytes) would be.

So I prefer to work with percentages because there are places where I want to post something, that it is important to be able to estimate the finished file size.

Adding an attachment to a Mint Forum post is a typical example of that, and the reason I made this post.

If I can find where I saved the old scripts I might post them here, or alternatively you might like to add yours, so people can add the choices.
There are also lots of other Custom Actions that Thunar is missing.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
ajgreeny
Level 7
Level 7
Posts: 1662
Joined: Mon Nov 19, 2007 3:27 pm

Re: THUNAR - Add 'Resize Image' to context menu

Post by ajgreeny »

Sure!

Here's my resize.sh script

Code: Select all

#!/bin/sh

mkdir -p ./Resized/$1

for file
do
if [ ! -e $file ]
then
continue
fi
toname="./Resized/"$1"/"$( echo $file | cut -f1 -d.)"_"$1".jpg"
convert -geometry $1x$1 -quality 100 "${file}" "${toname}"
done
And the custom action points to that script and appends 400, 600, or 800 %N after the pathway to the script, ie,

Code: Select all

/home/<user>/resize.sh 400 %N
The new files will be created in a sub-folder named Resized in whichever folder the images to be resized are sitting, and in that subfolder is another subfolder named 400, 600, or 800.

This suits me fine rather than using a %age size.
Post Reply

Return to “Tutorials”