Xfce Forum

Sub domains
 

You are not logged in.

#1 2016-08-31 00:53:52

jplinux
Member
Registered: 2016-08-31
Posts: 1

Adding Custom Actions in Thunar to Rotate Images using Gimp

This utilizes the Gimp batch system which requires some set up there.
in your .../.gimp-2.x/scripts directory you need to create to files: GimpRotRight.scm and GimpRotLeft.scm
GimpRotRight.scm:
(define (Gimp-Rotate-Right filename)
   (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
     (gimp-image-rotate image 0)
     (set! drawable (car (gimp-image-get-active-layer image)))
     (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
     (gimp-image-delete image)))

GimpRotLeft.scm:
(define (Gimp-Rotate-Left filename)
   (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
     (gimp-image-rotate image 2)
     (set! drawable (car (gimp-image-get-active-layer image)))
     (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
     (gimp-image-delete image)))

next create your custom actions
Name: GimpRotRight
Description: Gimp Rotate Right
Command: for file in %F;   do /bin/sh -c "gimp -i -b '(Gimp-Rotate-Right "\""$file"\"")' -b '(gimp-quit 0)'"  ;   done
check off Image Files for "Appears if selection contains"

Name: GimpRotLeft
Description: Gimp Rotate Left
Command: for file in %F;   do /bin/sh -c "gimp -i -b '(Gimp-Rotate-Left "\""$file"\"")' -b '(gimp-quit 0)'"  ;   done
check off Image Files for "Appears if selection contains"

It appears rather straight forward.
But I spend 16 hours of research and trail and error to finally get it to work.
It works for single and multiple selection.

I use it to rotate pictures physically (not exif modification), because when doing slide shows using kdenlive it need the pictures rotated physically.
Hope you all find it useful.

Offline

#2 2016-08-31 02:09:24

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: Adding Custom Actions in Thunar to Rotate Images using Gimp

Hello, welcome, and thanks for sharing.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2016-08-31 14:57:32

george_o
Member
Registered: 2013-11-05
Posts: 10

Re: Adding Custom Actions in Thunar to Rotate Images using Gimp

This is a very usefull ustom action. Thanks for sharing


dropbox.com - 2GB free online storage.

Offline

#4 2016-09-05 17:48:05

Misko_2083
Member
Registered: 2015-10-13
Posts: 211
Website

Re: Adding Custom Actions in Thunar to Rotate Images using Gimp

Another way to do this, if you don't want to keep the files in /home/user/.gimp-2.x/scripts,
create one script anywhere you like and use that script to rotate left, right or 180
gimp-rotate.sh

#!/bin/sh
# Rotate with python-fu

if [ $# -ne 2 ]; 
   then
           echo 'Usage: gimp-rotate.sh "file" [right|left|180]'
           exit
fi

case "$2" in
   right)
   OPT=0
   ;;
   left)
   OPT=2
   ;;
   180)
   OPT=1
   ;;
   *)
   echo 'Usage: gimp-rotate.sh "file" [right|left|180]'
   exit
   ;;
esac

gimp -d -i --batch-interpreter=python-fu-eval -b - << EOF
import gimpfu

def rotate(filename):
    img = pdb.gimp_file_load(filename, filename)
    layer = pdb.gimp_image_merge_visible_layers(img, 1)
    pdb.gimp_image_rotate(img, '${OPT}')
    pdb.gimp_file_save(img, layer, filename, filename)
    pdb.gimp_image_delete(img)

rotate('${1}')

pdb.gimp_quit(1)
EOF

make it executable
chmod 755 gimp-rotate.sh
Commands to use in thunar custom actions (replace /path/to/)
for file in %F; do /path/to/gimp-rotate.sh "$file" right;   done
for file in %F; do /path/to/gimp-rotate.sh "$file" left;   done
for file in %F; do /path/to/gimp-rotate.sh "$file" 180;   done

Compliments from Misko smile

Last edited by Misko_2083 (2016-09-06 04:45:09)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#5 2016-09-05 21:23:59

Misko_2083
Member
Registered: 2015-10-13
Posts: 211
Website

Re: Adding Custom Actions in Thunar to Rotate Images using Gimp

This one resizes the images but keeps the aspect ratio. Resized image will be in the same folder.
gimp-resize.sh

#!/bin/sh
# Resize, keep aspect ratio

if [ $# -ne 4 ]; 
   then
           echo "Missing parameter!"
           echo 'Usage: gimp-resize.sh "file_in" "file_out" width height'
           exit
fi

gimp -d -i --batch-interpreter=python-fu-eval -b - << EOF
import gimpfu
import math

def resize(filename_in, filename_out, max_width, max_height):

    img = pdb.gimp_file_load(filename_in, "")
    layer = pdb.gimp_image_merge_visible_layers(img, 1)

    width = int(layer.width)
    height = int(layer.height)

    max_width = int(max_width)
    max_height = int(max_height)

    if max_width <= 0:
        max_width = width
    if max_height <= 0:
        max_height= height

    if width <= max_width and height <= max_height:
        print "Nothing to do, returning"
        return

    image_aspect    = float(width) / float(height)

    boundary_aspect = float(max_width) / float(max_height)

    if image_aspect > boundary_aspect:
        new_width = max_width
        new_height= int(round(new_width/image_aspect))
    else:
        new_height = max_height
        new_width = int(round(image_aspect*new_height))

    new_name = filename_out

    pdb.gimp_image_scale(img, new_width, new_height)
    pdb.gimp_file_save(img, layer, new_name, new_name)


resize("${1}", "${2}", "${3}", "${4}")

pdb.gimp_quit(1)
EOF

make ecxecutable chmod +x

You can define the name in the custom action command.
Command: for file in %F; do /path/to/gimp-resize.sh "${file}" "${file%%.*}_resized.${file##*.}" 800 600;done

Forgot to mention there is some documentation about gimp-python https://www.gimp.org/docs/python/

Last edited by Misko_2083 (2016-09-06 04:45:36)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#6 2016-09-16 19:12:33

yurbev
Member
Registered: 2016-04-18
Posts: 55

Re: Adding Custom Actions in Thunar to Rotate Images using Gimp

Here's another way, using Jpegtran. This will replace the original image:

for file in %N; do jpegtran -rotate 90 -copy all -outfile %N %N; done

I think it's lossy though.

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.008 seconds, 8 queries executed - Memory usage: 547.73 KiB (Peak: 548.57 KiB) ]