Python 使用静态点的Bezier曲线

Python 使用静态点的Bezier曲线,python,python-imaging-library,bezier,curve,Python,Python Imaging Library,Bezier,Curve,所以我在线上找到了这段代码,它生成了一条使用随机点的随机贝塞尔曲线。我试图使它非随机,这样它将使用静态点,我得到它只使用4个点,这是很容易的。我以前从未在python中使用过PIL,事实上我正在慢慢学习python。我只做过前端工作(html、javascript、css等),我只是想知道是否有人能帮我。 以下是我在第行找到的代码: # Random Bezier Curve using De Casteljau's algorithm # http://en.wikipedia.org/wik

所以我在线上找到了这段代码,它生成了一条使用随机点的随机贝塞尔曲线。我试图使它非随机,这样它将使用静态点,我得到它只使用4个点,这是很容易的。我以前从未在python中使用过PIL,事实上我正在慢慢学习python。我只做过前端工作(html、javascript、css等),我只是想知道是否有人能帮我。 以下是我在第行找到的代码:

# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
import random
from PIL import Image, ImageDraw
imgx = 500
imgy = 500
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)

def B(coorArr, i, j, t):
    if j == 0:
        return coorArr[i]
    return B(coorArr, i, j - 1, t) * (1 - t) + B(coorArr, i + 1, j - 1, t) * t

n = 4 # number of control points
coorArrX = []
coorArrY = []
for k in range(n):
    x = (0, imgx - 1)
    y = (0, imgy - 1)
    coorArrX.append(x)
    coorArrY.append(y)

# plot the curve
numSteps = 10000    
for k in range(numSteps):
    t = float(k) / (numSteps - 1)
    x = int(B(coorArrX, 0, n - 1, t))
    y = int(B(coorArrY, 0, n - 1, t))
    try:
        image.putpixel((x, y), (0, 255, 0))
    except:
        pass

# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
    except:
        pass

# image.save("BezierCurve.png", "PNG")
image.show() I add this so I can see it right away

如果有任何帮助,那就太好了。

好的,开始这一切的详细细节在长线下面。答案就在这里

您的静态点是x、y坐标,x值和y值分别放在不同的数组中(分别为coorArrx和coorArrY),请确保不要使用值=imgx或imy

# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
import random
from PIL import Image, ImageDraw
imgx = 500
imgy = 500
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)

def B(coorArr, i, j, t):
    if j == 0:
        return coorArr[i]
    return B(coorArr, i, j - 1, t) * (1 - t) + B(coorArr, i + 1, j - 1, t) * t

# n = random.randint(3, 6) # number of control points
n=4
#coorArrX = []
#coorArrY = []
#for k in range(n):
#    x = random.randint(0, imgx - 1)
#    y = random.randint(0, imgy - 1)
#    coorArrX.append(x)
#    coorArrY.append(y)
coorArrX=[3,129,12,77]
coorArrY=[128,52,12,491]

# plot the curve
numSteps = 10000
for k in range(numSteps):
    t = float(k) / (numSteps - 1)
    x = int(B(coorArrX, 0, n - 1, t))
    y = int(B(coorArrY, 0, n - 1, t))
    try:
        image.putpixel((x, y), (0, 255, 0))
    except:
        pass

# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
    except:
        pass
image.show()
== 我也是一个新来的人,我拒绝像你一样查阅这篇文章,这是一次学习的经历

但当我看这段代码时,我看到了一些奇怪的东西

for k in range(n):
    x = (0, imgx - 1)
    y = (0, imgy - 1)
    coorArrX.append(x)
    coorArrY.append(y)
你确定这部分是正确的吗?imgx在其他地方定义为500,n为4。 所以这可以理解为

for k in range(4):
    x = (0, 500 - 1)
    y = (0, 500 - 1)
这(因为这些值在本代码中根本不会改变)意味着:

每过一次。 因此,每次他们到达:

coorArrX.append(x)
coorArrY.append(y)
他们只是不断地将相同数据的新副本添加到数组中,因此当添加完成时,数组看起来是这样的(内部)

更令人困惑的是,coorArrX和coorArrY的基本部分(即每个元素都是相同的)是A)相同的,B)相同的。因此,当您看到代码的这一部分时:

# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
    except:
        pass
然后替换数组中的值,得到:

# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse(((0, 499) - 3, (0, 499) - 3, (0, 499) + 3, (0, 499) + 3), (255, 0, 0))
    except:
        pass
现在,这是控制绘图曲线段绘制的部分,但我不知道如何在这些不可能的坐标集上居中绘制任何东西

出现故障并执行了复制粘贴测试运行。这段代码纯粹是伪造的,要么是用来欺骗人们浪费时间,要么是放在OP出于同样原因发现它的地方


但是尝试很有趣

根据您的描述,唯一的问题似乎是关于Python的基础知识。我已经按照如下方式重新排列了代码,所以只需要触摸底部。现在,如果您想手动指定4个控制点,请继续操作(在下面的代码中,我自己指定了其中的4个作为示例)。您需要了解,在原始代码中,
coorArrX
coorArrY
只是列表,每个列表将包含4个点(分别为x和y坐标)。如果您手动指定它们,那么使用循环写入它们是没有意义的。我希望这段代码足够清晰:

# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
from PIL import Image, ImageDraw

def plot_curve(image, px, py, steps=1000, color=(0, 255, 0)):
    def B(coord, i, j, t):
        if j == 0:
            return coord[i]
        return (B(coord, i, j - 1, t) * (1 - t) +
                B(coord, i + 1, j - 1, t) * t)

    img = image.load()
    for k in range(steps):
        t = float(k) / (steps - 1)
        x = int(B(px, 0, n - 1, t))
        y = int(B(py, 0, n - 1, t))
        try:
            img[x, y] = color
        except IndexError:
            pass

def plot_control_points(image, px, py, radi=3, color=(255, 0, 0)):
    draw = ImageDraw.Draw(image)
    for x, y in zip(px, py):
        draw.ellipse((x - radi, y - radi, x + radi, y + radi), color)


# Your fixed, manually specified, points.
n = 4
coord_x = [25, 220, 430, 410]
coord_y = [250, 10, 450, 40]

image = Image.new("RGB", (500, 500))

plot_curve(image, coord_x, coord_y)
plot_control_points(image, coord_x, coord_y)

image.save("BezierCurve.png")

代码对我不起作用,不能使用
随机
在任何地方使用…我不确定你的疑问是什么,在你更改之前我已经看到了原始代码。因此,您定义了
n=4
。现在您想自己指定4个点,而不是使用
random
函数来获得它们?您所要做的就是为
coorArrX
提供四个x坐标,为
coorArrY
提供四个y坐标,并删除当前代码中填充这些列表的伪循环。例如,定义
coorArrX=[25,220,430,430]
coorArrY=[250,10,450,40]
,并移除范围(n)中k的循环
。…
。这解决了你的问题吗?这就是在x=random.randint(0499)y=random.randint(0499)之前的情况,所以它生成了一个介于0和499hmmmm之间的随机数…这些行到底在哪里,或者你可以链接原始页面源吗?这是链接OK,现在我明白了,但我不理解维基百科上的原始公式,所以虽然我理解这里使用的代码,但我不确定您请求的“静态”点部分。我认为它们的x和y值只需要写入coorArrx和coorArrY(假设你的点是3,2,将3附加到x数组,2附加到y数组)让我做一些实验,然后返回给你。这就是我所做的,只是为了测试范围(n)内k的几个点n=4#控制点的数量coorArrx=[]coorArrY=[]:x=100 y=100 coorArrX.append(x)coorArrY.append(y)if(x!=0)and(y!=0):x=150 y=50 coorArrX.append(x)coorArrY.append(y)if(x!=0)and(y!=0):x=200 y=50 coorArrX.append(x)coorArrY.append(y)if(x!=10)and(y!=10):x=280 y=100 coorArrX.append(x)coorArrY.append(y)
# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse(((0, 499) - 3, (0, 499) - 3, (0, 499) + 3, (0, 499) + 3), (255, 0, 0))
    except:
        pass
# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
from PIL import Image, ImageDraw

def plot_curve(image, px, py, steps=1000, color=(0, 255, 0)):
    def B(coord, i, j, t):
        if j == 0:
            return coord[i]
        return (B(coord, i, j - 1, t) * (1 - t) +
                B(coord, i + 1, j - 1, t) * t)

    img = image.load()
    for k in range(steps):
        t = float(k) / (steps - 1)
        x = int(B(px, 0, n - 1, t))
        y = int(B(py, 0, n - 1, t))
        try:
            img[x, y] = color
        except IndexError:
            pass

def plot_control_points(image, px, py, radi=3, color=(255, 0, 0)):
    draw = ImageDraw.Draw(image)
    for x, y in zip(px, py):
        draw.ellipse((x - radi, y - radi, x + radi, y + radi), color)


# Your fixed, manually specified, points.
n = 4
coord_x = [25, 220, 430, 410]
coord_y = [250, 10, 450, 40]

image = Image.new("RGB", (500, 500))

plot_curve(image, coord_x, coord_y)
plot_control_points(image, coord_x, coord_y)

image.save("BezierCurve.png")