Last active
November 11, 2017 06:14
-
-
Save jkatagi/ca172d0546aabf712710b80ee3bc6fea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# make patch images. | |
# 2017/03/17 | |
# todo: true_color, false_colorを指定 | |
import subprocess | |
import argparse | |
import os | |
import re | |
def main(): | |
parser = argparse.ArgumentParser(usage="make patch images from satellite imgase.\n" + | |
"%(prog)s [img_dir] [out_dir] [size] [center_lat] [center_lon]\n" + | |
"ex) %(prog)s /path/to/img_dir /path/to/out_dir 10 36.1 140.1") | |
parser.add_argument('img_dir', action='store', type=str, | |
help='directory where the input images are stored.') | |
parser.add_argument('out_dir', action='store', type=str, | |
help='directory whre the output images are stored.') | |
parser.add_argument('size', action='store', type=int, | |
help='radius size of the image to cut out. ') | |
parser.add_argument('center_lat', action='store', type=float, | |
help='center latitude of the image to cut out. ') | |
parser.add_argument('center_lon', action='store', type=float, | |
help='center longitude of the image to cut out. ') | |
args = parser.parse_args() | |
# set parameter | |
img_dir = args.img_dir | |
out_dir = args.out_dir | |
size = args.size | |
center_lat = args.center_lat | |
center_lon = args.center_lon | |
# get img list from img_dir | |
img_list = get_img_list(img_dir) | |
print("start making patch images ...") | |
for img_file in img_list: | |
make_patch_image(img_dir, img_file, out_dir, center_lat, center_lon, size) | |
print("complete process.") | |
def get_img_list(imgdir): | |
""" get tif list from imgdir.(helper functoin.)""" | |
imgdir_list = os.listdir(imgdir) | |
img_list = [f for f in imgdir_list if re.match(r'.*tif$', f)] | |
return img_list | |
def make_patch_image(img_dir, img_file, out_dir,center_lat, center_lon, size): | |
""" make patch image using gdal_translate command. | |
input: | |
img_dir : img directory. | |
img_file: img file which you want to cut. | |
out_dir : save directory. | |
center_lat: center latitude. | |
centa_lon: center longitude. | |
size: (unit:meter) | |
output: | |
""" | |
# assume 1deg=100km; so 1m=8.33333333333333e-05 | |
one_meter=8.33333333333333e-05 | |
distance = one_meter * size # unit: degree | |
# set coordinate. | |
xmin = str(center_lon - distance) | |
xmax = str(center_lon + distance) | |
ymin = str(center_lat - distance) | |
ymax = str(center_lat + distance) | |
size = str(size * 2 + 1) | |
cut_command = ["gdalwarp", "-te", | |
xmin, ymin, xmax, ymax, | |
"-ts", size, size, | |
"-overwrite", | |
img_dir + "/" + img_file, | |
out_dir + "/" + img_file] | |
png_command = ["gdal_translate", "-of", "PNG", | |
"-b", "4", "-b", "3", "-b", "2", | |
out_dir + "/" + img_file, | |
out_dir + "/" + img_file + ".png"] | |
# run gdal_command | |
subprocess.run(cut_command) | |
subprocess.run(png_command) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 2017/03/17 Jin Katagi | |
# make_patch_image.py を呼ぶ。 | |
# usage) $ ./run_make_patch_img.sh | |
validation_file="../csv/50points_coordinates_category.csv" | |
AV2_dir="/path/to/AV2_dir" | |
size=50 # 50m | |
total_line=500 | |
for line in `seq 1 ${total_line}`; do | |
id=` head -${line} ${validation_file}| tail -1 | awk -F "," '{print $1}'` | |
lat=`head -${line} ${validation_file} | tail -1 | awk -F "," '{print $4}'` | |
lon=`head -${line} ${validation_file} | tail -1 | awk -F "," '{print $3}'` | |
png_dir="../AV2_patch/false_color/png/${id}" | |
gif_dir="../AV2_patch/false_color/gif" | |
mkdir -p ${png_dir} ${gif_dir} | |
int_lat=`python -c "print(int(${lat}))"` | |
int_lon=`python -c "print(int(${lon}))"` | |
# パッチ画像を作成。 | |
# さらにpngに変換。 | |
python3 make_patch_img.py ${AV2_dir}/N${int_lat}E${int_lon} ${png_dir} ${size} ${lat} ${lon} | |
# 真っ黒な画像を削除 | |
for file in `ls ${png_dir}/*.png`; do | |
echo ${file} | |
null_or_not=`compare -metric PSNR ${file} ../null.png tmp.png 2>&1` | |
# nullの場合、inf | |
if [ "${null_or_not}" == "inf" ] ; then | |
rm ${file} | |
fi | |
done | |
# gifアニメに変更 | |
convert ${png_dir}/*.png ${gif_dir}/${id}_tmp.gif | |
# gifアニメを遅くする | |
convert -delay 50 ${gif_dir}/${id}_tmp.gif ${gif_dir}/${id}_tmp2.gif | |
# gif画像のサイズを3倍にし、直径10mの円を描く。 | |
convert -resize 300% -fill none -stroke black -draw 'circle 150,150 153,150' ${gif_dir}/${id}_tmp2.gif ${gif_dir}/${id}.gif | |
rm ${gif_dir}/*tmp* | |
done # line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment