Example #1
0
def generate_monochrome_image(expression, width=300):
    """Return a grayscale image of the given expression."""
    height = width

    def convert_coords(x, y):
        """Convert absolute coordinates to relative coords
        between (-1, -1) and (1, 1)."""
        width_unit = width / 2
        height_unit = height / 2
        rx = (x - width_unit) / width_unit
        ry = (y - height_unit) / height_unit
        return (rx, ry)

    def scale_intensity(rel_intensity):
        """Convert a relative intensity from [-1, 1] to [0,255]."""
        return int(rel_intensity * 127.5 + 127.5)

    image = Image.new("L", (width, height))

    # Go through each pixel in the image.
    # We will convert each coordinate to a coordinate between
    # (-1, -1) and (1, 1) before passing it to our expression.
    for py in range(height):
        for px in range(width):
            x, y = convert_coords(px, py)
            expr_value = run_expression(expression, x, y)
            intensity = scale_intensity(expr_value)
            image.putpixel((px, py), intensity)

    return image
Example #2
0
def generate_monochrome_image(expression, width=300):
    """Return a grayscale image of the given expression."""
    height = width

    def convert_coords(x, y):
        """Convert absolute coordinates to relative coords
        between (-1, -1) and (1, 1)."""
        width_unit = width / 2
        height_unit = height / 2
        rx = (x - width_unit) / width_unit
        ry = (y - height_unit) / height_unit
        return (rx, ry)

    def scale_intensity(rel_intensity):
        """Convert a relative intensity from [-1, 1] to [0,255]."""
        return int(rel_intensity * 127.5 + 127.5)

    image = Image.new("L", (width, height))

    # Go through each pixel in the image.
    # We will convert each coordinate to a coordinate between
    # (-1, -1) and (1, 1) before passing it to our expression.
    for py in range(height):
        for px in range(width):
            x, y = convert_coords(px, py)
            expr_value = run_expression(expression, x, y)
            intensity = scale_intensity(expr_value)
            image.putpixel((px, py), intensity)

    return image