Python 3 使用Pillow生成漂亮的分形树图片
作者:醉落红尘 时间:2022-05-03 14:53:23
该程序通过绘制树干(最初是树;后来是树枝)并递归地添加树来绘制“树”。 使用Pillow。
利用递归函数绘制分形树(fractal tree),分形几何学的基本思想:客观事物具有自相似的层次结构,局部与整体在形态、功能、信息、时间、空间等方面具有统计意义上的相似性,成为自相似性。自相似性是指局部是整体成比例缩小的性质。
版本:Python 3
# Adapted from http://rosettacode.org/wiki/Fractal_tree#Python
# to parameterise, and add colour.
# http://pillow.readthedocs.org/
# Author: Alan Richmond, Python3.codes, and others (Rosettacode)
import math, colorsys
from PIL import Image, ImageDraw
spread = 17 # how much branches spread apart
width, height = 1000, 800 # window size
maxd = 12 # maximum recursion depth
len = 9.0 # branch length factor
# http://pillow.readthedocs.org/en/latest/reference/Image.html
img = Image.new('RGB', (width, height))
# http://pillow.readthedocs.org/en/latest/reference/ImageDraw.html
d = ImageDraw.Draw(img)
# This function calls itself to add sub-trees
def drawTree(x1, y1, angle, depth):
if depth > 0:
# compute this branch's next endpoint
x2 = x1 + int(math.cos(math.radians(angle)) * depth * len)
y2 = y1 + int(math.sin(math.radians(angle)) * depth * len)
# https://docs.python.org/2/library/colorsys.html
(r, g, b) = colorsys.hsv_to_rgb(float(depth) / maxd, 1.0, 1.0)
R, G, B = int(255 * r), int(255 * g), int(255 * b)
# draw the branch
d.line([x1, y1, x2, y2], (R, G, B), depth)
# and append 2 trees by recursion
drawTree(x2, y2, angle - spread, depth - 1)
drawTree(x2, y2, angle + spread, depth - 1)
# Start drawing!
drawTree(width / 2, height * 0.9, -90, maxd)
img.show()
img.save("www.linuxidc.com.png", "PNG")
效果图如下:
总结
以上所述是小编给大家介绍的Python 3 使用Pillow生成漂亮的分形树图片网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.linuxidc.com/Linux/2019-12/161794.htm
标签:python,分形树,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 实现网易邮箱邮件阅读和删除的辅助小脚本
2022-03-17 10:55:34
![](https://img.aspxhome.com/file/2023/1/107021_0s.png)
python 机器学习之支持向量机非线性回归SVR模型
2022-06-17 20:23:55
![](https://img.aspxhome.com/file/2023/1/75941_0s.png)
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
2023-09-03 10:22:39
![](https://img.aspxhome.com/file/2023/4/61234_0s.png)
名片管理系统python版
2021-08-06 01:50:15
Oracle数据库的实例/表空间/用户/表之间关系简单讲解
2023-07-20 11:48:34
![](https://img.aspxhome.com/file/2023/4/64474_0s.jpg)
Python Pandas处理CSV文件的常用技巧分享
2022-06-18 12:56:19
![](https://img.aspxhome.com/file/2023/1/99621_0s.png)
python 中Arduino串口传输数据到电脑并保存至excel表格
2022-05-17 05:41:11
![](https://img.aspxhome.com/file/2023/6/69056_0s.png)
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
2022-12-24 23:49:30
PHP PDOStatement::bindColumn讲解
2023-06-10 04:35:55
Python获取秒级时间戳与毫秒级时间戳的示例代码
2022-05-05 22:13:00
解决python3运行selenium下HTMLTestRunner报错的问题
2022-04-14 16:44:19
python实现百度文库自动化爬取
2023-02-01 23:57:22
如何使用python中的networkx来生成一个图
2022-08-13 05:13:32
![](https://img.aspxhome.com/file/2023/3/101663_0s.png)
python print出共轭复数的方法详解
2021-11-15 20:37:57
python求解三角形第三边长实例
2021-08-24 06:16:28
![](https://img.aspxhome.com/file/2023/8/95398_0s.png)
asp如何让服务器延时执行更改后的数据?
2010-05-13 16:35:00
Python 实现自动化Excel报表的步骤
2022-12-01 10:49:29
![](https://img.aspxhome.com/file/2023/2/70712_0s.jpg)
Python Flask 实现 HTML 文件压缩案例代码(9 级压缩)
2021-05-22 15:22:13
![](https://img.aspxhome.com/file/2023/5/108115_0s.png)
R语言属性知识点总结及实例
2022-06-28 04:39:02
python中Flask Web 表单的使用方法介绍
2023-08-26 15:13:24
![](https://img.aspxhome.com/file/2023/8/90748_0s.webp)