【发布时间】:2019-10-12 05:26:33
【问题描述】:
我正在关注 this tutorial 并且不得不更新它以使用 PIL 的 Image.fromarray 而不是 scipy 的 toimage。当我运行这段代码(一个小的 64^2 而不是整个 1024^2)时,输出似乎有连续颜色的列。即使在我应该得到一个漂亮的黑色和白色斑点的第一个代码集中,它看起来也像重复的静态立方体列。
这是为什么呢?
import noise
import numpy as np
from PIL import Image
shape = (64,64)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0
world = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
world[i][j] = noise.pnoise2(j/scale,
i/scale,
octaves = octaves,
persistence = persistence,
lacunarity = lacunarity,
repeatx = 1024,
repeaty = 1024,
base = 0)
blue = [65,105,225]
green = [34,139,34]
beach = [238, 214, 175]
def add_color(arr):
color_world = np.zeros(arr.shape+(3,))
for i in range(shape[0]):
for j in range(shape[1]):
if arr[i][j] < -0.05:
color_world[i][j] = blue
elif arr[i][j] < 0:
color_world[i][j] = beach
elif arr[i][j] < 1.0:
color_world[i][j] = green
return color_world
color_world = add_color(world)
im = Image.fromarray(color_world, "RGB")
im.show()
【问题讨论】:
标签: python python-3.x python-imaging-library noise perlin-noise