You are here

Average Layer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Rate this item!

This script is intended for a series of photographs with (on each photgraph different) unwanted objects on it, e.g. a large place with moving people.


V1.1 works with rectangular selections!
The plugin creates a new layer or a new image consisting of the arithmetical, geometrical or harmonic mean of every pixel, thereby creating a 'ghost effect' of the moving objects.
If rather you want to remove objects, choose the 'cutoff' function, which removes outlier pixels after sorting the RGB values. This is also known as winsorizing. Also median and mode remove outliers, but less efficiently.

For best results, try to cutoff only on dark, bright or on both sides.

Working on selections only greatly improves speed. Just make a rectangular selection in your image and then call the "Average Layer" script.

This was inspired by reports of a friend of mine on a Photoshop plugin, which also removes unwanted objects from a series of photographs. I haven't seen it myself nor do I know the algorithm behind it. If someone knows better algorithms than winsorizing means, tips are welcome!


You need Scipy, a scientific computing package for python. Get it at the Scipy homepage.



Teddy on three photographs in various position.

After arithmetical mean with 67% cutoff (dark outlier pixels):

Teddy removed.

Some shades are still left, one might correct this quite easily with other tools.

Code License: 
Scripting Engine: 


Hi I am trying to use your plugin, on Gimp2.8. I installed Scipy. but something is wrong.
Here is the detail of the error :

Querying plug-in: '/home/louis/.gimp-2.8/plug-ins/'
Traceback (most recent call last):
File "/home/louis/.gimp-2.8/plug-ins/", line 32, in
from scipy.stats.stats import mode, gmean, hmean, median
ImportError: cannot import name median

(gimp:22229): LibGimpBase-WARNING **: gimp: gimp_wire_read(): error
Terminating plug-in: '/home/louis/.gimp-2.8/plug-ins/'

Do you see something I can do to fix this ?

I got it to work, though I can't say it was worth the trouble for this plugin alone - it won't work on large images, but you wouldn't want it to because it is, unfortunately, very, very slow in its main appeal (median averaging) and the result is only so-so. See bottom of post for alternatives.

If you want to try it anyway, here's the steps, starting from a setup where only The GIMP was installed. Note that I have no idea what I'm doing, this is just what resulted in the plugin working - there may be more appropriate steps. Single quotes should not be entered in code, paths, etc.

  1. Install Python 2.7, 32bit(!) -
  2. Install PyGTK for Python 2.7, required for The GIMP -
  3. (Re-)install The GIMP with Python support.
  4. Install numpy for Python 2.7:
  5. Install scipy for Python 2.7:
  6. numpy and scipy install to your Python folder by default, but at least on my machine The GIMP could not find scipy (e.g. 'import scipy' failed) in this manner. To remedy this, copy the 'numpy' and 'scipy' folders:
    1. From e.g. c:\Python27\Lib\site-packages\
    2. To %programfiles%\GIMP-2.0\Python\Lib\site-packages\
  7. Download ( ) and copy the plugin file to %programfiles%\GIMP-2.0\lib\gimp\2.0\plug-ins\
  8. At this point the plugin doesn't even show up. Open the plugin file in a text editor, we'll be making some changes....
    1. change the line "/Filters/Layer Tools/A_verage Layer",
      to "A_verage Layer",
    2. change the line do_the_work)
      to do_the_work,
    3. below that line, add the line menu="/Filters/Layer Tools/")
  9. As 'louispolaire' pointed out, for some reason from scipy.stats.stats import median fails. GIMP will simply say the plugin crashed. I do not know why. But thankfully you can replace median with nanmedian, which is the same function but ignores NaN values. Those don't exist in our context (gimp image data), so that's fine.
    1. change the line from scipy.stats.stats import mode, gmean, hmean, median
      to from scipy.stats.stats import mode, gmean, hmean, nanmedian
    2. change the line self.meanarray=median(self.sarray)
      to self.meanarray=nanmedian(self.sarray)
  10. At this point it should show up, and almost run, except that it will bug out saying the function expected 7 arguments and only got 5.
    1. BEFORE the line [PF_RADIO, "meantype", "Choose mean method", "arith", ( add these lines:
      [PF_IMAGE, "image", "Input image", None],
      [PF_DRAWABLE, "drawable", "Input drawable", None],

After that, this plugin should work - as far as I tested it. Here's some pitfalls:

  • Harmonic mean will often fail on images that have zero level-values (like black). Use the Levels tool to ensure the image has no zero level-values.
  • Several of the methods will not work on large images (and by that I mean something as small as 4MP), stating that things have run out of memory.
  • Even if they did work, if you try the main appeal of this plugin - the Median method - on even a small image, you'll quickly find that it takes a very, very long time.
  • And when it's done, it's very artifact-prone when used with natural photo/video.

This is not a jab at the author - it does what it's supposed to and its processing prowess is subject to the limits of the system. But if you're really looking for a utility that will keep same-y areas between images(/layers), discarding the outliers, you'd do well to look elsewhere.

  • gimp-plugin-astronomy is relatively hassle-free and very fast. Windows binaries for GIMP 2.8 are available.
  • G'MIC has a Median layer blend option as well - also slow, but without any hassles.

Can't numpy and scipy just be unpacked into the site-packages directory of GIMP's own Python? Even if they come as installer packages in a .exe file, a reasonably capable archiving application - e.g. 7-zip - might be able to unpack them.

Quite possibly - I believe there are plain .zip files available as well, though I was just going by what was mentioned here;"install scipy". On the other hand, if you're installing Python and all that anyway, then you might as well have numpy/scipy available 'officially' for whatever other Python things you might install, rather than just GIMP.

Tried your nice plugin with 3 sample shots. The result was that "median" filter crashed with error text: File "C:\Program Files\GIMP-2.0\lib\gimp\2.0\plug-ins\", line 205, in do_the_work working_class.export() File "C:\Program Files\GIMP-2.0\lib\gimp\2.0\plug-ins\", line 183, in export self.write_out(meanlayer, self.meanarray) File "C:\Program Files\GIMP-2.0\lib\gimp\2.0\plug-ins\", line 95, in write_out assert(byte_image.size == self.selection_width * self.selection_height * self.bpp) AssertionError -> heeelp... Edit: it seems that this line returns an empty array: self.meanarray=median(self.sarray)

I cannot reproduce this error. Are your images all of the exact same size? oeller

I've just copied both of your scripts (collect layer, average layer) to my plugins-folder. While collect-layer works, I can't find the average-layer-plugin, neither under filters ... nor under Xtns/PythonFu/Console/Browse. Any idea? Thanks in advance! (WinXP, Gimp 2.4.5)

You should find it under /Filters/Layer Effects. If still not found, check executable bit (at least on unix/linux). Hope this helps!

No, it doesn't appear in the filters menu and could not be found by pythonfu-console-browser. I'm working on WinXP, so is there any equivalent to this "executable bit" in the Win-world? Ok, let's see if some other Win-user drops by. Btw, a big applause for your script!! I've often heard about this PS-plugin so it's really great to have similiar for our Gimp!

1. What's your python version? 2. Thx for the compliments! :)


Subscribe to Comments for "Average Layer"