Python Film Emulation Script/Plugin

So I wanted to create a script or plugin for Gimp to automate a simulation of particular effects or films.

I’m totally new to Python, but want to learn… (before I used Scheme).

So I got the help from AI to get started, but I need your feedback to make things better.

The aim was to simulate the style from the Harman Switch Azure film and the Kodak Aerochrome… these are already ok for me.

But I’m also trying to get an Infrared kind of style, where foliage is more white. Not the black and white version, the sky still needs to be a bit blue-ish… Of course, it should be made for photos taken with a camera that is not converted for IR, nor with the use of specific filters. So regular photos everyone makes.

I wanted to avoid third party stuff in python, such as Numpy or OpenCV.
Anyway, it’s not doing the right stuff like with analog films… as not only foliage but all green gets white, which isn’t correct… I was thinking about starting from the Aerochrome version and change the reds from the trees to white, but not yet figured out how.

Any help is welcome. Perhaps you guys have nice ideas for other scripts or plugins in this direction for color changes and experimental films?

I uploaded the 2 python files and some photo examples.
What’s your thought on this?
Did AI do a good job in scripting, or are there parts that need to be corrected? (I’m not trusting AI 100%)

filmemulation.py (4.9 KB)
digitalinfrared.py (9.5 KB)

Welcome to Python! Coming from Script-Fu gives you a head start. Using AI carefully and intelligently is super helpful (I do it myself).

Since you asked, here’s some thoughts about the plugin files you shared.

filmemulation.py

Good call on keeping the filters as DrawableFilter live layer filters. When possible, use layer filters appended to a layer, not merged, because that delivers non-destructive editing to your users.

The Aerochrome layer approach needs a rethink. Globally rotating hue 180° doesn’t result in a film emulation. It’s a crude global hue shift that may look Aerochrome-ish on some images but way off on others.

Aerochrome’s infrared-sensitive layer records vegetation red/magenta, while treating blues and skin tones differently. It transforms colour per-range, not just on one axis.

The right tool is probably gimp:hue-saturation with its range property targeting specific hue ranges independently. In GIMP 3 the range enum values are:

Gimp.HueRange.ALL
Gimp.HueRange.RED
Gimp.HueRange.YELLOW
Gimp.HueRange.GREEN    # foliage — shift toward red/magenta
Gimp.HueRange.CYAN
Gimp.HueRange.BLUE     # sky — adjust separately
Gimp.HueRange.MAGENTA

You set range first, then set hue, saturation, and lightness. A more accurate Aerochrome emulation would shift greens toward red and adjust their saturation, handle blues separately, and make any shifts independent and user-controllable. You could do multiple passes through gimp:hue-saturation with different range values, all stacked as non-destructive filters on the same layer. Layer filters are cumulative, and their order matters. This approach gives you much more creative control.

About the hue_value introspection approach:

hue_pspec = hue_cfg.find_property("hue")
hue_value = getattr(hue_pspec, "maximum", 1.0)
hue_cfg.set_property("hue", hue_value)

It tries to find the maximum normalised value as a substitute for 180°. That’s fragile. find_property() returns a property specification, but getting a known constant through introspection adds unnecessary complexity. If you continue using gimp:hue-saturation for a global rotation, just hardcode 1.0 and add a comment explaining that it represents 180° in GIMP’s normalised hue range.

This file has no exception handling in do_create_procedure(). If any step in the plugin setup fails, the procedure may not be registered correctly, and the user may not get a useful error message. I’d wrap the procedure setup in try/except blocks and report any errors so users know what went wrong and what to report.

digitalinfrared.py

This gives users a live preview with debounce, undo group handling, and selection save/restore. All good.

But the apply_gegl helper has a significant problem, and it’s a good example of plausible but counterproductive AI-generated code:

def apply_gegl(drawable, op_name, **props):
    flt = Gimp.DrawableFilter.new(drawable, op_name, op_name)
    cfg = flt.get_config()
    for key, value in props.items():
        cfg.set_property(key.replace("_", "-"), value)
    drawable.append_filter(flt)
    drawable.merge_filters()   # <-- called after every single append

merge_filters() bakes your filters into the layer, with no way to edit them as live filters. Calling it inside a helper that runs multiple times means GIMP does a full pixel merge on every iteration. That wastes time on repeated processing while losing non-destructive editability. If the intent is a destructive bake, do it once at the end. If the intent is non-destructive, use append_filter() instead, and be careful about how many times you loop around an operation.

Hardcoded values are problematic in general. In this case, values like r = 0.10, g = 0.90 + 1.30 * foliage, blur = max(1.0, big * 0.004), SKY_BLUE = "#7fb0ff" are all parameters buried in the code. Values like channel mixer base weights, the sky colour, the blur scaling factor should be exposed as procedure arguments with sensible defaults, the same way foliage, sky, and detail already are. Where a value is genuinely a fixed internal constant, name it at the top of the file rather than leaving numbers in the middle of calculations.

Exception handling is minimal. Defensive programming helps avoid unnecessary support requests, and it requires handling foreseeable failure cases properly. Your broad except Exception blocks in rebuild() and run() catch everything but tell you little. During development at least, tracebacks give focused details that help with troubleshooting:

import traceback
# ...
except Exception as exc:
    Gimp.message("Error: %s\n%s" % (exc, traceback.format_exc()))

I find full trace information invaluable in my attempts to harden my plugins. Silent failures are very hard to debug.

1 Like

@chuckhenrich Wow, that’s a lot of valuable information here. Thanks a lot!

I think I should learn more basics about Python first, the learning curve is a bit too much (or I don’t have the time right now). 20 years ago I was into JavaScript and VBA for a long time, but I’m out for a while now. Python feels a bit different in a way. I only used Scheme for a couple of Gimp scripts before. They don’t work anymore in the new Gimp version so I was planning to redo them in Python one day.

I was testing the script on landscapes with vegetation indeed, things seem to work kind of ok on foliage. But take a picture in a city, with people, the results look like nothing. It’s the foliage-tones versus skin-tones that are a whole different approach.

Your feedback on the way of scripting to get messages on errors is helpful, aswel as all other approaches. I should spend some time to pay attention on the whole process and rewrite everything later on.

1 Like

Happy to help! :+1: