{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import noise\n", "import numpy as np\n", "from PIL import Image\n", "#import scipy.misc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# perlin noise" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def rgb_norm(world):\n", " world_min = np.min(world)\n", " world_max = np.max(world)\n", " norm = lambda x: (x-world_min/(world_max - world_min))*255\n", " return np.vectorize(norm)\n", "\n", "def prep_world(world):\n", " norm = rgb_norm(world)\n", " world = norm(world)\n", " return world" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "shape = (1024,1024)\n", "scale = 100\n", "octaves = 6\n", "persistence = 0.5\n", "lacunarity = 2.0\n", "seed = np.random.randint(0,100)\n", "seed = 126\n", "\n", "world = np.zeros(shape)\n", "for i in range(shape[0]):\n", " for j in range(shape[1]):\n", " world[i][j] = noise.pnoise2(i/scale, \n", " j/scale, \n", " octaves=octaves, \n", " persistence=persistence, \n", " lacunarity=lacunarity, \n", " repeatx=1024, \n", " repeaty=1024, \n", " base=seed)\n", "\n", "Image.fromarray(prep_world(world)).show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# adding color" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "blue = [65,105,225]\n", "green = [34,139,34]\n", "beach = [238, 214, 175]\n", "snow = [255, 250, 250]\n", "mountain = [139, 137, 137]\n", "\n", "def add_color(world):\n", " color_world = np.zeros(world.shape+(3,))\n", " for i in range(shape[0]):\n", " for j in range(shape[1]):\n", " if world[i][j] < -0.05:\n", " color_world[i][j] = blue\n", " elif world[i][j] < 0:\n", " color_world[i][j] = beach\n", " elif world[i][j] < .20:\n", " color_world[i][j] = green\n", " elif world[i][j] < 0.35:\n", " color_world[i][j] = mountain\n", " elif world[i][j] < 1.0:\n", " color_world[i][j] = snow\n", "\n", " return color_world\n", "\n", "color_world = add_color(world).astype(np.uint8)\n", "#Image.fromarray(color_world,'RGB').show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# adding a circular filter" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "a,b = shape[0]/2, shape[1]/2\n", "n = 1024\n", "r = 125\n", "y,x = np.ogrid[-a:n-a, -b:n-b]\n", "# creates a mask with True False values\n", "# at indices\n", "mask = x**2+y**2 <= r**2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "black = [0, 0, 0]\n", "island_world = np.zeros_like(color_world)\n", "\n", "for i in range(shape[0]):\n", " for j in range(shape[1]):\n", " if mask[i][j]:\n", " island_world[i][j] = color_world[i][j]\n", " else:\n", " island_world[i][j] = black" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "#Image.fromarray(island_world,'RGB').show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# adding a circular gradient" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "center_x, center_y = shape[1] // 2, shape[0] // 2\n", "circle_grad = np.zeros_like(world)\n", "\n", "for y in range(world.shape[0]):\n", " for x in range(world.shape[1]):\n", " distx = abs(x - center_x)\n", " disty = abs(y - center_y)\n", " dist = math.sqrt(distx*distx + disty*disty)\n", " circle_grad[y][x] = dist\n", "\n", "# get it between -1 and 1\n", "max_grad = np.max(circle_grad)\n", "circle_grad = circle_grad / max_grad\n", "circle_grad -= 0.5\n", "circle_grad *= 2.0\n", "circle_grad = -circle_grad\n", "\n", "#Image.fromarray(prep_world(circle_grad)).show()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "world_noise = np.zeros_like(world)\n", "\n", "for i in range(shape[0]):\n", " for j in range(shape[1]):\n", " if circle_grad[i][j]>0:\n", " world_noise[i][j] = (world[i][j] * circle_grad[i][j])\n", "\n", "#Image.fromarray(prep_world(world_noise)).show()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "lightblue = [0,191,255]\n", "blue = [65,105,225]\n", "green = [34,139,34]\n", "darkgreen = [0,100,0]\n", "sandy = [210,180,140]\n", "beach = [238, 214, 175]\n", "snow = [255, 250, 250]\n", "mountain = [139, 137, 137]\n", "\n", "threshold = 50\n", "\n", "def add_color2(world):\n", " color_world = np.zeros(world.shape+(3,))\n", " for i in range(shape[0]):\n", " for j in range(shape[1]):\n", " if world[i][j] < threshold + 100:\n", " color_world[i][j] = blue\n", " elif world[i][j] < threshold + 102:\n", " color_world[i][j] = beach\n", " elif world[i][j] < threshold + 104:\n", " color_world[i][j] = sandy\n", " elif world[i][j] < threshold + 115:\n", " color_world[i][j] = green\n", " elif world[i][j] < threshold + 130:\n", " color_world[i][j] = darkgreen\n", " elif world[i][j] < threshold + 137:\n", " color_world[i][j] = mountain\n", " else:\n", " color_world[i][j] = snow\n", "\n", " return color_world\n", "\n", "island_world_grad = add_color2(prep_world(world_noise)).astype(np.uint8)\n", "Image.fromarray(island_world_grad,'RGB').show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.12" } }, "nbformat": 4, "nbformat_minor": 2 }