-
Notifications
You must be signed in to change notification settings - Fork 3
/
crop_post_event.py
46 lines (41 loc) · 1.47 KB
/
crop_post_event.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
"""
created by Daniel Cao
crop images with specific pixel size from tiff based on the center points of the damage annotated in tomnod
"""
import gdal
import pandas as pd
import os
SIZE = 0.000897575 # used to get exactly 400x400 images (maybe???) hyak: 0.000574448
COUNT = 0
FAIL = 0
COOR_TIF_POST = 'coordinateAndTif-post-3.csv'
CROP_POST_400 = 'cropped-post-400/'
coordinate_tif_3 = pd.read_csv(COOR_TIF_POST)
for index, row in coordinate_tif_3.iterrows():
if row['label'] == 'Flooded / Damaged Building':
folder = row['complete_catalog_id']
tif = row['tif']
xmin = row['tomnod_x'] - SIZE
xmax = row['tomnod_x'] + SIZE
ymin = row['tomnod_y'] - SIZE
ymax = row['tomnod_y'] + SIZE
file_name_to_write = str(row['tomnod_x'])+'_'+str(row['tomnod_y'])+'.jpeg'
print('{} {}'.format(folder, tif))
try:
ds = gdal.Open(''+folder+'/'+tif)
os.chdir(CROP_POST_400)
try:
gdal.Translate(file_name_to_write, ds,
projWin=[xmin, ymin + 2 * SIZE, xmax, ymax - 2 * SIZE], format='jpeg')
except:
print("Crop failure")
pass
os.chdir('../')
COUNT += 1
except:
print("something wrong with gdal open")
FAIL += 1
pass
print("total number of cropped images: ", COUNT)
print("total number of failed images: ", FAIL)
print("finish")