def pixbuf_to_image(pixbuf: Pixbuf) -> Image.Image: data = pixbuf.get_pixels() width = pixbuf.props.width height = pixbuf.props.height stride = pixbuf.props.rowstride mode = "RGB" if pixbuf.props.has_alpha: mode = "RGBA" image = Image.frombytes(mode, (width, height), data, "raw", mode, stride) return image
def _get_pixbuf_pixel(pb: GdkPixbuf.Pixbuf, x, y): bytes = pb.get_pixels() n_channels = pb.get_n_channels() rowstride = pb.get_rowstride() bps = pb.get_bits_per_sample() bytes_per_pixel = int(n_channels * bps / 8) offset = y * rowstride + x * bytes_per_pixel return list(bytes[offset:offset + bytes_per_pixel])