def make_image_surface(bitmap, copy=True): if (type(bitmap) == FT_Bitmap): # bare FT_Bitmap content = bitmap else: # wrapped Bitmap instance content = bitmap._FT_Bitmap "creates a Cairo ImageSurface containing (a copy of) the Bitmap pixels." if content.pixel_mode == FT_PIXEL_MODE_MONO: cairo_format = FORMAT_A1 elif content.pixel_mode == FT_PIXEL_MODE_GRAY: cairo_format = FORMAT_A8 else: raise NotImplementedError("unsupported bitmap format %d" % content.pixel_mode) src_pitch = content.pitch dst_pitch = ImageSurface.format_stride_for_width(cairo_format, content.width) if not copy and dst_pitch == src_pitch and content.buffer != None: pixels = content.buffer else: pixels = to_array(content, content.pixel_mode, dst_pitch) result = ImageSurface.create_for_data(pixels, cairo_format, content.width, content.rows, dst_pitch) return result
def func(image, objects, *args, **kwargs): # Allow sending in a single object in every function. if not (isinstance(objects, list) or isinstance(objects, tuple)): objects = [objects] # Calculate the appropriate scaling for the markers. area = image.shape[1] * image.shape[0] for ref_area, scale in MARKER_SCALES: if area >= ref_area: break # TODO: Ideally, we would like to avoid having to create a copy of the # array just to add paddings to support the cairo format, but there's # no way to use a cairo surface with 24bpp. # TODO: Take into account endianness of the machine, as it's possible # it has to be concatenated in the other order in some machines. # We need to add an extra `alpha` layer, as cairo only supports 32 bits # per pixel formats, and our numpy array uses 24. This means creating # one copy before modifying and a second copy afterwards. with_alpha = np.concatenate([ image[..., ::-1], 255 * np.ones( (image.shape[0], image.shape[1], 1), dtype=np.uint8) ], axis=2) surface = ImageSurface.create_for_data(with_alpha, cairo.Format.RGB24, image.shape[1], image.shape[0]) ctx = Context(surface) # Set up the font. If not available, will default to a Sans Serif # font available in the system, so no need to have fallbacks. ctx.select_font_face("DejaVuSans-Bold", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(int(16 * scale)) vis_func(ctx, objects, scale=scale, *args, **kwargs) # Return the newly-drawn image, excluding the extra alpha channel # added. image = with_alpha[..., :-1][..., ::-1] return image
def _make_image_surface(bitmap, copy=True): """Convert FreeType bitmap to Cairo ImageSurface. Special thanks to Hintak and his example code: https://github.com/rougier/freetype-py/blob/master/examples/bitmap_to_surface.py TODO (M Foley) understand this better and see if a more elegant solution exists.""" content = bitmap._FT_Bitmap cairo_format = FORMAT_A8 src_pitch = content.pitch dst_pitch = ImageSurface.format_stride_for_width(cairo_format, content.width) pixels = _to_array(content, content.pixel_mode, dst_pitch) result = ImageSurface.create_for_data(pixels, cairo_format, content.width, content.rows, dst_pitch) return result
def make_image_surface(bitmap, copy=True): if (type(bitmap) == FT_Bitmap): # bare FT_Bitmap content = bitmap else: # wrapped Bitmap instance content = bitmap._FT_Bitmap "creates a Cairo ImageSurface containing (a copy of) the Bitmap pixels." if (False and (byteorder == 'little') and (content.pixel_mode == FT_PIXEL_MODE_MONO)): try: libtiff = CDLL("not") except: library = get_handle() dest = c_void_p() error = FT_Bitmap_Init(dest) if error: raise FT_Exception(error) error = FT_Bitmap_Convert(library, byref(content), dest, 1) if error: raise FT_Exception(error) content = dest if content.pixel_mode == FT_PIXEL_MODE_MONO: cairo_format = FORMAT_A1 elif content.pixel_mode == FT_PIXEL_MODE_GRAY: cairo_format = FORMAT_A8 else: raise NotImplementedError("unsupported bitmap format %d" % content.pixel_mode) src_pitch = content.pitch dst_pitch = ImageSurface.format_stride_for_width(cairo_format, content.width) if not copy and dst_pitch == src_pitch and content.buffer != None: pixels = content.buffer else: pixels = to_array(content, content.pixel_mode, dst_pitch) result = ImageSurface.create_for_data(pixels, cairo_format, content.width, content.rows, dst_pitch) return result