LoginSignup
1
3

More than 3 years have passed since last update.

pythonの画像処理ライブラリpillowでgifアニメを作成する

Last updated at Posted at 2019-06-02

環境

  • Windows10 Pro
  • Python 3.6.4 :: Anaconda
  • numpy 1.14.0
  • pillow 6.0.0

概要

フェルマーらせん

r=a*sqrt(\theta)

aを変えてアニメーションさせてみます

コード

spiral.py
import numpy as np
import random
import colorsys
from PIL import Image, ImageDraw

#フェルマーらせんを描く関数
def radF(a, t):
    return a * np.sqrt(t)

images = []

#aを変化させてgifアニメ作成
for a in range(60, 20, -2):
    im = Image.new('RGB', (size, size), (0, 0, 0))
    draw = ImageDraw.Draw(im)
    theta = 0
    step = 2 * np.pi * 0.0001
    size = 400
    offset = size/2
    i = 0

    while i < 100000:
        i += 1
        #HSVのHのみ、iとaに応じて変化させる
        color=colorsys.hsv_to_rgb((i/2000+a/50),0.5,0.9)
        draw.line((radF(a,theta) * np.cos(theta) + offset, radF(a,theta) * np.sin(theta) + offset, radF(a,(theta + step)) * np.cos(theta + step) + offset, radF(a,(theta + step)) * np.sin(theta + step) + offset), fill=(int(color[0]*255),int(color[1]*255),int(color[2]*255)), width=2)    
        theta += step

    images.append(im)

for im in reversed(images):
    images.append(im)

images[0].save('./spiral.gif', save_all=True, append_images=images[1:], optimize=False, duration=45, loop=0)

出力

spiral.gif

bibriography

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3