def __init__(self, image):

        data = PixelData(image)
        width = data.width
        height = data.height

        # Texture dimensions after removing the 9patch outline.
        self.width = width - 2
        self.height = height - 2

        # Only need to retain the texture for drawing
        self.texture = image.get_texture()

        # Find stretch area markers
        for x in range(1, width - 1):
            if data.is_black(x, height - 1):
                self.stretch_left = x
                break
        else:
            self.stretch_left = 1

        for x in range(width - 2, 0, -1):
            if data.is_black(x, height - 1):
                self.stretch_right = width - x
                break
        else:
            self.stretch_right = 1

        for y in range(1, height - 1):
            if data.is_black(0, y):
                self.stretch_bottom = y
                break
        else:
            self.stretch_bottom = 1

        for y in range(height - 2, 0, -1):
            if data.is_black(0, y):
                self.stretch_top = height - y
                break
        else:
            self.stretch_top = 1

        # Find content area markers, if any
        for x in range(1, width - 1):
            if data.is_black(x, 0):
                self.padding_left = x - 1
                break

        for x in range(width - 2, 0, -1):
            if data.is_black(x, 0):
                self.padding_right = self.width - x
                break

        for y in range(1, height - 1):
            if data.is_black(width - 1, y):
                self.padding_bottom = y - 1
                break

        for y in range(height - 2, 0, -1):
            if data.is_black(width - 1, y):
                self.padding_top = self.height - y
                break

        # Texture coordinates, in pixels
        u1 = 1
        v1 = 1
        u2 = self.stretch_left + 1
        v2 = self.stretch_bottom + 1
        u3 = width - self.stretch_right - 1
        v3 = height - self.stretch_top - 1
        u4 = width - 1
        v4 = height - 1

        # Texture coordinates as ratio of image size (0 to 1)
        u1, u2, u3, u4 = [s / float(width) for s in (u1, u2, u3, u4)]
        v1, v2, v3, v4 = [s / float(height) for s in (v1, v2, v3, v4)]

        # Scale texture coordinates to match the tex_coords pyglet gives us
        # (these aren't necessarily 0-1 as the texture may have been packed)
        (tu1, tv1, _, 
         _, _, _, 
         tu2, tv2, _, 
         _, _, _) = self.texture.tex_coords
        u_scale = tu2 - tu1
        u_bias = tu1
        v_scale = tv2 - tv1
        v_bias = tv1
        u1, u2, u3, u4 = [u_bias + u_scale * s for s in (u1, u2, u3, u4)]
        v1, v2, v3, v4 = [v_bias + v_scale * s for s in (v1, v2, v3, v4)]

        # 2D texture coordinates, bottom-left to top-right
        self.tex_coords = (
            u1, v1,
            u2, v1,
            u3, v1,
            u4, v1,
            u1, v2,
            u2, v2,
            u3, v2,
            u4, v2,
            u1, v3,
            u2, v3,
            u3, v3,
            u4, v3,
            u1, v4,
            u2, v4,
            u3, v4,
            u4, v4,
        )

        # Quad indices
        self.indices = []
        for y in range(3):
            for x in range(3):
                self.indices.extend([
                    x + y * 4,
                    (x + 1) + y * 4,
                    (x + 1) + (y + 1) * 4,
                    x + (y + 1) * 4,
                ])
Create NinePatch cuts of an image
dataset/ETHPy150Open ardekantur/pyglet/experimental/ninepatch.py/NinePatch.__init__