Skip to content

Instantly share code, notes, and snippets.

@edouardklein
Created June 22, 2014 06:11
Show Gist options
  • Save edouardklein/6fef6a268c8117a2b7ba to your computer and use it in GitHub Desktop.
Save edouardklein/6fef6a268c8117a2b7ba to your computer and use it in GitHub Desktop.
Underwater picture correction with the GIMP
# This script can be used to correct red absorption
# when taking pictures underwater
# to use in the GIMP, open the python-fu console,
# set the my_dir variable and exec the file :
# exec(open('path/to/file.py).read())
# Realeased under the WTF public license
for file in listdir(my_dir):
if (file[-4:] != '.JPG'):
continue
print my_dir+'\\'+file
image = pdb.gimp_file_load(my_dir+'\\'+file,file)
display = pdb.gimp_display_new(image)
#Finding the almost-max value for red pixels (99% of pixels have a red value less than that). Stupid, brute force method.
percentile = 0
quasi_max = 1
while percentile < .999:
quasi_max += 1
mean, std_dev, median, pixels, count, percentile = pdb.gimp_histogram(image.layers[0], 1, 0, quasi_max)
#print 'quasi max, percentile'+str(quasi_max)+', '+str(percentile)
print '\tquasi_max is '+str(quasi_max)
#Adjusting the values in order to have a full histogram
new_curve = [min(255,int(i/float(quasi_max)*255.)) for i in range(0,256)]
pdb.gimp_curves_explicit(image.layers[0], 1, 256, new_curve)
pdb.gimp_file_save(image, image.layers[0], my_dir+'\\Filtered\\'+file,file)
pdb.gimp_display_delete(display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment