GIMP-ML/gimp-plugins/deblur.py

58 lines
1.7 KiB
Python
Raw Normal View History

2020-05-12 05:40:43 +00:00
import os
baseLoc = os.path.dirname(os.path.realpath(__file__))+'/'
2020-04-27 04:32:33 +00:00
from gimpfu import *
import sys
sys.path.extend([baseLoc+'gimpenv/lib/python2.7',baseLoc+'gimpenv/lib/python2.7/site-packages',baseLoc+'gimpenv/lib/python2.7/site-packages/setuptools',baseLoc+'DeblurGANv2'])
import cv2
from predictorClass import Predictor
import numpy as np
def channelData(layer):#convert gimp image to numpy
region=layer.get_pixel_rgn(0, 0, layer.width,layer.height)
pixChars=region[:,:] # Take whole layer
bpp=region.bpp
# return np.frombuffer(pixChars,dtype=np.uint8).reshape(len(pixChars)/bpp,bpp)
return np.frombuffer(pixChars,dtype=np.uint8).reshape(layer.height,layer.width,bpp)
def createResultLayer(image,name,result):
rlBytes=np.uint8(result).tobytes();
2020-08-08 11:56:54 +00:00
rl=gimp.Layer(image,name,image.width,image.height,0,100,NORMAL_MODE)
2020-04-27 04:32:33 +00:00
region=rl.get_pixel_rgn(0, 0, rl.width,rl.height,True)
region[:,:]=rlBytes
image.add_layer(rl,0)
gimp.displays_flush()
def getdeblur(img):
2020-08-23 07:44:29 +00:00
predictor = Predictor(weights_path=baseLoc+'weights/deblur/'+'best_fpn.h5')
2020-08-08 11:56:54 +00:00
if img.shape[2] == 4: # get rid of alpha channel
img = img[:,:,0:3]
2020-04-27 04:32:33 +00:00
pred = predictor(img, None)
return pred
def deblur(img, layer):
2020-08-08 11:56:54 +00:00
gimp.progress_init("Deblurring " + layer.name + "...")
2020-04-27 04:32:33 +00:00
imgmat = channelData(layer)
pred = getdeblur(imgmat)
createResultLayer(img,'deblur_'+layer.name,pred)
register(
"deblur",
"deblur",
"Running deblurring.",
"Kritik Soman",
"Your",
"2020",
"deblur...",
"*", # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
[ (PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
],
[],
deblur, menu="<Image>/Layer/GIML-ML")
main()