问题描述
我正在为GIMP开发python插件,我想以numpy数组的形式获取图层的RGB矩阵.要访问python插件中的图层,请使用以下代码:
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]
我想使layer
变量而不是gimp.Image变量成为一个numpy数组,该数组为每个像素包含其RGB值.我在其他nonGimp-python代码中使用的是下一行:frame2 = misc.imread('C:\Users\User\Desktop\image2.png').astype(np.float32)
.如果我打印frame2
,我会得到一个像这样的矩阵,其中包含每个像素的RGB值:
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.]]]
有什么方法可以将gimp.Image类型的变量转换为numpy数组,而无需将其保存在文件中并使用Scipy重新加载?
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?
谢谢.
推荐答案
您也已经在关注像素区域". (在[a href ="https://www.gimp.org/docs/python/index.html" rel ="nofollow noreferrer">此处中对此进行了简要描述).基本上,给定一层:
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]
这将返回一个1/3/4字节的字符串(请参见region.bpp
),例如,白色像素返回为'\xff\xff\xff
',红色像素返回为'\xff\x00\x00'
(假设没有alpha通道:3bpp)
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).
您还可以访问带有切片的区域,因此左上角的4个像素为:
You can also access areas with slices, so the 4 pixels in the top left corner are:
cornerNW=region[0:2,0:2]
这将返回一个12字节的字符串(带有alpha通道的16个字节)'\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00'
.这可以从另一个方向进行,您可以分配给一个区域:
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
直接图层 nparray函数
我在当前实验中使用的一对函数:
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()
这篇关于Gimp Python插件gimp.Image作为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!