from gimpfu import *
import os,sys
#GLOBAL CONSTANT
def inpaint(image,layer):
_margin=32
import numpy as np
import cv2
#image=gimp.image_list()[0]
#layer=active_layer = pdb.gimp_image_get_active_layer(image)
import numpy as np
import cv2
def crop(image,layer):
#bbox
margin=_margin
flag,sx0,sy0,sx1,sy1=gimp.pdb.gimp_selection_bounds(image)
#global axis
if not flag : return
sel=pdb.gimp_image_get_selection(image)
sx0=max(sx0-margin,0)
sy0=max(sy0-margin,0)
sx1=min(sx1+margin,sel.width)
sy1=min(sy1+margin,sel.height)
#layer local to global
ofsx,ofsy=layer.offsets
lx0=ofsx
ly0=ofsy
lx1=ofsx+layer.width
ly1=ofsy+layer.height
#selction shrink
sx0=max(lx0,sx0)
sy0=max(ly0,sy0)
sx1=min(lx1,sx1)
sy1=min(ly1,sy1)
#global to local
lx0=sx0-ofsx
ly0=sy0-ofsy
lx1=sx1-ofsx
ly1=sy1-ofsy
#
selw=sx1-sx0
selh=sy1-sy0
selpix=sel.get_pixel_rgn(sx0,sy0,selw,selh)
mask_flat = np.fromstring(selpix[:,:],dtype=np.uint8)
mask=mask_flat.reshape( (selh,selw) )
#
lw=lx1-lx0
lh=ly1-ly0
bpp=layer.bpp
pix=layer.get_pixel_rgn( lx0,ly0, lw,lh)
#
img_flat= np.fromstring(pix[:,:],dtype=np.uint8)
img=img_flat.reshape( (lh,lw,bpp) )
return img,mask,pix,selpix
gimp.context_push()
image.undo_group_start()
img,mask,pix,selpix=crop(image,layer)
h,w,bpp=img.shape
if bpp==4:
imga=img
img=cv2.cvtColor(img,cv2.COLOR_RGBA2RGB,img)
#fgcol=gimp.get_foreground()
inpaintRadius=3
#cv2.inpaint(img,mask, inpaintRadius, flags[, dst]) -> dst
img2=cv2.inpaint(img,mask, inpaintRadius, cv2.INPAINT_TELEA)
idx=mask>0
img[idx,:]=img2[idx,:]
if bpp==4:
img=cv2.cvtColor(img,cv2.COLOR_RGB2RGBA,img)
img[idx,3]=imga[idx,3]
pix[:,:]=img.tostring()[:]
image.undo_group_end()
gimp.context_pop()
pdb.gimp_displays_flush()
### Registration
whoiam='\n'+os.path.abspath(sys.argv[0])
register(
#
"python-fu-inpaint",
N_("inpaint"+whoiam),
"cv2 InPaint",
"BoxHeadRoom",
"BoxHeadRoom",
"2017",
N_("Inpaint"),
"RGB*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
],
[],
inpaint,
#menu="<Image>/Filters/Distorts",
menu="<Image>/Filters/Render",
domain=("gimp20-python", gimp.locale_directory)
)
main()