Python 3 使用Pillow生成漂亮的分形树图片


Posted in Python onDecember 24, 2019

该程序通过绘制树干(最初是树;后来是树枝)并递归地添加树来绘制“树”。 使用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生成漂亮的分形树图片

总结

以上所述是小编给大家介绍的Python 3 使用Pillow生成漂亮的分形树图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python的一些用法分享
Oct 07 Python
跟老齐学Python之Python安装
Sep 12 Python
Python数据分析之如何利用pandas查询数据示例代码
Sep 01 Python
Python实现调度算法代码详解
Dec 01 Python
python操作列表的函数使用代码详解
Dec 28 Python
Django自定义过滤器定义与用法示例
Mar 22 Python
pandas 转换成行列表进行读取与Nan处理的方法
Oct 30 Python
Python实现的KMeans聚类算法实例分析
Dec 29 Python
Python bisect模块原理及常见实例
Jun 17 Python
scrapy与selenium结合爬取数据(爬取动态网站)的示例代码
Sep 28 Python
Python classmethod装饰器原理及用法解析
Oct 17 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
Jan 06 Python
python保存log日志,实现用log日志画图
Dec 24 #Python
Django 限制访问频率的思路详解
Dec 24 #Python
python 统计文件中的字符串数目示例
Dec 24 #Python
如何基于python操作json文件获取内容
Dec 24 #Python
解决python 读取 log日志的编码问题
Dec 24 #Python
python实现按关键字筛选日志文件
Dec 24 #Python
python 实现提取log文件中的关键句子,并进行统计分析
Dec 24 #Python
You might like
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
php循环创建目录示例分享(php创建多级目录)
2014/03/04 PHP
php生成随机密码自定义函数代码(简单快速)
2014/05/10 PHP
PHP实现二维数组去重功能示例
2017/01/12 PHP
thinkPHP3.2.3结合Laypage实现的分页功能示例
2018/05/28 PHP
thinkphp5框架调用其它控制器方法 实现自定义跳转界面功能示例
2019/07/03 PHP
javascript同步Import,同步调用外部js的方法
2008/07/08 Javascript
javascript 对表格的行和列都能加亮显示
2008/12/26 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
2013/09/26 Javascript
jquery+html5时钟特效代码分享(可设置闹钟并且语音提醒)
2020/03/30 Javascript
jquery+ajax+text文本框实现智能提示完整实例
2016/07/09 Javascript
jQuery查找节点并获取节点属性的方法
2016/09/09 Javascript
浅析BootStrap Treeview的简单使用
2016/10/12 Javascript
ES6中Generator与异步操作实例分析
2017/03/31 Javascript
angularjs中ng-bind-html的用法总结
2017/05/23 Javascript
使用Bootstrap和Vue实现用户信息的编辑删除功能
2017/10/25 Javascript
浅析vue中常见循环遍历指令的使用 v-for
2018/04/18 Javascript
Vue 中axios配置实例详解
2018/07/27 Javascript
详解如何配置vue-cli3.0的vue.config.js
2018/08/23 Javascript
VUE接入腾讯验证码功能(滑块验证)备忘
2019/05/07 Javascript
Vue 刷新当前路由的实现代码
2019/09/26 Javascript
JS函数参数的传递与同名参数实例分析
2020/03/16 Javascript
用python实现面向对像的ASP程序实例
2014/11/10 Python
老生常谈Python基础之字符编码
2017/06/14 Python
教你使用python画一朵花送女朋友
2018/03/29 Python
pycharm工具连接mysql数据库失败问题
2020/04/01 Python
python判断是空的实例分享
2020/07/06 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
2020/12/08 Python
pycharm 配置svn的图文教程(手把手教你)
2021/01/15 Python
机械化及自动化毕业生的自我评价分享
2013/11/06 职场文书
工程造价专业大学生职业生涯规划书
2014/01/18 职场文书
先进个人事迹材料
2014/01/25 职场文书
银行业务授权委托书
2014/10/10 职场文书
检讨书模板大全
2015/05/07 职场文书
详解MySQL的半同步
2021/04/22 MySQL
Python django中如何使用restful框架
2021/06/23 Python