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.
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.