from rawpy import imread
import imageio
from rawpy._rawpy import LibRawNoThumbnailError, LibRawUnsupportedThumbnailError, ThumbFormat


def extract_jpg_thumb(raw_file_path):

    with imread(raw_file_path) as raw:
        try:
            thumb = raw.extract_thumb()
        except (LibRawNoThumbnailError, LibRawUnsupportedThumbnailError):
            return None

        if thumb.format == ThumbFormat.JPEG:
            with open('thumbnail.jpg', 'wb') as f:
                f.write(thumb.data)
        elif thumb.format == ThumbFormat.BITMAP:
            imageio.imsave('thumbnail.jpg', thumb.data)
        else:
            return None

    return 'thumbnail.jpg'

def get_raw_image_dimensions(raw_file_path):
    i = imread(raw_file_path)
    height, width = i.raw_image.shape[:2]

    return height,width