简体   繁体   English

Gimp Python插件gimp.Image为numpy数组

[英]Gimp Python plugin gimp.Image as numpy array

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. 我正在为GIMP开发一个python插件,我想获得一个层的RGB矩阵作为numpy数组。 To access the layer in the python plugin I use the next code: 要访问python插件中的图层,我使用下一个代码:

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. 我想制作layer变量,而不是gimp.Image变量,这是一个numpy数组,包含每个像素的RGB值。 What I use in other nonGimp-python code is this next line: frame2 = misc.imread('C:\\Users\\User\\Desktop\\image2.png').astype(np.float32) . 我在其他nonGimp-python代码中使用的是下一行: 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: 如果我打印frame2我得到一个像这样的矩阵,包含每个像素的RGB值:

[[[ 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? 有没有办法将gimp.Image类型变量转换为numpy数组而不将其保存在文件中并使用Scipy重新加载?

Thanks. 谢谢。

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). 这将返回一个1/3/4字节的字符串(参见region.bpp ),因此例如白色像素返回为'\\xff\\xff\\xff '而红色的像素返回为'\\xff\\x00\\x00' (假设没有alpha通道:3bpp)。

You can also access areas with slices, so the 4 pixels in the top left corner are: 您还可以访问带切片的区域,因此左上角的4个像素为:

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' . 这将返回一个12字节的字符串(16个带alpha通道) '\\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 直接层<> nparray函数

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM