Commits

Anonymous committed 00865d2

Tag: version 0.2
New: faster writing of PIL image

  • Participants
  • Parent commits c47b1ff

Comments (0)

Files changed (5)

heightfield/__init__.py

-__version__ = (0, 1)
+__version__ = (0, 2)

heightfield/deposition.py

     (30, 0.1, 2000),
 ]
 
+CONTINENTS = [
+    (500, 2, 6),
+    (200, 0.5, 15),
+    (70, 0.1, 300),
+    (30, 0.04, 2000),
+]
 
 def deposit(landscape, octaves, base_height=0, progress_callback=None):
     w = h = landscape.size

heightfield/main.py

 import sys
 from .surface import Surface
-from .deposition import deposit, ISLANDS
+from .deposition import deposit, ISLANDS, CONTINENTAL, CONTINENTS
 
 
 # default heightfield size
     else:
         callback = get_cli_callback()
 
-    deposit(landscape, ISLANDS, progress_callback=callback)
+    deposit(landscape, CONTINENTS, progress_callback=callback)
 
     if options.output:
         print "Writing %s..." % options.output

heightfield/surface.py

 import math
 from numpy import zeros
-from PIL import Image
 from pkg_resources import resource_stream
 
-colours = Image.open(resource_stream(__name__, 'data/heightmap.png'))
 
 
 class Surface(object):
         self.dirty = True
 
     def to_pil(self):
+        from PIL import Image
+        colours = Image.open(resource_stream(__name__, 'data/heightmap.png'))
+        colormap = list(colours.getdata())
+        out = []
         im = Image.new(colours.mode, (self.size, self.size))
-        for y in xrange(self.size):
-            for x in xrange(self.size):
-                h = self[x, y]
-                val = max(0, min(1.0, h * 0.1) * 255)
-                col = colours.getpixel((int(val), 0))
-                im.putpixel((x, y), col)
+        for h in self.surface.flat:
+            val = max(0, min(1.0, h * 0.1) * 255)
+            out.append(colormap[int(val)])
+        im.putdata(out)
         return im
 from heightfield import __version__
 version = '.'.join([str(component) for component in __version__])
 
+
 setup(
     name='heightfield',
     version=version,