Create Anaglyph (GIMP Plugin)
gimpfu_numpy_converter.py
Go to the documentation of this file.
1 from gimpfu import *
2 import numpy
3 
4 def layer2array(layer,dtype=None):
5  """!
6  converts a layer to an array.
7  @param layer a gimp layer
8  @param dtype the requested output type (optional)
9  @return a numpy array representing the layer (height x width x channels)
10  """
11  r = layer.get_pixel_rgn(0, 0, layer.width, layer.height, False, False)
12  m = numpy.reshape(numpy.fromstring(r[:,:],dtype=numpy.uint8),(r.h,r.w,r.bpp))
13  if dtype!=None:
14  return numpy.array(m, dtype=dtype)
15  else:
16  return m
17 
18 def copy_array_into_layer(data, layer):
19  """!
20  copies an array into a layer. (image/layer size must match)
21  @param layer a gimp layer
22  @param data a numpy array representing the layer (height x width x channels; must match the layers' size/channels). The data is converted into uint8.
23  """
24  r = layer.get_pixel_rgn(0, 0, layer.width, layer.height, False, False)
25  data8 = numpy.array(data[:],dtype=numpy.uint8);
26  r[:,:] = data8.tostring()
27 
28 
def copy_array_into_layer(data, layer)
copies an array into a layer.
def layer2array(layer, dtype=None)
converts a layer to an array.