プラグインのソース



import os,sys

from gimpfu import *



def dist(image):
#from common import Sketcher
#image=gimp.image_list()[0]
layer=active_layer = pdb.gimp_image_get_active_layer(image)
sel=pdb.gimp_image_get_selection(image)

assert layer.width==sel.width and layer.height == sel.height
import numpy as np
import cv2

gimp.context_push()
image.undo_group_start()

bpp=layer.bpp
pix=layer.get_pixel_rgn( 0,0, layer.width,layer.height)

img_flat= np.fromstring(pix[:,:],dtype=np.uint8)
img=img_flat.reshape( (pix.h,pix.w,bpp) )
img2=img.copy()
cvtmode={3:cv2.COLOR_RGB2GRAY,4:cv2.COLOR_RGBA2GRAY}
cvtmode2={3:cv2.COLOR_GRAY2RGB,4:cv2.COLOR_GRAY2RGBA}

im_dist=np.zeros( (pix.h,pix.w),dtype=np.float32)
im_dist8=np.zeros( (pix.h,pix.w) ,dtype=np.uint8)

sel=pdb.gimp_image_get_selection(image)
selpix=sel.get_pixel_rgn(0,0,sel.width,sel.height)
mark_flat = np.fromstring(selpix[:,:],dtype=np.uint8)
mark=mark_flat.reshape( (selpix.h,selpix.w) )
res = cv2.distanceTransform(mark, cv2.DIST_L2, 5,im_dist)
im_dist8[:]=np.uint8(255*im_dist/im_dist.max())
res=cv2.cvtColor(im_dist8,cvtmode2[bpp],img2)
#h,w,p=img2.shape
#img2_flat=img2.reshape(h*w,p)
idx=mark>0
img[idx,:]=img2[idx,:]
#ofsx,ofsy=layer.offsets
#layerw,layerh=layer.width,layer.height
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-dist",
N_("dist"+whoiam),
"cv2 distTrans",
"BoxHeadRoom",
"BoxHeadRoom",
"2015",
N_("dist"),
"RGB*",
[
(PF_IMAGE, "image", "Input image", None),

],
[],
dist,
menu="<Image>/Filters/Render",
domain=("gimp20-python", gimp.locale_directory)
)

main()