forked from ryu577/pyray
/
examples.py
103 lines (88 loc) · 5.05 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy as np
import os
from PIL import Image, ImageDraw, ImageFont, ImageMath
import circle
import rotation
####################################################################################
# To understand what's going on, read the comments carefully _ Josephus 20:21 #
####################################################################################
def draw_rotating_sphere(save_dir, number_of_circles, line_thickness):
# Craete total 30 images
for i in np.arange(30):
# We'll rotate the sphere by 18 degrees (18 = pi/10)
r = rotation.rotation(3, np.pi*i / 100.0)
# Create the canvas size of (500, 500)
im = Image.new("RGB", (500, 500), (1, 1, 1))
draw = ImageDraw.Draw(im, 'RGBA')
circle.draw_sphere(draw, np.array([0,0,0]), np.array([0,0,1]), 1, r, num_circle = number_of_circles, rgba=(182, 183, 186, 255), width = line_thickness)
# Save
file_name = save_dir + str(i) + '.png'
im.save(file_name)
def draw_oscillating_sphere(save_dir, number_of_circles, line_thickness):
# Craete total 60 images
for i in np.arange(60):
# 2.5 is an angle of Z axis
# pi * sin(k) is an oscillating factor
r = rotation.rotation(3, 2.5 + np.pi*np.sin(i/10.0) * np.random.uniform(0.75,1) / 20.0)
# Create the canvas size of (500, 500)
im = Image.new("RGB", (500, 500), (1, 1, 1))
draw = ImageDraw.Draw(im, 'RGBA')
# Sphere's center is np.array([0,0,0])
# The vector that passes through the center is np.array([0,0,1])
# Radius is oscillating randomly: 1 * np.random.uniform(0.75,1) + 0.4 * np.sin(np.pi/10.0*i)
circle.draw_sphere(draw, np.array([0,0,0]), np.array([0,0,1]), 1 * np.random.uniform(0.75,1) + 0.4 * np.sin(np.pi/10.0*i), r, num_circle = number_of_circles, rgba=(182, 183, 186, 255), width = line_thickness)
file_name = save_dir + str(i) + '.png'
im.save(file_name)
def draw_wavy_sphere_wrapper(save_dir, number_of_circles, line_thickness):
# Create n images (frames) where n = number_of_circles
for i in np.arange(number_of_circles):
wavy_index = i % number_of_circles
# 2.52 is an angle of Z axis (Why do I choose 2.52? For an aesthetic reason:))
r = rotation.rotation(3, 2.50 + np.pi*np.sin(i/10.0) * np.random.uniform(0.8,1) / 30.0)
# Create the canvas size of (500, 500)
im = Image.new("RGB", (500, 500), (1, 1, 1))
draw = ImageDraw.Draw(im, 'RGBA')
# Sphere's center is np.array([0,0,0])
# The vector that passes through the center is np.array([0,0,1])
# Radius is oscillating randomly
circle.draw_sphere(draw, np.array([0,0,0]), np.array([0,0,1]), 1, r, wavy_index = wavy_index ,num_circle = number_of_circles, rgba=(182, 183, 186, 255), width = line_thickness)
file_name = save_dir + str(i) + '.png'
im.save(file_name)
def draw_wavy_sphere_acceleration_wrapper(save_dir, number_of_circles, line_thickness):
# Create n images (frames) where n = number_of_circles
for i in np.arange(number_of_circles):
wavy_index = i % number_of_circles
if (wavy_index > 0.65*number_of_circles and wavy_index%2!=0):
pass
else:
# 2.52 is an angle of Z axis (Why do I choose 2.52? For an aesthetic reason:))
r = rotation.rotation(3, 2.50 + np.pi*np.sin(i/10.0) * np.random.uniform(0.8,1) / 30.0)
# Create the canvas size of (500, 500)
im = Image.new("RGB", (500, 500), (1, 1, 1))
draw = ImageDraw.Draw(im, 'RGBA')
# Sphere's center is np.array([0,0,0])
# The vector that passes through the center is np.array([0,0,1])
# Radius is oscillating randomly
circle.draw_sphere2(draw, np.array([0,0,0]), np.array([0,0,1]), 1, r, wavy_index = wavy_index ,num_circle = number_of_circles, rgba=(182, 183, 186, 255), width = line_thickness)
file_name = save_dir + str(i) + '.png'
im.save(file_name)
if __name__ == "__main__":
# Create the directories to save image files
save_dir_list = ['Images\\RotatingSphereDense\\im',
'Images\\RotatingSphereLight\\im',
'Images\\RotatingOscillatingSphere\\im',
'Images\\WavySphere\\im',
'Images\\WavySphere_A\\im']
for path in save_dir_list:
if not os.path.exists(path):
os.makedirs(path)
# Dense sphere: Draw the sphere that consist of 40 circles with line thickness 1
draw_rotating_sphere('Images\\RotatingSphereDense\\im', 40, 1)
# Light sphere: Draw the sphere that consist of 20 circles with line thickness 1
draw_rotating_sphere('Images\\RotatingSphereLight\\im', 20, 1)
# Oscillating sphere: Draw the sphere that consist of n circles with line thickness 2
draw_oscillating_sphere('Images\\RotatingOscillatingSphere\\im', 20, 2)
# Wavy sphere
draw_wavy_sphere_wrapper('Images\\WavySphere\\im', 66, 1)
# Wavy sphere with acceleration
draw_wavy_sphere_acceleration_wrapper('Images\\WavySphere_A\\im', 66, 1)