ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
load_image.py 827 B
from PIL import Image
import numpy as np
def ft_load(path: str) -> np.ndarray:
"""Load an image, print its shape and return its RGB pixel content.
On any failure a clear error message is printed and None is returned.
"""
try:
if not isinstance(path, str):
raise TypeError("path must be a string")
with Image.open(path) as image:
array = np.array(image.convert("RGB"))
print(f"The shape of image is: {array.shape}")
return array
except FileNotFoundError:
print(f"Error: file '{path}' not found")
except Exception as error:
print(f"Error: {error}")
return None
def main() -> None:
"""Load the landscape image and print its pixel content."""
print(ft_load("../landscape.jpg"))
if __name__ == "__main__":
main()