Bezier曲线使用静态点

时间:2012-12-04 23:29:35

标签: python python-imaging-library bezier curve

所以我发现这个代码在线,并且它使用随机Bezier曲线,它使用随机点。我试图让它非随机,以便它使用静态点我得到它只使用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

任何帮助,如果有的话会很棒。

2 个答案:

答案 0 :(得分:1)

Ok开始这一切的长期详细的BS低于长线。得到的答案就在这里。

您的静态点是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)

(因为这些值在此代码中根本不会改变)意味着:

x = (0, 499)
y = (0, 499)
每次通过

。 所以每次他们到达:

coorArrX.append(x)
coorArrY.append(y)

他们只是不断向数组中添加相同数据的新副本,因此完成后数组就像这样(内部)

[(0, 499), (0, 499), (0, 499), (0,499)]

使这更令人困惑的是,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

现在这是控制绘图曲线段绘制的部分,但是我没有看到elispe在那些不可能的坐标集上的中心如何能够绘制任何东西?!

打破并进行了复制粘贴测试运行。 这段代码纯粹是伪造的,要么是为了欺骗人们浪费时间,要么是因为同样的原因而放置在OP发现的地方。

但这很有趣!!

答案 1 :(得分:0)

从您的描述中,唯一的问题似乎是Python基础知识。我已按如下方式重新排列代码,因此唯一需要触及的内容是在底部。现在,如果你想手动指定4个控制点,请继续执行(在下面的代码中,我自己指定了4个控制点作为示例)。您需要了解的是,在原始代码中,coorArrXcoorArrY只是列表,每个列表将分别保留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")
相关问题