MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_image

Function get_image

fractals/mandelbrot.py:86–129  ·  view source on GitHub ↗

Function to generate the image of the Mandelbrot set. Two types of coordinates are used: image-coordinates that refer to the pixels and figure-coordinates that refer to the complex numbers inside and outside the Mandelbrot set. The figure-coordinates in the arguments of this fun

(
    image_width: int = 800,
    image_height: int = 600,
    figure_center_x: float = -0.6,
    figure_center_y: float = 0,
    figure_width: float = 3.2,
    max_step: int = 50,
    use_distance_color_coding: bool = True,
)

Source from the content-addressed store, hash-verified

84
85
86def get_image(
87 image_width: int = 800,
88 image_height: int = 600,
89 figure_center_x: float = -0.6,
90 figure_center_y: float = 0,
91 figure_width: float = 3.2,
92 max_step: int = 50,
93 use_distance_color_coding: bool = True,
94) -> Image.Image:
95 """
96 Function to generate the image of the Mandelbrot set. Two types of coordinates
97 are used: image-coordinates that refer to the pixels and figure-coordinates
98 that refer to the complex numbers inside and outside the Mandelbrot set. The
99 figure-coordinates in the arguments of this function determine which section
100 of the Mandelbrot set is viewed. The main area of the Mandelbrot set is
101 roughly between "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
102
103 Commenting out tests that slow down pytest...
104 # 13.35s call fractals/mandelbrot.py::mandelbrot.get_image
105 # >>> get_image().load()[0,0]
106 (255, 0, 0)
107 # >>> get_image(use_distance_color_coding = False).load()[0,0]
108 (255, 255, 255)
109 """
110 img = Image.new("RGB", (image_width, image_height))
111 pixels = img.load()
112
113 # loop through the image-coordinates
114 for image_x in range(image_width):
115 for image_y in range(image_height):
116 # determine the figure-coordinates based on the image-coordinates
117 figure_height = figure_width / image_width * image_height
118 figure_x = figure_center_x + (image_x / image_width - 0.5) * figure_width
119 figure_y = figure_center_y + (image_y / image_height - 0.5) * figure_height
120
121 distance = get_distance(figure_x, figure_y, max_step)
122
123 # color the corresponding pixel based on the selected coloring-function
124 if use_distance_color_coding:
125 pixels[image_x, image_y] = get_color_coded_rgb(distance)
126 else:
127 pixels[image_x, image_y] = get_black_and_white_rgb(distance)
128
129 return img
130
131
132if __name__ == "__main__":

Callers 1

mandelbrot.pyFile · 0.85

Calls 3

get_color_coded_rgbFunction · 0.85
get_black_and_white_rgbFunction · 0.85
get_distanceFunction · 0.70

Tested by

no test coverage detected