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判断质数(素数)的简单方法讲解
May 05 Python
Mac中Python 3环境下安装scrapy的方法教程
Oct 26 Python
Python下载网络小说实例代码
Feb 03 Python
python3第三方爬虫库BeautifulSoup4安装教程
Jun 19 Python
PyCharm设置SSH远程调试的方法
Jul 17 Python
对numpy中二进制格式的数据存储与读取方法详解
Nov 01 Python
Python理解递归的方法总结
Jan 28 Python
Python中单线程、多线程和多进程的效率对比实验实例
May 14 Python
python求最大值,不使用内置函数的实现方法
Jul 09 Python
python-Web-flask-视图内容和模板知识点西宁街
Aug 23 Python
django rest framework 自定义返回方式
Jul 12 Python
Python图像处理之图像拼接
Apr 28 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下mysql数据库操作类(改自discuz)
2010/07/03 PHP
php数组函数序列之sort() 对数组的元素值进行升序排序
2011/11/02 PHP
yii 2.0中表单小部件的使用方法示例
2017/05/23 PHP
浅谈laravel数据库查询返回的数据形式
2019/10/21 PHP
Javascript在IE下设置innerHTML时出现未知的运行时错误的解决方法
2011/01/12 Javascript
文本框中禁止非数字字符输入比如手机号码、邮编
2013/08/19 Javascript
js实现弹窗插件功能实例代码分享
2013/12/12 Javascript
基于jquery实现轮播焦点图插件
2016/03/31 Javascript
浅析jquery与checkbox的checked属性的问题
2016/04/27 Javascript
滚动条的监听与内容随着滚动条动态加载的实现
2017/02/08 Javascript
javascript事件的传播基础实例讲解(35)
2017/02/14 Javascript
Vue实现todolist删除功能
2018/06/26 Javascript
js+css实现红包雨效果
2018/07/12 Javascript
json前后端数据交互相关代码
2018/09/19 Javascript
vue项目中引入Sass实例方法
2019/08/27 Javascript
Vue之封装公用变量以及实现方式
2020/07/31 Javascript
Vue 实现监听窗口关闭事件,并在窗口关闭前发送请求
2020/09/01 Javascript
MySQL中表的复制以及大型数据表的备份教程
2015/11/25 Python
详解Python网络爬虫功能的基本写法
2016/01/28 Python
python中利用Future对象回调别的函数示例代码
2017/09/07 Python
python pandas dataframe 按列或者按行合并的方法
2018/04/12 Python
利用python打开摄像头及颜色检测方法
2018/08/03 Python
python的pygal模块绘制反正切函数图像方法
2019/07/16 Python
关于Python内存分配时的小秘密分享
2019/09/05 Python
Django利用elasticsearch(搜索引擎)实现搜索功能
2020/11/26 Python
CSS3制作文字半透明倒影效果的两种实现方式
2014/08/08 HTML / CSS
德国古洛迷亚百货官网:GALERIA Kaufhof
2017/06/20 全球购物
Piercing Pagoda官网:耳环、戒指、项链、手链等
2020/09/28 全球购物
软件缺陷的分类都有哪些
2014/08/22 面试题
建筑个人求职信范文
2014/01/25 职场文书
父母对孩子的寄语
2014/04/09 职场文书
本溪水洞导游词
2015/02/11 职场文书
2015年测量员工作总结
2015/05/23 职场文书
创业计划书之农家乐
2019/10/09 职场文书
深入浅出讲解Java8函数式编程
2022/01/18 Java/Android
Python+Tkinter制作专属图形化界面
2022/04/01 Python