用python 在空白图像上绘制一个圆,要求其参数为圆心坐标为 (249,249),其半径为150像素,利用中点圆画法及Bresenham两种画法绘制。两种算法分别两个函数,在调用某 —个函数时可用相应算法进行绘制圆。
时间: 2023-06-03 09:03:58 浏览: 75
好的,这是一个比较简单的Python图像处理问题。下面是使用中点圆画法以及Bresenham算法绘制圆的代码实现。
中点圆画法:
```python
from PIL import Image, ImageDraw
def midpoint_circle(image, center, radius, color):
x0, y0 = center
x = radius
y = 0
p = (5 - radius*4)//4
draw = ImageDraw.Draw(image)
while (x >= y):
draw.point((x + x0, y + y0), fill=color)
draw.point((-x + x0, y + y0), fill=color)
draw.point((x + x0, -y + y0), fill=color)
draw.point((-x + x0, -y + y0), fill=color)
draw.point((y + x0, x + y0), fill=color)
draw.point((-y + x0, x + y0), fill=color)
draw.point((y + x0, -x + y0), fill=color)
draw.point((-y + x0, -x + y0), fill=color)
y += 1
if (p < 0):
p += 2 * y + 1
else:
x -= 1
p += 2 * (y - x) + 1
return image
image = Image.new('RGB', (500, 500), 'white')
center = (249, 249)
radius = 150
color = 'red'
image = midpoint_circle(image, center, radius, color)
image.show()
```
Bresenham算法:
```python
from PIL import Image, ImageDraw
def bresenham_circle(image, center, radius, color):
x0, y0 = center
x = 0
y = radius
p = 3 - 2 * radius
draw = ImageDraw.Draw(image)
while (x <= y):
draw.point((x + x0, y + y0), fill=color)
draw.point((y + x0, x + y0), fill=color)
draw.point((-x + x0, y + y0), fill=color)
draw.point((-y + x0, x + y0), fill=color)
draw.point((x + x0, -y + y0), fill=color)
draw.point((y + x0, -x + y0), fill=color)
draw.point((-x + x0, -y + y0), fill=color)
draw.point((-y + x0, -x + y0), fill=color)
if (p < 0):
p += 4 * x + 6
else:
p += 4 * (x - y) + 10
y -= 1
x += 1
return image
image = Image.new('RGB', (500, 500), 'white')
center = (249, 249)
radius = 150
color = 'red'
image = bresenham_circle(image, center, radius, color)
image.show()
```
这两个函数分别使用了中点圆画法和Bresenham算法,在调用某个函数时即可绘制对应的圆。你可以根据需要选择相应的算法,将代码复制到你的Python环境中进行运行测试即可。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)