def cvCreateMatFromGtkPixbuf(pixbuf): """Converts a gtk.gdk.pixbuf into a CV_8UC3 Mat The pixbuf must have 8 bits per sample and no alpha channel, or else a TypeError is raised. Since in gtk, a pixbuf's color space is RGB, the output Mat would therefore use the same color space (i.e. you may want to call cvCvtColor() to convert the color space *after* invoking this function). Whether the pixbuf's data is shared or copied to the Mat depends on whether the function gtk.gdk.get_pixels() returns its data array or a copy of the array, and not on PyOpenCV. """ if not isinstance(pixbuf, gtk.gdk.Pixbuf): raise TypeError("The first argument 'pixbuf' is not a gtk pixbuf.") if pixbuf.get_has_alpha(): raise TypeError("Pixbuf source with an alpha channel is currently not supported.") if pixbuf.get_bits_per_sample() != 8: raise TypeError("The pixbuf source does not have exactly 8 bits per sample.") rows = pixbuf.get_height() cols = pixbuf.get_width() step = pixbuf.get_row_stride() z = _NP.frombuffer(pixbuf.get_pixels(), dtype="uint8", count=rows * step).reshape(rows, step, 1) z.shape = (rows, cols, 3) z.strides = (step, 3, 1) return asMat(z)
def cvCreateImageFromPilImage(pilimage): """Converts a PIL.Image into a Mat Right now, PyOpenCV can convert PIL.Images of band ('L'), ('I'), ('F'), ('R', 'G', 'B'), or ('R', 'G', 'B', 'A'). Whether the data array is copied from PIL.Image to IplImage or shared between the two images depends on how PIL converts the PIL.Image's data into a string (i.e. via function PIL.Image.tostring()). """ try: dtype, elem_size, nchannels, decoder, mode = _pil_image_bands_to_attrs[pilimage.getbands()] except KeyError: raise TypeError("Don't know how to convert the image. Check its bands and/or its mode.") cols = pilimage.size[0] rows = pilimage.size[1] step = cols * nchannels * elem_size data = pilimage.tostring(decoder, mode, step) z = _NP.frombuffer(data, dtype=dtype, count=rows * cols * nchannels).reshape(rows, cols, nchannels) return asMat(z)