-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeophoto.py
144 lines (127 loc) · 3.57 KB
/
geophoto.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import sys
import json
from argparse import ArgumentParser
from shutil import copy
from pathlib import Path
from tqdm import tqdm
from PIL import Image
import coordlib
import imglib
def iterate_files(source_folder, extensions):
for file in os.listdir(source_folder):
basename, ext = os.path.splitext(file)
ext = ext.lower()
if ext in extensions:
yield Path(file)
def img_factory(img):
return {
"type": "Feature",
"id": img["id"],
"geometry": {
"type": "Point",
"coordinates": img["coordinates"],
},
"properties": {
"src": str(img["src"]),
"url": str(img["url"]),
"value": img["value"],
},
}
def create_geojson(images):
return {
"type": "FeatureCollection",
"features": [
img_factory(img)
for img in images
],
}
def get_args():
parser = ArgumentParser()
parser.add_argument(
"-d",
required=True,
dest="source_dir",
type=str,
help="path to folder with images",
)
parser.add_argument(
"-o",
"--output",
required=True,
dest="output_dir",
type=str,
help="path to output folder",
)
# parser.add_argument('-o', '--output', default='dataset.geojson', dest='output', type=str, help='name of output file')
parser.add_argument(
"--size",
default=100,
dest="thumbnail_size",
type=int,
help="size of thumbnail in px",
)
parser.add_argument(
"--thumbnail",
default="thumbnail",
dest="thumbnail_field",
type=str,
help="field name for thumbnail",
)
parser.add_argument(
"--url-base",
default="",
dest="url_base",
type=str,
help="url base for thumbnail",
)
parser.add_argument(
"--default-coord",
default=None,
dest="default_coord",
nargs="+",
type=float,
help="default GPS coordinate for image",
)
return parser.parse_args()
if __name__ == "__main__":
args = get_args()
source_folder = Path(args.source_dir).expanduser()
destination_folder = Path(args.output_dir).expanduser()
url_base = args.url_base
size = (args.thumbnail_size, args.thumbnail_size)
if not destination_folder.is_dir():
os.mkdir(destination_folder)
images = []
counter = 0
files = list(iterate_files(source_folder, [".jpg", ".jpeg"]))
for file in tqdm(files):
filepath = source_folder / file
image = Image.open(filepath)
image.load()
exif = imglib.get_exif(image)
geotags = imglib.get_geo_info(exif)
if not geotags and not args.default_coord:
continue
coordinates = args.default_coord
if geotags:
coordinates = coordlib.get_coordinates(geotags)
name = Path(f"{file.stem}-thumbnail.jpg")
path = os.path.join(destination_folder, name)
img = imglib.process_image(image, size)
img.save(path)
copy(filepath, destination_folder / file)
images.append(
{
"id": counter,
"url": url_base + str(name),
"src": url_base + str(file),
"coordinates": coordinates,
"value": 1,
}
)
counter += 1
features = create_geojson(images)
filename = destination_folder / Path("data.geojson")
with open(filename, "w") as f:
json.dump(features, f, indent=4)