Подтвердить что ты не робот

Размер изображения (Python, OpenCV)

Я хотел бы получить размер изображения в python, так как я делаю это с помощью С++.

int w = src->width;
printf("%d", 'w');
4b9b3361

Ответ 1

Используйте функцию GetSize из модуля cv с вашим изображением в качестве параметра. Он возвращает ширину, высоту как кортеж с двумя элементами:

width, height = cv.GetSize(src)

Ответ 2

Используя openCV и numpy, это так просто:

import cv2

img = cv2.imread('path/to/img',0)
height, width = img.shape[:2]

Ответ 3

Я использую numpy.size(), чтобы сделать то же самое:

import numpy as np
import cv2

image = cv2.imread('image.jpg')
height = np.size(image, 0)
width = np.size(image, 1)

Ответ 4

Для меня самый простой способ - взять все значения, возвращенные image.shape:

height, width, channels = img.shape

если вы не хотите, чтобы количество каналов (полезно определить, является ли изображение bgr или оттенком серого), просто снимите значение:

height, width, _ = img.shape

Ответ 5

Вот метод, который возвращает размеры изображения:

from PIL import Image
import os

def get_image_dimensions(imagefile):
    """
    Helper function that returns the image dimentions

    :param: imagefile str (path to image)
    :return dict (of the form: {width:<int>, height=<int>, size_bytes=<size_bytes>)
    """
    # Inline import for PIL because it is not a common library
    with Image.open(imagefile) as img:
        # Calculate the width and hight of an image
        width, height = img.size

    # calculat ethe size in bytes
    size_bytes = os.path.getsize(imagefile)

    return dict(width=width, height=height, size_bytes=size_bytes)

Ответ 6

Мы можем использовать frame = cv2.resize(frame, (width,height)) вот простой пример -

import cv2
import numpy as np


frame = cv2.imread("temp/i.jpg")
frame = cv2.resize(frame, (500,500))
cv2.imshow('frame', frame)



if cv2.waitKey(1) & 0xFF == ord('q'):
    break
cv2.destroyAllWindows()