Python 3 使用Pillow生成漂亮的分形树图片
Python  /  管理员 发布于 7年前   195
该程序通过绘制树干(最初是树;后来是树枝)并递归地添加树来绘制“树”。 使用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, colorsysfrom PIL import Image, ImageDrawspread = 17 # how much branches spread apartwidth, height = 1000, 800 # window sizemaxd = 12 # maximum recursion depthlen = 9.0 # branch length factor# http://pillow.readthedocs.org/en/latest/reference/Image.htmlimg = Image.new('RGB', (width, height))# http://pillow.readthedocs.org/en/latest/reference/ImageDraw.htmld = ImageDraw.Draw(img)# This function calls itself to add sub-treesdef 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生成漂亮的分形树图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号