OpenDroneMap-ODM/opendm/get_image_size.py

30 wiersze
869 B
Python

from PIL import Image
import cv2
2025-02-23 19:22:38 +00:00
import rawpy
from opendm import log
Image.MAX_IMAGE_PIXELS = None
def get_image_size(file_path, fallback_on_error=True):
"""
Return (width, height) for a given img file
"""
2025-02-23 19:22:38 +00:00
try:
2025-02-23 19:22:38 +00:00
if file_path[-4:].lower() in [".dng", ".raw", ".nef"]:
with rawpy.imread(file_path) as img:
s = img.sizes
width, height = s.raw_width, s.raw_height
else:
with Image.open(file_path) as img:
width, height = img.size
except Exception as e:
if fallback_on_error:
2025-02-23 19:22:38 +00:00
log.ODM_WARNING("Cannot read %s with image library, fallback to cv2: %s" % (file_path, str(e)))
img = cv2.imread(file_path)
width = img.shape[1]
height = img.shape[0]
else:
raise e
return (width, height)