PostGIS  2.3.8dev-r@@SVN_REVISION@@
genraster.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 #
5 # A very simple generator of rasters for testing WKT Raster.
6 # A test file is a matrix of numbered cells greyscaled randomly.
7 #
8 # Requirements: Python + Python Imaging Library (PIL)
9 # Also, path to FreeSans.ttf is a hardcoded Unix path, so fix it if you need.
10 #
11 
29 import Image
30 import ImageDraw
31 import ImageFont
32 import random
33 import sys
34 
35 if len(sys.argv) < 5 or len(sys.argv) > 6:
36  print 'Usage: genraster.py <xsize> <ysize> <xsizecell> <ysizecell> <outline colour>'
37  print 'Note: Generated image is saved as out.png'
38  sys.exit(1)
39 
40 g_file = "out.png"
41 g_size = ( int(sys.argv[1]), int(sys.argv[2]) )
42 g_cell_size = ( int(sys.argv[3]), int(sys.argv[4]) )
43 if len(sys.argv) == 6:
44  g_outline = int(sys.argv[5])
45 else:
46  g_outline = None
47 
48 ncells = (g_size[0] / g_cell_size[0]) * (g_size[1] / g_cell_size[1])
49 print 'Number of cells: ',ncells
50 print 'ID \tULX\tULY\tCLR\tTXTCLR\tOUTCLR'
51 
52 img = Image.new("L", g_size, 255)
53 draw = ImageDraw.Draw(img)
54 
55 colour_step = 8
56 count = 0
57 for j in range(0, g_size[1], g_cell_size[1]):
58  for i in range(0, g_size[0], g_cell_size[0]):
59 
60  if count < 256 / colour_step:
61  value = count * colour_step
62  else:
63  value = random.randrange(0, 255)
64 
65  if value < 16:
66  value_text = 255
67  else:
68  value_text = 0;
69 
70 
71  font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSans.ttf',
72  g_cell_size[1] - int(g_cell_size[1] * 0.4))
73 
74  draw.rectangle( [(i,j), (i + g_cell_size[0], j + g_cell_size[1])], fill=value, outline=g_outline)
75  draw.text( (i,j), ('%d' % count), fill=value_text, font=font)
76 
77  print '%d:\t%d\t%d\t%d\t%d\t%s' % (count, i, j, value, value_text, str(g_outline))
78  count = count + 1
79 
80 del draw
81 img.save(g_file, 'PNG')
82 print 'Output saved: %s' % g_file