def test_coords(): h, w = 800, 800 height, width = h, w # Draw seed image cyan = np.full([h, w, 3], np.float64([(27, 56, 80)]) / 200) pink = np.full([h, w, 3], np.float64([175, 111, 127]) / 255) orange = np.full([h, w, 3], np.float64([239, 159, 95]) / 255) yellow = np.full([h, w, 3], np.float64([239, 207, 95]) / 255) colors = np.zeros([h, w, 3]) def max_color(v): return np.maximum(colors, v) def sub_color(v): return colors * (1 - v) colors = max_color(create_circle(w, h, 0.37, [0.4, 0.5]) * cyan) colors = max_color(create_circle(w, h, 0.37, [0.6, 0.4]) * cyan) colors = max_color(create_circle(w, h, 0.27, [0.7, 0.6]) * cyan) colors = sub_color(create_circle(w, h, 0.35, [0.4, 0.5])) colors = sub_color(create_circle(w, h, 0.35, [0.6, 0.4])) colors = sub_color(create_circle(w, h, 0.25, [0.7, 0.6])) colors = max_color(create_circle(w, h, 0.01, [0.4, 0.5]) * orange) colors = max_color(create_circle(w, h, 0.01, [0.6, 0.4]) * pink) colors = max_color(create_circle(w, h, 0.01, [0.7, 0.6]) * yellow) colors = sn.linearize(colors) # Create generalized voronoi luma = sn.reshape(np.sum(colors, 2)) coords = sn.generate_cpcf(luma != 0) voronoi = sn.dereference_cpcf(colors, coords) # Warp the voronoi warpx, warpy = width / 15, height / 15 noise = sn.generate_fBm(width, height, 4, 4, 3) i, j = np.arange(width, dtype='i2'), np.arange(height, dtype='i2') coords = np.dstack(np.meshgrid(i, j, sparse=False)) warpx = warpx * np.cos(noise * math.pi * 2) warpy = warpy * np.sin(noise * math.pi * 2) coords += np.int16(np.dstack([warpx, warpy])) coords[:, :, 0] = np.clip(coords[:, :, 0], 0, width - 1) coords[:, :, 1] = np.clip(coords[:, :, 1], 0, height - 1) warped = sn.dereference_cpcf(voronoi, coords) strip = [sn.resize(i, height=256) for i in (colors, voronoi, warped)] sn.show(sn.hstack(strip))
def test_udf(): c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) mask = circles != 0.0 udf = snowy.unitize(snowy.generate_udf(mask)) nx, ny = snowy.gradient(udf) grad = snowy.unitize(nx + ny) snowy.show(snowy.hstack([circles, udf, grad]))
def draw_quad(): verts = np.array([[-0.67608007, 0.38439575, 3.70544936, 0., 0. ], [-0.10726266, 0.38439575, 2.57742041, 1., 0. ], [-0.10726266, -0.96069041, 2.57742041, 1., 1. ], [-0.67608007, -0.96069041, 3.70544936, 0., 1. ]]) texture = snowy.load(qualify('../tests/texture.png')) target = np.full((1080, 1920, 4), (0.54, 0.54, 0.78, 1.00), dtype=np.float32) snowy.draw_polygon(target, texture, verts) target = snowy.resize(target[400:770, 700:1000], height = 256) texture = snowy.resize(texture, height = 256) quad = snowy.hstack([texture, target]) snowy.export(quad, qualify('quad.png')) snowy.show(quad)
def test_gdf(): "This is a (failed) effort to create a smoother distance field." c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) circles = np.clip(snowy.blur(circles, radius=2), 0, 1) circles = np.clip(snowy.blur(circles, radius=2), 0, 1) source = (1.0 - circles) * 2000.0 gdf = np.sqrt(snowy.generate_gdf(source)) gdf = snowy.unitize(gdf) nx, ny = snowy.gradient(gdf) grad = snowy.unitize(nx + ny) snowy.show(snowy.hstack([circles, gdf, grad]))
def test_normals(): isle = create_island(10) height, width, nchan = isle.shape occlusion = np.empty([height, width, 1]) seconds = timeit.timeit( lambda: np.copyto(occlusion, sn.compute_skylight(isle)), number=1) print(f'\ncompute_skylight took {seconds} seconds') normals = np.empty([height - 1, width - 1, 3]) seconds = timeit.timeit( lambda: np.copyto(normals, sn.compute_normals(isle)), number=1) print(f'\ncompute_normals took {seconds} seconds') normals = sn.resize(normals, 750, 512) # Flatten the normals according to landmass versus sea. normals += np.float64([0, 0, 100]) * np.where(isle < 0.0, 1.0, 0.005) normals /= sn.reshape(np.sqrt(np.sum(normals * normals, 2))) # Compute the lambertian diffuse factor lightdir = np.float64([0.2, -0.2, 1]) lightdir /= np.linalg.norm(lightdir) df = np.clip(np.sum(normals * lightdir, 2), 0, 1) df = sn.reshape(df) df *= occlusion # Apply color LUT gradient_image = sn.resize(sn.load(path('terrain.png')), width=1024)[:, :, :3] def applyColorGradient(elevation): xvals = np.arange(1024) yvals = gradient_image[0] apply_lut = interpolate.interp1d(xvals, yvals, axis=0) el = np.clip(1023 * elevation, 0, 1023) return apply_lut(sn.unshape(el)) albedo = applyColorGradient(isle * 0.5 + 0.5) albedo *= df # Visualize the lighting layers normals = 0.5 * (normals + 1.0) isle = np.dstack([isle, isle, isle]) occlusion = np.dstack([occlusion, occlusion, occlusion]) df = np.dstack([df, df, df]) island_strip = sn.resize(sn.hstack([occlusion, normals, df, albedo]), height=256) sn.save(island_strip, 'docs/island_strip.png') sn.show(island_strip)
def test_tileable(): n = snowy.generate_noise(200, 400, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) gold = snowy.resize(n, 200, 200) n = snowy.generate_noise(20, 40, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = snowy.resize(n, 100, 200) bad = np.hstack([n, n]) n = snowy.generate_noise(20, 40, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = snowy.resize(n, 100, 200, wrapx=True) good = np.hstack([n, n]) snowy.show(snowy.hstack([gold, bad, good], 2, .7))
heading.contents[0].replace_with(anchor) open(resultfile, 'w').write(str(soup)) generate_page(qualify('index.md'), qualify('index.html'), False) generate_page(qualify('reference.md'), qualify('reference.html'), True) # Test rotations and flips gibbons = snowy.load(qualify('gibbons.jpg')) gibbons = snowy.resize(gibbons, width=gibbons.shape[1] // 5) gibbons90 = snowy.rotate(gibbons, 90) gibbons180 = snowy.rotate(gibbons, 180) gibbons270 = snowy.rotate(gibbons, 270) hflipped = snowy.hflip(gibbons) vflipped = snowy.vflip(gibbons) snowy.export(snowy.hstack([gibbons, gibbons180, vflipped], border_width=4, border_value=[0.5,0,0]), qualify("xforms.png")) # Test noise generation n = snowy.generate_noise(100, 100, frequency=4, seed=42, wrapx=True) n = np.hstack([n, n]) n = 0.5 + 0.5 * n snowy.show(n) snowy.export(n, qualify('noise.png')) # First try minifying grayscale gibbons = snowy.load(qualify('snowy.jpg')) gibbons = np.swapaxes(gibbons, 0, 2) gibbons = np.swapaxes(gibbons[0], 0, 1) gibbons = snowy.reshape(gibbons)
import snowy source = snowy.open('poodle.png') source = snowy.resize(source, height=200) blurry = snowy.blur(source, radius=4.0) snowy.save(snowy.hstack([source, blurry]), 'diptych.png') # This snippet does a resize, then a blur, then horizontally concatenates the two images parrot = snowy.load('parrot.png') height, width = parrot.shape[:2] nearest = snowy.resize(parrot, width * 6, filter=snowy.NEAREST) mitchell = snowy.resize(parrot, width * 6) snowy.show(snowy.hstack([nearest, mitchell])) # This snippet first magnifies an image using a nearest-neighbor filter, then using the default Mitchell filter. parrot = snowy.load('parrot.png') height, width = parrot.shape[:2] nearest = snowy.resize(parrot, width * 6, filter=snowy.NEAREST) mitchell = snowy.resize(parrot, width * 6) snowy.show(snowy.hstack([nearest, mitchell]))
friction = 0.1 clumpy(f'pendulum_phase {dim} {friction} 1 5 field.npy') clumpy(f'bridson_points {dim} {spacing} 0 pts.npy') clumpy('advect_points pts.npy field.npy ' + f'{step_size} {kernel_size} {decay} {nframes} anim1.npy') friction = 0.9 clumpy(f'pendulum_phase {dim} {friction} 1 5 field.npy') clumpy(f'bridson_points {dim} {spacing} 0 pts.npy') clumpy('advect_points pts.npy field.npy ' + f'{step_size} {kernel_size} {decay} {nframes} anim2.npy') import imageio writer = imageio.get_writer('anim.mp4', fps=60) for i in tqdm(range(0, nframes, skip)): im1 = snowy.reshape(np.load("{:03}anim1.npy".format(i))) im1 = snowy.resize(im1, 960 - 6, 1088 - 8) im2 = snowy.reshape(np.load("{:03}anim2.npy".format(i))) im2 = snowy.resize(im2, 960 - 6, 1088 - 8) im = np.uint8(255.0 - snowy.hstack([im1, im2], border_width=4)) writer.append_data(im) writer.close() print('Generated anim.mp4') system('rm *.npy')