def create_wrap_figures(): ground = snowy.load(qualify('ground.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('ground2x2.jpg')) ground = snowy.blur(ground, radius=14, filter=snowy.LANCZOS) snowy.export(ground, qualify('blurry_ground_bad.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(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.export(ground, qualify('blurry_ground_good.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(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.export(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.export(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.export(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.export(sdf, qualify('tiled_sdf_bad.png'))
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 setup(grayscale=False, imgfile='~/Desktop/SaltLakes.jpg'): print('Loading image...') global imgarray global pilimage imgarray = snowy.load(imgfile) if grayscale: assert imgarray.shape[2] == 3, "Not an RGB image." r, g, b = np.split(imgarray, 3, axis=2) imgarray = r pilimage = Image.fromarray(np.uint8(snowy.unshape(imgarray)))
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_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_draw_quad2(): target = np.full((1080, 1920, 4), (0, 0, 0, 0), dtype=np.float32) texture = snowy.load('tests/texture.png') # These are in NDC so they span -W to +W vertices = np.array([[-0.67608007, 0.38439575, 1.7601049, 3.70544936], [-0.10726266, 0.38439575, 0.60928749, 2.57742041], [-0.10726266, -0.96069041, 0.60928749, 2.57742041], [-0.67608007, -0.96069041, 1.7601049, 3.70544936]]) texcoords = np.array([[0., 0.], [1., 0.], [1., 1.], [0., 1.]]) x, y, w = vertices[:, 0], vertices[:, 1], vertices[:, 3] u, v = texcoords[:, 0], texcoords[:, 1] vertices = np.transpose(np.vstack([x, y, w, u, v])) print(vertices) snowy.draw_polygon(target, texture, vertices) overlay = snowy.load('tests/overlay.png') im = snowy.compose(target, overlay)[400:770, 600:900] target = snowy.resize(im, height=100) snowy.show(target)
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)
def test_thick(): source = sn.load('tests/sobel_input.png')[:, :, :3] small_source = sn.resize(source, width=256) blurred = sn.blur(source, radius=2) small_blurred = sn.resize(blurred, width=256) L = skimage_sobel(blurred) sksobel = np.dstack([L, L, L]) small_sksobel = sn.resize(sksobel, width=256) L = sn.rgb_to_luminance(blurred) L = sn.compute_sobel(L) snsobel = np.dstack([L, L, L]) small_snsobel = sn.resize(snsobel, width=256) small_sksobel = np.clip(1 - small_sksobel * 40, 0, 1) small_snsobel = np.clip(1 - small_snsobel * 40, 0, 1) strip = np.hstack([ small_blurred, small_source * small_sksobel, small_source * small_snsobel ]) sn.show(strip)
def test_luminance(): source = sn.load('tests/sobel_input.png')[:, :, :3] L = rgb2gray(source) skresult = np.dstack([L, L, L]) small_skresult = sn.resize(skresult, width=256) L = sn.rgb_to_luminance(source) snresult = np.dstack([L, L, L]) small_snresult = sn.resize(snresult, width=256) L = skimage_sobel(source) sksobel = np.dstack([L, L, L]) small_sksobel = sn.resize(sksobel, width=256) L = sn.rgb_to_luminance(source) L = sn.compute_sobel(L) snsobel = np.dstack([L, L, L]) small_snsobel = sn.resize(snsobel, width=256) sn.show( np.hstack( [small_skresult, small_snresult, small_sksobel, small_snsobel]))
headings = soup.find_all(tag) for heading in headings: content = heading.contents[0].strip() id = content.replace(' ', '_').lower() heading["id"] = id anchor = soup.new_tag('a', href='#' + id) anchor.string = content 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)
# 4. Generate normal map. normals = snowy.resize(snowy.compute_normals(elevation), width, height) snowy.show(0.5 + 0.5 * normals) # 5. Apply harsh diffuse lighting. lightdir = np.float64([0.2, -0.2, 1]) lightdir /= np.linalg.norm(lightdir) lambert = np.sum(normals * lightdir, 2) snowy.show(snowy.reshape(lambert) * occlusion) # 6. Lighten the occlusion, flatten the normals, and re-light. occlusion = 0.5 + 0.5 * occlusion normals += np.float64([0, 0, 0.5]) normals /= snowy.reshape(np.sqrt(np.sum(normals * normals, 2))) lambert = np.sum(normals * lightdir, 2) lighting = snowy.reshape(lambert) * occlusion snowy.show(lighting) # 7. Apply color gradient. xvals = np.arange(256) yvals = snowy.load('tests/terrain.png')[0, :, :3] apply_lut = interpolate.interp1d(xvals, yvals, axis=0) el = elevation * 0.2 + 0.49 el = np.clip(255 * el, 0, 255) albedo = apply_lut(snowy.unshape(el)) snowy.show(albedo * lighting)
import snowy import os import re HEIGHT = 2250 WIDTH = 3000 def func(arg): pattern = r'(.*\.jpg)$' return re.match(pattern, string=arg) print(func('123.jpg')) path = os.path.abspath(u'D:/周日敬拜/2017-9-24') files = os.listdir(path) imagefile_paths = [os.path.join(path, file).lower() for file in files] print(imagefile_paths) for file in imagefile_paths: source = snowy.load(file) height, width = source.shape[:2] if (height != HEIGHT or width != WIDTH): new_imagine = snowy.resize(source, width=WIDTH, height=HEIGHT) snowy.export(new_imagine, file) # source = snowy.load(imagefile_paths[0]) # height, width = source.shape[:2] # newImagine = snowy.resize(source, width=100, height=100) # snowy.export(newImagine, os.path.join(path, '8.jpg')) # print(height, width)
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 apply_lut = interpolate.interp1d(np.arange(len(yvals)), yvals, axis=0) el = elevation * 0.2 + 0.49 el = np.clip(255 * el, 0, 255) albedo = apply_lut(snowy.unshape(el)) print("Saving to disk.") final = albedo * lighting snowy.save(trim(final), "terrain.png")
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]))