Python 3 使用Pillow生成漂亮的分形树图片
作者:醉落红尘 发布时间:2022-05-03 14:53:23
标签:python,分形树,图片
该程序通过绘制树干(最初是树;后来是树枝)并递归地添加树来绘制“树”。 使用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
0
投稿
猜你喜欢
- 1. 张量的拼接(1) numpy.concatenatenp.concatenate((a1,a2,a3,…), axis=0)张量的拼接
- 我们现在回到函数上。记得我们用 SUM 这个指令来算出所有的 Sales (营业额)吧!如果我们的需求变成是要算出每一间店 (store_n
- 本文实例讲述了Python实现手写一个类似django的web框架。分享给大家供大家参考,具体如下:用与django相似结构写一个web框架
- python模拟登陆网页主要使用到urllib、urllib2、cookielib及BeautifulSoup等基本模块,当然进阶阶段我们还
- Python 循环Python 有两个原始的循环命令:while 循环for 循环while 循环如果使用 while 循环,只要条件为真,
- python以下是个人学习 python 研究判断ip连通性方法的集合。 缺点可能有办法解决,如有错误,欢迎矫正。方法一import osr
- 原图矫正后我新建了个jz的文件夹放相机矫正所需要拍摄的图片,如下:共12张 # coding:utf-8import cv2imp
- 集合的特性无序、不重复、可迭代常用api创建一个集合需要显式地使用set()方法来声明,如果使用字面量{}来声明解析器会认为这是一个字典。a
- 注意,要看懂这里,必须具备简单的Python数据分析知识,必须知道matplotlib的简单使用!例1:plt.subplot(221) #
- 一、bs4解析import requestsfrom bs4 import BeautifulSoupimport datetimeif _
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它采用完全独立于语言的文本格式,可替换XML成
- 一、Servlet实现文件上传,需要添加第三方提供的jar包下载地址:1) commons-fileupload-1.2.2-bin.zip
- 英文版:File -> settings -> Editor -> File Encodings首先打开设置:文件 -&g
- 这些帖子将分为三个部分。1.密码验证功能2.重构密码验证函数3.对密码验证功能进行单元测试这是Python系列中自定义密码验证的第三部分,也
- Python sorted() 函数sorted() 函数对所有可迭代的对象进行排序操作sorted 语法:sorted(iterable,
- Python2.7已于2020年1月1日开始停用,之前RF做自动化都是基于Python2的版本。没办法,跟随时代的脚步,我们也不得不升级以应
- 引言:Python中的变量在使用中很流畅,可以不关注类型,任意赋值,对于开发来说效率得到了提升,但若不了解其中的机理,往往也会犯一些小错,让
- 本文实例为大家分享了python3磁盘空间监控的具体代码,供大家参考,具体内容如下软硬件环境python3apscheduler前言在做频繁
- 背景:作为一个python小白,今天从菜鸟教程上看了一些python的教程,看到了python的一些语法,对比起来(有其他语言功底),感觉还
- 群里有人提出这么一个需求:每天都会传过来一份 Word 文档,里面有多个 Excel 附件,需要把 Excel 内容读取出来。第一反应是使用