root/spike/wktraster/scripts/genraster.py

Revision 5811, 2.7 KB (checked in by mloskot, 18 months ago)

[wktraster] genratser.py: encoding declaration missing and print OUTCLR.

  • Property svn:executable set to *
Line 
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 modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation; either version 3 of the License, or
18# (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
27# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28#
29###############################################################################
30import Image
31import ImageDraw
32import ImageFont
33import random
34import sys
35
36if 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
41g_file = "out.png"
42g_size = ( int(sys.argv[1]), int(sys.argv[2]) )
43g_cell_size = ( int(sys.argv[3]), int(sys.argv[4]) )
44if len(sys.argv) == 6:
45    g_outline = int(sys.argv[5])
46else:
47    g_outline = None
48
49ncells = (g_size[0] / g_cell_size[0]) * (g_size[1] / g_cell_size[1])
50print 'Number of cells: ',ncells
51print 'ID \tULX\tULY\tCLR\tTXTCLR\tOUTCLR'
52
53img = Image.new("L", g_size, 255)
54draw = ImageDraw.Draw(img)
55
56colour_step = 8
57count = 0
58for 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
81del draw
82img.save(g_file, 'PNG')
83print 'Output saved: %s' % g_file
Note: See TracBrowser for help on using the browser.