voronoi图编程构造_使用 python 实现 Voronoi 图

该篇博客介绍了一个使用Python实现Voronoi图的程序,通过导入PIL、random和math库,生成随机像素点并计算每个像素点到最近随机点的距离,以此创建颜色丰富的Voronoi图。最后,程序将生成的图像保存为PNG文件并显示出来。
摘要由CSDN通过智能技术生成

以下程序 使用python实现 Voronoi图from PIL import Image

import random

import math

def generate_voronoi_diagram(width, height, num_cells):

image = Image.new("RGB", (width, height))

putpixel = image.putpixel

imgx, imgy = image.size

nx = []

ny = []

nr = []

ng = []

nb = []

for i in range(num_cells):

nx.append(random.randrange(imgx))

ny.append(random.randrange(imgy))

nr.append(random.randrange(256))

ng.append(random.randrange(256))

nb.append(random.randrange(256))

for y in range(imgy):

for x in range(imgx):

dmin = math.hypot(imgx-1, imgy-1)

j = -1

for i in range(num_cells):

d = math.hypot(nx[i]-x, ny[i]-y)

if d < dmin:

dmin = d

j = i

putpixel((x, y), (nr[j], ng[j], nb[j]))

image.save("VoronoiDiagram.png", "PNG")

image.show()

generate_voronoi_diagram(500, 500, 25)

本文由【waitig】发表在等英博客 本文固定链接:使用 python 实现 Voronoi 图 欢迎关注本站官方公众号,每日都有干货分享!

点赞 (0)赏分享 (0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值