PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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
29from __future__ import print_function
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)
#define str(s)