Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing last applied Auto Levels from a script
#4
(04-09-2024, 09:40 AM)BigMackCam Wrote: Hi @Ofnuts and thanks so much for the helpful reply...

I checked the pdb.gimp_drawable_levels procedure and it doesn't seem to support the run_mode parameter, unless I'm missing something:

   pdb.gimp_drawable_levels(drawable, channel, low_input, high_input, clamp_input, gamma, low_output, high_output, clamp_output)

Thanks for the information on how to obtain the correct directory path, and forthcoming sample code. Very much appreciated!

Sample code

Code:
#! /bin/env python

import sys,re
from collections import namedtuple

stringValuePattern=r'\(([a-z-]+) ([^)]+)\)'
intValuePattern=r'\(([a-z-]+) (\d+)\)'
floatValuePattern=r'\(([a-z-]+) (\d+(\.\d+)?)\)'
booleanValuePattern=r'\(([a-z-]+) (yes|no)\)'

ChannelSettings=namedtuple('ChannelSettings',['name','loInput','hiInput','gamma','loOutput','hiOutput'])
LevelsSettings=namedtuple('LevelsSettings',['time','linear','clampInput','clampOutput','value','red','green','blue','alpha'])

def expected(pattern,name,line):
    matched=re.search(pattern,line)
    if not matched:
        raise Exception('Pattern not matched for line %s',line)
    if matched.group(1)!=name:
        raise Exception('Unexpected name %s (expected: %s)' % (matched.group(1),name))
    return matched.group(2)

def expectedChannel(channelName,line):
    value=expected(stringValuePattern,'channel',line)
    if value!=channelName:
        raise Exception('Unexpected channel %s (expected: %s)' % (value,channelName))
                     
def expectedInt(name,line):
    value=expected(intValuePattern,name,line)
    return int(value)

def expectedFloat(name,line):
    value=expected(floatValuePattern,name,line)
    return float(value)

def expectedBoolean(name,line):
    value=expected(booleanValuePattern,name,line)
    return value=='yes'

def readChannel(channelName,levelsConf):
    expectedChannel(channelName,next(levelsConf))
    return ChannelSettings(
        channelName,
        expectedFloat('low-input',next(levelsConf)),
        expectedFloat('high-input',next(levelsConf)),
        expectedFloat('gamma',next(levelsConf)),
        expectedFloat('low-output',next(levelsConf)),
        expectedFloat('high-output',next(levelsConf)),
        )

def readLevels(file):
    with open(file) as levelsConf:
        next(levelsConf) # skip top comment
        next(levelsConf) # skip blank line
        next(levelsConf) # settings header

        return LevelsSettings(
            expectedInt('time',next(levelsConf)),
            expectedBoolean('linear',next(levelsConf)),
            expectedBoolean('clamp-input',next(levelsConf)),
            expectedBoolean('clamp-output',next(levelsConf)),
            readChannel('value',levelsConf),
            readChannel('red',levelsConf),
            readChannel('green',levelsConf),
            readChannel('blue',levelsConf),
            readChannel('alpha',levelsConf),
        )   
    
settings=readLevels(sys.argv[1])
print settings       

From a file that beings with:

Code:
# settings

(GimpLevelsConfig "2024-04-09 10:32:38"
    (time 1712651558)
    (linear no)
    (clamp-input no)
    (clamp-output no)
    (channel value)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0.05849056603773585)
    (high-output 1)
    (channel red)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0)
    (high-output 1)
    (channel green)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0)
    (high-output 1)
    (channel blue)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0)
    (high-output 1)
    (channel alpha)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0)
    (high-output 1))
(GimpLevelsConfig "2024-04-09 10:30:18"
    (time 1712651418)
    (linear no)
    (clamp-input no)
    (clamp-output no)
    (channel value)
    (low-input 0)
    (high-input 1)
    (gamma 1)
    (low-output 0)
######## etc....
Yields:
Code:
LevelsSettings(time=1712651558, linear=False, clampInput=False, clampOutput=False,
value=ChannelSettings(name='value', loInput=0.0, hiInput=1.0, gamma=1.0, loOutput=0.05849056603773585, hiOutput=1.0),
red=ChannelSettings(name='red', loInput=0.0, hiInput=1.0, gamma=1.0, loOutput=0.0, hiOutput=1.0),
green=ChannelSettings(name='green', loInput=0.0, hiInput=1.0, gamma=1.0, loOutput=0.0, hiOutput=1.0),
blue=ChannelSettings(name='blue', loInput=0.0, hiInput=1.0, gamma=1.0, loOutput=0.0, hiOutput=1.0),
alpha=ChannelSettings(name='alpha', loInput=0.0, hiInput=1.0, gamma=1.0, loOutput=0.0, hiOutput=1.0))


Moderately tested. Enjoy.
Reply


Messages In This Thread
RE: Accessing last applied Auto Levels from a script - by Ofnuts - 04-09-2024, 12:55 PM

Forum Jump: