OpenDroneMap-ODM/scripts/resize.py

60 wiersze
2.0 KiB
Python
Czysty Zwykły widok Historia

2015-11-19 12:03:22 +00:00
import cv2
import pyexiv2
2015-11-20 10:00:43 +00:00
from opendm import log
from opendm import system
from opendm import dataset
2015-11-19 12:03:22 +00:00
2015-11-20 10:00:43 +00:00
def resize(project_path, args, photos):
2015-11-19 12:03:22 +00:00
2015-11-20 10:00:43 +00:00
log.ODM_INFO('Preparing images - %s' % project_path)
# check if we have input data
if len(photos) == 0:
log.ODM_WARNING('Photos array is empty - Proceed to load images')
images_dir = dataset.join_paths(project_path, 'images')
photos = dataset.load_dataset(images_dir, args)
2015-11-19 12:03:22 +00:00
2015-11-19 14:45:27 +00:00
# preconditions
2015-11-19 12:03:22 +00:00
if len(photos) < 1:
log.ODM_ERROR('Not enough photos in photos to resize')
return False
2015-11-19 14:45:27 +00:00
# create working directory
2015-11-20 10:00:43 +00:00
working_dir = dataset.join_paths(project_path, 'images_resize')
2015-11-19 14:45:27 +00:00
system.mkdir_p(working_dir)
2015-11-19 12:03:22 +00:00
2015-11-19 14:45:27 +00:00
# loop over photos
2015-11-19 12:03:22 +00:00
for photo in photos:
try:
# open and resize image with opencv
img = cv2.imread(photo.path_file)
2015-11-19 14:45:27 +00:00
# compute new size
max_side = max(photo.width, photo.height)
ratio = float(args['resize_to']) / float(max_side)
img_r = cv2.resize(img, None, fx=ratio, fy=ratio)
2015-11-19 12:03:22 +00:00
# write image with opencv
2015-11-19 14:45:27 +00:00
new_path_file = dataset.join_paths(working_dir, photo.file_name)
2015-11-19 12:03:22 +00:00
cv2.imwrite(new_path_file, img_r)
2015-11-19 14:45:27 +00:00
# read metadata with pyexiv2
2015-11-19 12:03:22 +00:00
old_meta = pyexiv2.ImageMetadata(photo.path_file)
new_meta = pyexiv2.ImageMetadata(new_path_file)
2015-11-19 14:45:27 +00:00
old_meta.read()
2015-11-19 12:03:22 +00:00
new_meta.read()
# copy metadata
old_meta.copy(new_meta)
new_meta.write()
# log message
log.ODM_DEBUG('Resized image %s | dimensions: %s' % \
(photo.file_name, img_r.shape))
2015-11-19 14:45:27 +00:00
# TODO(edgar): update photos array
2015-11-19 12:03:22 +00:00
except cv2.error as e:
# something went wrong with this image
log.ODM_ERROR('Could not resize image %s' % photo.file_name)
log.ODM_ERROR('%s' % e)
return False
2015-11-20 10:00:43 +00:00
log.ODM_INFO('Resized %s images' % len(photos))
2015-11-19 12:03:22 +00:00
return True