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( 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) 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_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 did I choose 2.52? For aesthetic reasons:)) r = 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 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)
def draw_rotating_sphere(number_of_circles, line_thickness, save_dir=None, is_stream=False): if not is_stream and save_dir is None: raise Exception("Save directory required when not streaming") # Create total 30 images for i in np.arange(30): # We'll rotate the sphere by 18 degrees (18 = pi/10) r = 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') 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) if is_stream: yield get_image_bytes(im) else: file_name = save_dir + str(i) + '.png' im.save(file_name)
async def stream(): number_of_circles = 20 line_thickness = 2 for i in np.arange(60): r = rotation( 3, 2.5 + np.pi * np.sin(i / 10.0) * np.random.uniform(0.75, 1) / 20.0 ) im = Image.new("RGB", (500, 500), (1, 1, 1)) draw = ImageDraw.Draw(im, "RGBA") 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, ) img_bytes = io.BytesIO() im.save(img_bytes, format="PNG") yield b"".join( [ b"--frame\r\n", b"Content-Type: image/jpeg\r\n\r\n", img_bytes.getvalue(), b"\r\n", ] )
async def websocket_connect(self, message): await self.send({"type": "websocket.accept"}) number_of_circles = 20 line_thickness = 2 for i in np.arange(60): r = rotation( 3, 2.5 + np.pi * np.sin(i / 10.0) * np.random.uniform(0.75, 1) / 20.0 ) im = Image.new("RGB", (500, 500), (1, 1, 1)) draw = ImageDraw.Draw(im, "RGBA") 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, ) img_bytes = io.BytesIO() im.save(img_bytes, format="PNG") await asyncio.sleep(0.1) await self.send({"type": "websocket.send", "bytes": img_bytes.getvalue()}) await self.send({"type": "websocket.close", "code": 1000})
def test_rotation_360(): """ Rotating by 360 degrees should preserve the vector. """ rot_matrix = rotation(3, np.pi * 2) vec = np.random.uniform(size=3) vec_rot = np.dot(rot_matrix, vec) assert sum(abs(vec - vec_rot) / vec) < 1e-5
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(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') 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)