资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在Python3中使用Pillow生成一个分形树图片-创新互联

这篇文章给大家介绍如何在Python 3中使用Pillow生成一个分形树图片,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联专注于万秀网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供万秀营销型网站建设,万秀网站制作、万秀网页设计、万秀网站官网定制、微信小程序服务,打造万秀网络公司原创品牌,更为您提供万秀网站排名全网营销落地服务。

该程序通过绘制树干(最初是树;后来是树枝)并递归地添加树来绘制“树”。 使用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生成一个分形树图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享名称:如何在Python3中使用Pillow生成一个分形树图片-创新互联
本文URL:http://cdkjz.cn/article/dsodij.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220