def _generate_png(self, matrix, width, height, padding, foreground, background):
        """
        Generates an identicon image in the PNG format out of the passed block
        matrix, with the requested width, height, padding, foreground colour,
        and background colour.

        Arguments:

          matrix - Matrix describing which blocks in the identicon should be
          painted with foreground (background if inverted) colour.

          width - Width of resulting identicon image in pixels.

          height - Height of resulting identicon image in pixels.

          padding - Tuple describing padding around the generated identicon. The
          tuple should consist out of four values, where each value is the
          number of pixels to use for padding. The order in tuple is: top,
          bottom, left, right.

          foreground - Colour which should be used for foreground (filled
          blocks), represented as a string of format supported by the
          PIL.ImageColor module.

          background - Colour which should be used for background and padding,
          represented as a string of format supported by the PIL.ImageColor
          module.

        Returns:

          Identicon image in PNG format, returned as a byte list.
        """

        # Set-up a new image object, setting the background to provided value.
        image = Image.new("RGB", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background)

        # Set-up a draw image (for drawing the blocks).
        draw = ImageDraw.Draw(image)

        # Calculate the block widht and height.
        block_width = width // self.columns
        block_height = height // self.rows

        # Go through all the elements of a matrix, and draw the rectangles.
        for row, row_columns in enumerate(matrix):
            for column, cell in enumerate(row_columns):
                if cell:
                    # Set-up the coordinates for a block.
                    x1 = padding[2] + column * block_width
                    y1 = padding[0] + row * block_height
                    x2 = padding[2] + (column + 1) * block_width - 1
                    y2 = padding[0] + (row + 1) * block_height - 1

                    # Draw the rectangle.
                    draw.rectangle((x1, y1, x2, y2), fill=foreground)

        # Set-up a stream where image will be saved.
        stream = BytesIO()

        # Save the image to stream.
        image.save(stream, format="png", optimize=True)
        image_raw = stream.getvalue()
        stream.close()

        # Return the resulting PNG.
        return image_raw
dataset/ETHPy150Open azaghal/pydenticon/pydenticon/__init__.py/Generator._generate_png/original