def test_gamma(): source = path('gamma_dalai_lama_gray.jpg') dalai_lama = snowy.load(source) snowy.show(dalai_lama) small = snowy.resize(dalai_lama, height=32) snowy.save(small, path('small_dalai_lama.png')) snowy.show(small)
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_range(): source = path('../docs/ground.jpg') ground = snowy.load(source) assert np.amin(ground) >= 0 and np.amax(ground) <= 1 with tempfile.NamedTemporaryFile() as fp: target = fp.name + '.png' snowy.save(ground, target) show_filename(target) show_filename(source) show_array(ground) blurred = snowy.blur(ground, radius=10) snowy.show(blurred)
mask = warped < 0.1 print("Computing the distance field.") elevation = snowy.generate_sdf(mask) elevation /= np.amax(elevation) print("Computing ambient occlusion.") occlusion = snowy.compute_skylight(elevation) occlusion = 0.25 + 0.75 * occlusion print("Generating normal map.") normals = snowy.resize(snowy.compute_normals(elevation), width, height) # Save the landmass portion of the elevation data. landmass = elevation * np.where(elevation < 0.0, 0.0, 1.0) snowy.save(trim(landmass), "landmass.png") # Flatten the normals according to landmass versus sea. normals += np.float64([0, 0, 1000]) * np.where(elevation < 0.0, 1.0, 0.01) normals /= snowy.reshape(np.sqrt(np.sum(normals * normals, 2))) print("Applying diffuse lighting.") lightdir = np.float64([0.5, -0.5, 1]) lightdir /= np.linalg.norm(lightdir) lambert = np.sum(normals * lightdir, 2) lighting = snowy.reshape(lambert) * occlusion print("Applying color gradient.") yvals = snowy.load("gradient.png")[0, :, :3] water_color = np.copy(yvals[126]) yvals[0:128] = water_color
def create_wrap_figures(): ground = snowy.load(qualify('ground.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.save(ground2x2, qualify('ground2x2.jpg')) ground = snowy.blur(ground, radius=14, filter=snowy.LANCZOS) snowy.save(ground, qualify('blurry_ground_bad.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.save(ground2x2, qualify('blurry_ground2x2_bad.jpg')) ground = snowy.load(qualify('ground.jpg')) ground = snowy.blur(ground, radius=14, wrapx=True, wrapy=True, filter=snowy.LANCZOS) snowy.save(ground, qualify('blurry_ground_good.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.save(ground2x2, qualify('blurry_ground2x2_good.jpg')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=False) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.save(n, qualify('tiled_noise_bad.png')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.save(n, qualify('tiled_noise_good.png')) c0 = create_circle(400, 200, 0.3) c1 = create_circle(400, 200, 0.08, 0.8, 0.8) circles = np.clip(c0 + c1, 0, 1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=True, wrapy=True)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.save(sdf, qualify('tiled_sdf_good.png')) sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=False, wrapy=False)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.save(sdf, qualify('tiled_sdf_bad.png'))
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.save( 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.save(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)
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]))