{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport ast\n\nimport matplotlib.pyplot as plt\n\nfrom matplotlib import animation, rc\nfrom IPython.display import HTML, Image\n\nimport cv2\n\nrc('animation', writer='imagemagick')\n\ncat = pd.read_csv('../input/train_simplified/owl.csv', nrows=500)\ncat['drawing'] = cat['drawing'].apply(ast.literal_eval)\n\n\n\nBASE_SIZE = 256\n\n\ndef draw_cv2(raw_strokes, step, size=256, lw=6):\n   \n    max_x = max( [max(x)  for x, y in raw_strokes ] )\n    max_y = max( [max(y)  for x, y in raw_strokes ] )\n   \n    max_x, max_y =  max_x + 1, max_y + 1\n    img = np.zeros((max_y, max_x), np.uint8)\n\n   \n    for i in range(step):\n        stroke = raw_strokes[i]\n        \n        x,y=stroke[0], stroke[1]\n        for i in range(len(x) - 1):\n            _ = cv2.line(img, (x[i], y[i]),\n                         (x[i + 1], y[i + 1]), 255, lw)\n   \n   \n    top = max ( (BASE_SIZE - max_y) // 2 , 0 )\n    bottom = max ( BASE_SIZE - top - max_y , 0)\n   \n    left = max (  (BASE_SIZE - max_x) // 2, 0 )\n    right = max ( BASE_SIZE - max_x - left, 0 )\n   \n   \n    img =  cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT)\n\n    if size != BASE_SIZE:\n        return cv2.resize(img, (size, size))\n    else:\n        return img\n\n\n\nclass Draw:\n    def __init__(self, raw_strokes, fig):\n        self.raw_strokes = raw_strokes\n        self.count_frame = len(raw_strokes)\n        self.fig = fig\n    \n    def animation(self, size=256, lw=6, interval=250, blit=True):\n        ims = []\n        for frame in range(self.count_frame):\n            image = draw_cv2(self.raw_strokes, frame, size, lw)\n            im=plt.imshow(image, animated=True)\n            #im.set_cmap('hot')\n            #plt.axis('off')\n            ims.append([im])\n        \n        \n        return animation.ArtistAnimation(self.fig, ims, interval, blit)\n        \n        \nfig=plt.figure()    \ndraw = Draw (cat['drawing'].values[182], fig )\nanim = draw.animation(size=256, lw=5)  \n#HTML(anim.to_html5_video())    \nanim.save(\"owl.gif\")\n\nplt.close(fig)\n\nImage(url='owl.gif',width=512, height=512)\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}