Чтобы получить ширину и высоту изображения в Python, вы можете использовать различные методы в зависимости от библиотек, с которыми вы предпочитаете работать. Вот несколько часто используемых методов с использованием разных библиотек:
-
Использование библиотеки 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) -
Использование библиотеки 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) -
Использование библиотеки 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) -
Использование библиотеки 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)