# Copyright (C) 2021-2025, Mindee.# This program is licensed under the Apache License 2.0.# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.frompathlibimportPathimportcv2importnumpyasnpfromdoctr.utils.common_typesimportAbstractFile__all__=["read_img_as_numpy"]
[docs]defread_img_as_numpy(file:AbstractFile,output_size:tuple[int,int]|None=None,rgb_output:bool=True,)->np.ndarray:"""Read an image file into numpy format >>> from doctr.io import read_img_as_numpy >>> page = read_img_as_numpy("path/to/your/doc.jpg") Args: file: the path to the image file output_size: the expected output size of each page in format H x W rgb_output: whether the output ndarray channel order should be RGB instead of BGR. Returns: the page decoded as numpy ndarray of shape H x W x 3 """ifisinstance(file,(str,Path)):ifnotPath(file).is_file():raiseFileNotFoundError(f"unable to access {file}")img=cv2.imread(str(file),cv2.IMREAD_COLOR)elifisinstance(file,bytes):_file:np.ndarray=np.frombuffer(file,np.uint8)img=cv2.imdecode(_file,cv2.IMREAD_COLOR)else:raiseTypeError("unsupported object type for argument 'file'")# Validity checkifimgisNone:raiseValueError("unable to read file.")# Resizingifisinstance(output_size,tuple):img=cv2.resize(img,output_size[::-1],interpolation=cv2.INTER_LINEAR)# Switch the channel orderifrgb_output:img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)returnimg