2019-06-11 20:29:46 +00:00
|
|
|
from PIL import Image
|
|
|
|
import cv2
|
2019-03-07 17:36:05 +00:00
|
|
|
|
2019-06-11 20:29:46 +00:00
|
|
|
from opendm import log
|
2019-03-07 17:36:05 +00:00
|
|
|
|
2019-06-11 20:29:46 +00:00
|
|
|
Image.MAX_IMAGE_PIXELS = None
|
2019-03-07 17:36:05 +00:00
|
|
|
|
2019-06-11 20:29:46 +00:00
|
|
|
def get_image_size(file_path, fallback_on_error=True):
|
2019-03-07 17:36:05 +00:00
|
|
|
"""
|
2019-06-11 20:29:46 +00:00
|
|
|
Return (width, height) for a given img file
|
2019-03-07 17:36:05 +00:00
|
|
|
"""
|
2019-06-11 20:29:46 +00:00
|
|
|
try:
|
|
|
|
with Image.open(file_path) as img:
|
|
|
|
width, height = img.size
|
|
|
|
except Exception as e:
|
|
|
|
if fallback_on_error:
|
2019-06-25 15:04:54 +00:00
|
|
|
log.ODM_WARNING("Cannot read %s with PIL, fallback to cv2: %s" % (file_path, str(e)))
|
2019-06-11 20:29:46 +00:00
|
|
|
img = cv2.imread(file_path)
|
|
|
|
width = img.shape[1]
|
|
|
|
height = img.shape[0]
|
2019-03-07 17:36:05 +00:00
|
|
|
else:
|
2019-06-11 20:29:46 +00:00
|
|
|
raise e
|
2019-03-07 17:36:05 +00:00
|
|
|
|
2019-06-11 20:29:46 +00:00
|
|
|
return (width, height)
|