Gimp Python plugin gimp.Image as numpy array

佐手、 提交于 2019-12-07 16:51:51

问题


I'm working on a python plugin for GIMP and I would like to obtain the RGB matrix of a layer as a numpy array. To access the layer in the python plugin I use the next code:

def python_function(img, layer):
    layer = img.layers[0]

I would like to make layer variable, instead of a gimp.Image variable, a numpy array containing, for each pixel, its RGB values. What I use in other nonGimp-python code is this next line: frame2 = misc.imread('C:\Users\User\Desktop\image2.png').astype(np.float32). If I print frame2 I get a matrix such as this one, containing for each pixel its RGB values:

[[[ 111.  179.  245.]
  [ 111.  179.  245.]
  [ 111.  179.  245.]
  ..., 
  [  95.  162.  233.]
  [  95.  162.  233.]
  [  95.  162.  233.]]

 [[ 111.  179.  245.]
  [ 111.  179.  245.]
  [ 111.  179.  245.]
  ..., 
  [  95.  162.  233.]
  [  95.  162.  233.]
  [  95.  162.  233.]]

 [[ 111.  179.  245.]
  [ 111.  179.  245.]
  [ 111.  179.  245.]
  ..., 
  [  95.  162.  233.]
  [  95.  162.  233.]
  [  95.  162.  233.]]
  ..., 
  [ 113.  127.  123.]
  [ 113.  127.  123.]
  [ 113.  127.  123.]]

 [[  98.  112.  108.]
  [  98.  112.  108.]
  [  98.  112.  108.]
  ..., 
  [ 113.  127.  123.]
  [ 113.  127.  123.]
  [ 113.  127.  123.]]]

Is there any way to convert a gimp.Image type variable to a numpy array without saving it on a file and reloading it using Scipy?

Thanks.


回答1:


You have too look at "pixel regions". These are (scantily) described here. Basically, given a layer:

You can get a region that covers the layer like this:

region=layer.get_pixel_rgn(0, 0, layer.width,layer.height)

You can access pixels by indexing:

pixel=region[x,y]

this returns a string of 1/3/4 bytes (see region.bpp), so for instance a white pixel is returned as '\xff\xff\xff' and a red one as '\xff\x00\x00' (assuming no alpha channel: 3bpp).

You can also access areas with slices, so the 4 pixels in the top left corner are:

cornerNW=region[0:2,0:2]

This returns a string of 12 bytes (16 with alpha-channel) '\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00'. This works in the other direction, you can assign to an area:

region[0:2,0:2]='\xff'*12 # set to white

Direct layer<>nparray functions

A pair of functions I use in my current experiments:

# Returns NP array (N,bpp) (single vector ot triplets)
def channelData(layer):
    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)

def createResultLayer(image,name,result):
    rlBytes=np.uint8(result).tobytes();
    rl=gimp.Layer(image,name,image.width,image.height,image.active_layer.type,100,NORMAL_MODE)
    region=rl.get_pixel_rgn(0, 0, rl.width,rl.height,True)
    region[:,:]=rlBytes
    image.add_layer(rl,0)
    gimp.displays_flush()


来源:https://stackoverflow.com/questions/47536186/gimp-python-plugin-gimp-image-as-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!