-
Notifications
You must be signed in to change notification settings - Fork 1
/
images_in_poly.py
214 lines (186 loc) · 6.77 KB
/
images_in_poly.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# summary TODO
import os, sys, datetime
import argparse
import shutil
import json
from dateutil.tz import tzlocal
from lib.exif_read import ExifRead as EXIF
from lib.exif_write import ExifEdit
from shapely.geometry import Point, GeometryCollection, shape
def list_images(directory):
"""
Create a list of image tuples sorted by capture timestamp.
@param directory: directory with JPEG files
@return: a list of image tuples with time, directory, lat,long...
"""
file_list = []
for root, sub_folders, files in os.walk(directory):
file_list += [
os.path.join(root, filename)
for filename in files
if filename.lower().endswith(".jpg")
]
files = []
# get DateTimeOriginal data from the images and sort the list by timestamp
for filepath in file_list:
metadata = EXIF(filepath)
# metadata.read()
try:
t = metadata.extract_capture_time()
geo = metadata.extract_geo()
files.append((filepath, t, Point(geo["longitude"], geo["latitude"])))
except KeyError as e:
# if any of the required tags are not set the image is not added to the list
print("Skipping {0}: - {1} is missing".format(filepath, e))
files.sort(key=lambda timestamp: timestamp[1])
# print_list(files)
return files
def import_geojson(geojson_file, properties_key):
"""
import the geojson file and create a dict with the commune's name as the key
and a shape object as the value
"""
with open(geojson_file) as f:
features = json.load(f)["features"]
GeometryCollection([shape(feature["geometry"]).buffer(0) for feature in features])
areas = {}
for feature in features:
area_name = feature["properties"][properties_key]
area_shape = shape(feature["geometry"])
areas[area_name] = area_shape
return areas
def check_point_in_polygon(point, area_shape):
return area_shape.contains(point)
def find_polygon(point, area_shapes, first_check=None):
"""
find outer polygon for each image (store the previous correct polygon to speed up calculation
time as the next image will probably be in the same one)
"""
if first_check is not None:
if check_point_in_polygon(point, area_shapes[first_check]):
return first_check
for area in area_shapes:
if check_point_in_polygon(point, area_shapes[area]):
return area
def write_exif(image_path, value):
#add_custom_tag(self, value, main_key, tag_key)
#self._ef[main_key][tag_key] = value
#self._ef["IPTC"]["City"] = city
metadata = ExifEdit(image_path)
metadata.add_gpsareainformation(str(value))
metadata.write()
def file_to_destination(image_path, source_directory, destination_directory, move=False):
"""
Copy/move image to the destination directory, keeping the subfolder path
"""
relative_path = os.path.relpath(image_path, source_directory)
full_destination_path = os.path.join(destination_directory, relative_path)
os.makedirs(
os.path.join(destination_directory, os.path.dirname(relative_path)),
exist_ok=True,
)
if os.path.abspath(image_path) == os.path.abspath(full_destination_path):
# source and destination are identical
pass
elif move == True:
shutil.move(image_path, full_destination_path)
elif move == False:
shutil.copy2(image_path, full_destination_path)
return full_destination_path
def arg_parse():
"""Parse the command line
options :
json_file, source, destination
"""
parser = argparse.ArgumentParser(
description="Script to find in which polygon a geolocalized image belongs to"
)
parser.add_argument(
"-j", "--json_file", help="Path to the geojson file", required=True
)
parser.add_argument(
"-p",
"--properties",
help="Geojson properties key used to name the subfolders",
required=True,
)
parser.add_argument(
"-s",
"--source",
help="Path to the images folder. Sub folder are scanned too",
required=True,
)
parser.add_argument(
"-a",
"--copy_orphan",
help="Copy images located outside of any polygon to the destination/no_area directory",
action="store_true",
default=False
)
parser.add_argument(
"-d",
"--destination",
help="Path to the destination folder in which the images will be copied",
)
parser.add_argument(
"-m",
"--move",
help="Move files to destination instead of copy files",
action="store_true",
)
parser.add_argument(
"-w",
"--write_tag",
help="write area name inside image exif metadata (GpsAreaInformation)",
action="store_true",
default=False
)
parser.add_argument(
"-q",
"--quiet",
help="Don't display images results",
action="store_true",
)
parser.add_argument("-v", "--version", action="version", version="release 1.2")
args = parser.parse_args()
return args
def main():
print("image source path is:", args.source)
print("importing geojson: ", args.json_file, end=" - ")
area_dict = import_geojson(args.json_file, args.properties)
print("{} polygons loaded".format(len(area_dict)))
# create image list with point position for shapely
print("creating image list...", end=" ")
images_list = list_images(args.source)
print("{} images found".format(len(images_list)))
# find outer polygon for each image
print("searching for city...")
previous_area = None
image_count = 0
used_area = {}
for image in images_list:
area = find_polygon(image[2], area_dict, previous_area)
if not area and not args.copy_orphan:
#image outside of polygons and copy orphan is False
continue
used_area[area] = used_area.get(area, 0) + 1
if area is not None:
previous_area = area
if not args.quiet:
print("{} -> {}".format(image[0], area))
if args.destination:
# copy/move images to a new directory named with the city name
new_path = file_to_destination(
image[0], args.source, os.path.join(args.destination, str(area or "no_area")), args.move)
image_count += 1
if args.write_tag and area is not None:
write_exif(new_path, area)
print("{} images {} to {} directory : {}".format(image_count, "copied" if args.move == False else "moved", len(used_area), used_area))
print("End of Script")
if __name__ == "__main__":
# Parsing the command line arguments
args = arg_parse()
print(args)
main()