Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Layer opacity shortcut
#1
Hi all,
I've been using Gimp for years on a fairly basic level. Recently I have been doing a lot of image stitching - there's overlap in the individual images so I just bring them in as layers, reduce the top level opacity, match positions, return it to full opacity and merge down. Not difficult but repetitive.
I make a lot of use of keyboard shortcuts ("Accelerators") and the preset "10% more Transparent" is great but I generally need to press it 3 or 4 times to get a useable level of transparency. 
That got me thinking about Gimp's scripting potential, something I've never looked at before - "set active level to 60% opacity" with a single keypress would be so useful for me.
I'm very familiar with html and Office VBA but my first looks at python-fu have me very confused. If I just want this function do I have to create a plug-in and register it then get it to appear in the list of available commands so I can assign a combination of keys? The python console itself is already pretty confusing.
I think I've got something that could maybe do what I want but I can't figure how to run it to see what happens in order to debug. Then again maybe I'm completely off track...

➤> image = gimp.image_list()[0]
➤> active_layer = pdb.gimp_image_get_active_layer(image)
➤> pdb.gimp_layer_set_opacity(active_layer, 60)

Any help with solving this would be greatly appreciated.
Reply
#2
Have a look at this:
Code:
#!/usr/bin/env python
# Author: Me
# Copyright 2018
# License: GPL v3
# GIMP plugin to do stuf for DaveMac
# For DaveMac:- https://www.gimp-forum.net/Thread-Layer-opacity-shortcut

import sys
sys.stderr = open("C:/tmp/gimp_python_errs.txt",'a')
sys.stdout=sys.stderr

from gimpfu import *
pdb = gimp.pdb

# this is the bit that does all the work
def dave_mac(image,drawable):
    if (drawable.opacity == 100):
      drawable.opacity = 60.0
    else:
      drawable.opacity = 100.0
    
    # The End of the main routine

# menu registration
register(
    "python-fu-dave_mac",
    "DaveMac",
    "Change Layer Opacity",
    "DaveMac",
    "gimp-forum.net",
    "16.03.2018",
    "Change Layer Opacity",
    "*",
    [
      (PF_IMAGE, "image",       "Input image", None),
      (PF_DRAWABLE, "drawable", "Input drawable", None),
    ],
    [],
    dave_mac,
    menu="<Image>/Layer/"
    )

main()

Most of it is just menu registration and comments.

I expanded on the specification and made it look up the current opacity. If it's currently 100, it sets it to 60, otherwise it sets it to 100.
Reply
#3
(03-16-2018, 08:20 AM)DaveMac Wrote: Hi all,
I've been using Gimp for years on a fairly basic level. Recently I have been doing a lot of image stitching - there's overlap in the individual images so I just bring them in as layers, reduce the top level opacity, match positions, return it to full opacity and merge down. Not difficult but repetitive.
I make a lot of use of keyboard shortcuts ("Accelerators") and the preset "10% more Transparent" is great but I generally need to press it 3 or 4 times to get a useable level of transparency. 
That got me thinking about Gimp's scripting potential, something I've never looked at before - "set active level to 60% opacity" with a single keypress would be so useful for me.
I'm very familiar with html and Office VBA but my first looks at python-fu have me very confused. If I just want this function do I have to create a plug-in and register it then get it to appear in the list of available commands so I can assign a combination of keys? The python console itself is already pretty confusing.
I think I've got something that could maybe do what I want but I can't figure how to run it to see what happens in order to debug. Then again maybe I'm completely off track...

➤> image = gimp.image_list()[0]
➤> active_layer = pdb.gimp_image_get_active_layer(image)
➤> pdb.gimp_layer_set_opacity(active_layer, 60)

Any help with solving this would be greatly appreciated.

By default, when you define a script where the first two parameters are an image and a layer, the script is called with these parameters set as a the current image and active layer, so you get them without doing anything specific.

You code you look  like this:

Code:
# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os, sys
from gimpfu import *

# The functions that do it, they will be called with image/layer
# set to the image/layer on which you call the script
# Technically, they can also be called with 'layer' being actually a "channel",
# (layers and channels are both "drawable") but you can't set the opacity
# on a channel and the script will croak. Not really a problem here.

def opacity60(image,layer):
    pdb.gimp_layer_set_opacity(layer, 60)

def opacity100(image,layer):
    pdb.gimp_layer_set_opacity(layer, 100)

### Registrations

# Not strictly necessary, but this tells you where the file with the script is
whoiam='\n'+os.path.abspath(sys.argv[0])

# When Gimp starts it calls this script which executes the two calls to the register()
# function which themselves call Gimp and tell it what/how to call in this file.
# As you can see you can have two functions in one single file
register(
    'opacity60',    # identifier of the plugin. When defining keybaird shortcut, look for python-fu-opacity60
    'Set opacity to 60%'+whoiam,    # The help on the menu (tells where the script file is)
    'Set opacity to 60%',           # "Additional information" in the PDB browser
    'Me','Myself','2018',           # Copyright/author info, normally
    '60%',                          # the menu item
    "RGB*, GRAY*",  # Enable only on RGB and grayscale image (wouldn't work on color-indexed)
    # The two parameters to the script, set automatically with current image/layer
    [
        (PF_IMAGE, "image", "Input image", None),
        (PF_DRAWABLE, "drawable", "Input drawable", None)
    ],
    [],
    opacity60,                      # the function above in "def opacity60(image,layer)..."
    menu="<Image>/Layer/Opacity",   # the menu in which you'll hjave the '60%' item above
)

# Barebones version for 100%
register(
    'opacity100','','','','','','100%','RGB*, GRAY*',
    [(PF_IMAGE, "image", "Input image", None),(PF_DRAWABLE, "drawable", "Input drawable", None)],[],
    opacity100,menu="<Image>/Layer/Opacity"
)

main()

As you can see:

  1. there are more comments than code
  2. for such a simple function telling Gimp how to call the code is more complicated than the code itself.
Attached the source python file. If you edit it, keep in mind that Python is very picky about code indentation. And use a programmer's editor.


PS: VBA, really?


Attached Files
.zip   opacity.zip (Size: 1.11 KB / Downloads: 228)
Reply
#4
Thanks to both of you. I will have a look at these over the weekend.

@ofnuts: yes, VBA (not VB6). Excel is still the world's standard spreadsheet so writing applications in VBA fills half my working life!
Reply
#5
(03-16-2018, 08:28 PM)DaveMac Wrote: Thanks to both of you. I will have a look at these over the weekend.

@ofnuts: yes, VBA (not VB6). Excel is still the world's standard spreadsheet so writing applications in VBA fills half my working life!

Well, you have a sad life Smile... This said, I have seen a delivery truck management application where XML files are parsed in COBOL, so someone somewhere has an even sadder life.
Reply
#6
You have no idea how sad!
:-D

The other half of my business life is spent "playing" with SAP
Reply
#7
(03-17-2018, 04:16 PM)DaveMac Wrote: You have no idea how sad!
:-D

The other half of my business life is spent "playing" with SAP

OK, I widthdraw what I said about XML and Cobol. You definitely win. I hope Gimp and Python will bring some lightness...
Reply
#8
Thumbs Up 
Sorry for not getting back sooner. I chose Kevin's version since the if/else gives it a toggle function.
It works a treat!
I use an Infinitton LCD keypad and the image for the key I've set to the assigned shortcut also toggles.
         
Reply


Forum Jump: