Методы получения ширины и высоты изображения в Python

Чтобы получить ширину и высоту изображения в Python, вы можете использовать различные методы в зависимости от библиотек, с которыми вы предпочитаете работать. Вот несколько часто используемых методов с использованием разных библиотек:

  1. Использование библиотеки PIL/Pillow:

    from PIL import Image
    image = Image.open("image.jpg")  # Replace "image.jpg" with the path to your image file
    width, height = image.size
    print("Width:", width)
    print("Height:", height)
  2. Использование библиотеки OpenCV:

    import cv2
    image = cv2.imread("image.jpg")  # Replace "image.jpg" with the path to your image file
    height, width, _ = image.shape
    print("Width:", width)
    print("Height:", height)
  3. Использование библиотеки imageio:

    import imageio
    image = imageio.imread("image.jpg")  # Replace "image.jpg" with the path to your image file
    height, width, _ = image.shape
    print("Width:", width)
    print("Height:", height)
  4. Использование библиотеки wand (требуется ImageMagick):

    from wand.image import Image
    with Image(filename="image.jpg") as img:  # Replace "image.jpg" with the path to your image file
    width = img.width
    height = img.height
    print("Width:", width)
    print("Height:", height)