-1

在那里,我使用赛璐珞库创建了一个动画。此动画显示沿椭圆线移动的点。问题是,当我运行这段代码时,一切都在崇高的文本中工作,但是当我使用 Jupyter 时,它会输出错误。我怎么解决这个问题?您可以在答案中看到我的代码。

 import math
 import numpy as np
 import matplotlib.pyplot as plt
 from celluloid import Camera
 import copy

 img = np.ones((90,60,3))
 fig = plt.figure(figsize=(10,8), dpi = 100, facecolor='w')

 camera = Camera(fig)
 quarterCircleOne = []
 quarterCircleTwoHalfOne = []
 quarterCircleTwoHalfTwo = []
 quarterCircleThree = []
 quarterCircleFourHalfOne = []
 quarterCircleFourHalfTwo = []

xc = 45
yc = 30
rx = 25
ry = 15
def GenerateElipse(xc,yc,rx,ry):
    img[xc, yc] = (255,0,0)
    img[xc+1, yc] = (255,0,0)
    img[xc-1, yc] = (255,0,0)
    img[xc, yc+1] = (255,0,0)
    img[xc, yc-1] = (255,0,0)
    xk = 0
    yk = ry
    pk = ry**2 -rx**2 * ry+1/4* rx**2

    while ry**2 * xk < rx**2 *yk:
        if pk>0:
            xk = xk+1
            yk = yk-1
            pk = pk + 2 * ry**2 *xk + ry**2 - 2* rx**2 *yk
        else:
            xk = xk+1
            pk=pk + 2* ry**2 * xk + ry**2
        img[xc+xk, yc+yk] = (0,0,0)
        img[xc+xk, yc-yk] = (0,0,0)
        img[xc-xk, yc+yk] = (0,0,0)
        img[xc-xk, yc-yk] = (0,0,0)
        quarterCircleOne.append([xc-xk, yc-yk])
        quarterCircleTwoHalfTwo.append([xc-xk, yc+yk])
        quarterCircleThree.append([xc+xk, yc+yk])
        quarterCircleFourHalfOne.append([xc+xk, yc-yk])

    pk = ry**2 * (xk-1/2)**2 + rx**2 * (yk-1)**2 - rx**2 * ry**2

    while yk>0:
        if pk>0:
            yk = yk-1
            pk=pk -2 * rx**2 * yk + rx**2
        else:
            xk = xk+1
            yk = yk-1
            pk=pk - 2 * rx**2 * yk +  2* ry**2 *xk + rx**2
        img[xc+xk, yc+yk] = (0,0,0)
        img[xc+xk, yc-yk] = (0,0,0)
        img[xc-xk, yc+yk] = (0,0,0)
        img[xc-xk, yc-yk] = (0,0,0)
        quarterCircleOne.append([xc-xk, yc-yk])
        quarterCircleTwoHalfOne.append([xc-xk, yc+yk])
        quarterCircleThree.append([xc+xk, yc+yk])
        quarterCircleFourHalfTwo.append([xc+xk, yc-yk])

  GenerateElipse(45, 30, 25, 15)


  def DrawElipse(speed, img):
     sortedQuarterCircleOne = sorted(quarterCircleOne,key=lambda x: x[1])
     sortedQuarterCircleTwoHalfOne = 
     sorted(quarterCircleTwoHalfOne,key=lambda x: x[1])
     sortedQuarterCircleTwoHalfTwo = 
     sorted(quarterCircleTwoHalfTwo,key=lambda x: x[0])
     sortedQuarterCircleThree = sorted(quarterCircleThree,key=lambda x: x[1], 
     reverse=True)
     sortedQuarterCircleFourHalfTwo = 
     sorted(quarterCircleFourHalfTwo,key=lambda x: x[1], reverse=True)
     sortedQuarterCircleFourHalfOne = 
     sorted(quarterCircleFourHalfOne,key=lambda x: x[0], reverse=True)

     circle = [sortedQuarterCircleOne, sortedQuarterCircleTwoHalfOne, 
   sortedQuarterCircleTwoHalfTwo, sortedQuarterCircleThree, 
   sortedQuarterCircleFourHalfTwo, sortedQuarterCircleFourHalfOne]

i = 0
for x in range(0, 6):
    for coordinates in circle[x]:
        freshImage = copy.deepcopy(img)
        freshImage[coordinates[0], coordinates[1]] = (0,0,0)
        freshImage[coordinates[0], coordinates[1]+1] = (255,0,0)
        freshImage[coordinates[0], coordinates[1]-1] = (255,0,0)
        freshImage[coordinates[0]+1, coordinates[1]-1] = (0,0,255)
        freshImage[coordinates[0]+1, coordinates[1]+1] = (0,0,255)
        freshImage[coordinates[0]+1, coordinates[1]] = (255,0,0)
        freshImage[coordinates[0]-1, coordinates[1]-1] = (0,0,255)
        freshImage[coordinates[0]-1, coordinates[1]+1] = (0,0,255)
        freshImage[coordinates[0]-1, coordinates[1]] = (255,0,0)
        if i % speed == 0:
            plt.imshow(freshImage)
            camera.snap()
        i = i + 1
DrawElipse(1, img);
animation = camera.animate()
plt.show()

在此处输入图像描述

4

1 回答 1

1

“错误”Clipping input data to the valid range...源于您提供给plt.imshow. 它暗示您应该在 [0..255] 内为整数提供数据,例如在np.uint8[0..1] 内为浮点类型提供数据,例如np.float32。检查您的输入数据,并在必要时预先对其进行剪辑,或将其转换为正确的数据类型,例如plt.imshow(np.uint8(freshImage)).

该错误不是导致动画不显示的原因;这是 matplotlib 的 imshow 提供的错误。尽管输入范围存在问题,但它实际上会尽力渲染图像。

要查看动画,最后使用这个(在 Jupyter 笔记本中,请记住from IPython.display import HTML):

animation = camera.animate()
HTML(animation.to_html5_video())

根据 Celluliod 的文档:https ://github.com/jwkvam/celluloid

于 2021-03-03T23:44:46.097 回答