Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cairo
- CAIRO_OPERATOR_HSL_LUMINOSITY = 28 # my py2cairo seems outdated
- def convert_to_grayscale(img_in):
- img_out = img_in.create_similar(
- cairo.CONTENT_COLOR_ALPHA, img_in.get_width(), img_in.get_height())
- cr = cairo.Context(img_out)
- cr.set_source_rgba(1, 1, 1, 1)
- cr.paint()
- cr.set_source_surface(img_in)
- cr.set_operator(CAIRO_OPERATOR_HSL_LUMINOSITY)
- cr.paint()
- return img_out
- import cairo
- import numpy
- import sys
- def convert_to_grayscale(img_in):
- """Convert an image to grayscale.
- Arguments:
- img_in: (cairo.ImageSurface) input image.
- Return:
- (cairo.ImageSurface) image in grayscale, in ARGB32 mode.
- Timing:
- ~100ms to convert an image of 800x800
- Examples:
- # returns a B&W image
- >>> convert_to_grayscale(cairo.ImageSurface.create_from_png('test.png'))
- """
- a = numpy.frombuffer(img_in.get_data(), numpy.uint8)
- w, h = img_in.get_width(), img_in.get_height()
- a.shape = (h, h, 4)
- assert sys.byteorder == 'little', (
- 'The luminosity vector needs to be switched if we're in a big endian architecture. '
- 'The alpha channel will be at position 0 instead of 3.')
- alpha = a[:, :, 3]
- alpha.shape = (w, h, 1)
- luminosity_float = numpy.sum(a * numpy.array([.114, .587, .299, 0]), axis=2)
- luminosity_int = numpy.array(luminosity_float, dtype=numpy.uint8)
- luminosity_int.shape = (w, h, 1)
- grayscale_gbra = numpy.concatenate((luminosity_int, luminosity_int, luminosity_int, alpha),
- axis=2)
- stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_ARGB32, w)
- assert stride == 4 * w, 'We need to modify the numpy code if the stride is different'
- img_out = cairo.ImageSurface.create_for_data(grayscale_gbra, cairo.FORMAT_ARGB32, w, h, stride)
- return img_out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement