-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathvectorize.py
84 lines (68 loc) · 2.27 KB
/
vectorize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
This file contains code copied and modified from 'beachfront-py', which is
under the following license:
---
beachfront-py
https://github.com/venicegeo/beachfront-py
Copyright 2016, RadiantBlue Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import numpy
import potrace as _potrace
from PIL import Image
import click
import json
def lines_to_features(lines, source='imagery'):
""" Create features from lines """
gid = 0
features = []
for line in lines:
feature = {
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [line]
},
'properties': {
'id': gid,
'source': source
}
}
features.append(feature)
gid += 1
return features
def to_geojson(lines, source='imagery'):
geojson = {
'type': 'FeatureCollection',
'features': lines_to_features(lines, source=source),
}
return geojson
def potrace_array(arr, turdsize=10.0, tolerance=0.2):
""" Trace numpy array using potrace """
bmp = _potrace.Bitmap(arr)
polines = bmp.trace(turdsize=turdsize, turnpolicy=_potrace.TURNPOLICY_WHITE,
alphamax=0.0, opticurve=1.0, opttolerance=tolerance)
lines = []
for line in polines:
lines.append(line.tesselate().tolist())
return lines
def vectorize(img_file, turdsize=10.0, tolerance=0.2):
img = Image.open(img_file)
arr = numpy.asarray(img)
arr = numpy.any(arr[:, :, :-1], axis=2)
lines = potrace_array(arr, turdsize, tolerance)
return to_geojson(lines)
@click.command()
@click.argument('img_file', default='-')
def vectorize_cmd(img_file):
click.echo(json.dumps(vectorize(img_file)))
if __name__ == '__main__':
vectorize_cmd()