PostGIS  3.0.6dev-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 from __future__ import print_function
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)
#define str(s)