Load an image file and return its data as a list of NumPy arrays in RGB format.
This function reads an image file from the specified path, validates its existence
and format, and returns the image data as a list of NumPy arrays in RGB format.
For multi-page image formats like TIFF, each page is returned as a separate array.
引数:
| 名前 |
タイプ |
デスクリプション |
デフォルト |
image_path
|
str
|
The path to the image file to be loaded.
|
必須
|
戻り値:
| タイプ |
デスクリプション |
list[ndarray]
|
list[np.ndarray]: A list of NumPy arrays, where each array represents an image
|
list[ndarray]
|
in RGB format. For single-page images, the list contains one element.
|
発生:
| タイプ |
デスクリプション |
FileNotFoundError
|
If the specified file does not exist.
|
ValueError
|
- If the file format is not supported.
- If the file contains invalid image data.
- If the file is a PDF.
- If any side of the image (width or height) is less than 32 pixels.
|
Notes
- Supported input formats are: ["jpg", "jpeg", "png", "bmp", "tiff", "tif", "pdf"].
- Multi-page formats like TIFF are supported, and each page is returned as a
separate NumPy array.
- The returned image data is in BGR format, which is commonly used for image
processing tasks.
- PDF files are not supported by this function. Use
load_pdf() for handling PDFs.
Example
from yomitoku.data.functions import load_image
pages = load_image("example.tiff")
print(len(pages)) # Output: Number of pages in the TIFF file
print(pages[0].shape) # Output: (height, width, channels) for the first page
ソースコード位置: src/yomitoku/data/functions.py
| def load_image(image_path: str) -> list[np.ndarray]:
"""
Load an image file and return its data as a list of NumPy arrays in RGB format.
This function reads an image file from the specified path, validates its existence
and format, and returns the image data as a list of NumPy arrays in RGB format.
For multi-page image formats like TIFF, each page is returned as a separate array.
Args:
image_path (str): The path to the image file to be loaded.
Returns:
list[np.ndarray]: A list of NumPy arrays, where each array represents an image
in RGB format. For single-page images, the list contains one element.
Raises:
FileNotFoundError: If the specified file does not exist.
ValueError:
- If the file format is not supported.
- If the file contains invalid image data.
- If the file is a PDF.
- If any side of the image (width or height) is less than 32 pixels.
Notes:
- Supported input formats are: ["jpg", "jpeg", "png", "bmp", "tiff", "tif", "pdf"].
- Multi-page formats like TIFF are supported, and each page is returned as a
separate NumPy array.
- The returned image data is in BGR format, which is commonly used for image
processing tasks.
- PDF files are not supported by this function. Use `load_pdf()` for handling PDFs.
Example:
```python
from yomitoku.data.functions import load_image
pages = load_image("example.tiff")
print(len(pages)) # Output: Number of pages in the TIFF file
print(pages[0].shape) # Output: (height, width, channels) for the first page
```
"""
image_path = Path(image_path)
if not image_path.exists():
raise make_error(ErrorCode.IMAGE_FILE_NOT_FOUND)
ext = image_path.suffix[1:].lower()
if ext not in SUPPORT_INPUT_FORMAT:
raise make_error(ErrorCode.UNSUPPORTED_IMAGE_FORMAT)
if ext == "pdf":
raise make_error(ErrorCode.PDF_NOT_SUPPORTED_BY_LOAD_IMAGE)
try:
img = Image.open(image_path)
except Exception:
raise make_error(ErrorCode.INVALID_IMAGE_DATA)
pages = []
if ext in ["tif", "tiff"]:
try:
while True:
img_arr = np.array(img.copy().convert("RGB"))
validate_image(img_arr)
pages.append(img_arr[:, :, ::-1])
img.seek(img.tell() + 1)
except EOFError:
pass
else:
img_arr = np.array(img.convert("RGB"))
validate_image(img_arr)
pages.append(img_arr[:, :, ::-1])
return pages
|