PostGIS  2.1.10dev-r@@SVN_REVISION@@
genraster.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # $Id$
5 #
6 # A very simple generator of rasters for testing WKT Raster.
7 # A test file is a matrix of numbered cells greyscaled randomly.
8 #
9 # Requirements: Python + Python Imaging Library (PIL)
10 # Also, path to FreeSans.ttf is a hardcoded Unix path, so fix it if you need.
11 #
12 ###############################################################################
13 # (C) 2009 Mateusz Loskot <mateusz@loskot.net>
14 #
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software Foundation,
27 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #
29 ###############################################################################
30 import Image
31 import ImageDraw
32 import ImageFont
33 import random
34 import sys
35 
36 if len(sys.argv) < 5 or len(sys.argv) > 6:
37  print 'Usage: genraster.py <xsize> <ysize> <xsizecell> <ysizecell> <outline colour>'
38  print 'Note: Generated image is saved as out.png'
39  sys.exit(1)
40 
41 g_file = "out.png"
42 g_size = ( int(sys.argv[1]), int(sys.argv[2]) )
43 g_cell_size = ( int(sys.argv[3]), int(sys.argv[4]) )
44 if len(sys.argv) == 6:
45  g_outline = int(sys.argv[5])
46 else:
47  g_outline = None
48 
49 ncells = (g_size[0] / g_cell_size[0]) * (g_size[1] / g_cell_size[1])
50 print 'Number of cells: ',ncells
51 print 'ID \tULX\tULY\tCLR\tTXTCLR\tOUTCLR'
52 
53 img = Image.new("L", g_size, 255)
54 draw = ImageDraw.Draw(img)
55 
56 colour_step = 8
57 count = 0
58 for j in range(0, g_size[1], g_cell_size[1]):
59  for i in range(0, g_size[0], g_cell_size[0]):
60 
61  if count < 256 / colour_step:
62  value = count * colour_step
63  else:
64  value = random.randrange(0, 255)
65 
66  if value < 16:
67  value_text = 255
68  else:
69  value_text = 0;
70 
71 
72  font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSans.ttf',
73  g_cell_size[1] - int(g_cell_size[1] * 0.4))
74 
75  draw.rectangle( [(i,j), (i + g_cell_size[0], j + g_cell_size[1])], fill=value, outline=g_outline)
76  draw.text( (i,j), ('%d' % count), fill=value_text, font=font)
77 
78  print '%d:\t%d\t%d\t%d\t%d\t%s' % (count, i, j, value, value_text, str(g_outline))
79  count = count + 1
80 
81 del draw
82 img.save(g_file, 'PNG')
83 print 'Output saved: %s' % g_file