图像尺寸(Python中,OpenCV的)(Image size (Python, OpenCV))

2019-07-02 09:34发布

我想获得的图像的大小蟒蛇,因为我用C ++做。

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

Answer 1:

使用功能GetSize从模块cv与你的形象作为参数。 它返回宽度,高度与2个元素的元组:

width, height = cv.GetSize(src)


Answer 2:

使用opencv和numpy的是,因为这很容易:

import numpy as np
import cv2

img = cv2.imread('your_image.jpg',0)
height, width = img.shape[:2]


Answer 3:

我用numpy.size()做相同的:

import numpy as np
import cv2

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


Answer 4:

对我来说,最简单的方法是采取由image.shape返回的所有值:

height, width, channels = img.shape

如果你不想要的通道数(以确定是否该图像是BGR或灰度有用)刚落值:

height, width, _ = img.shape


Answer 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)


Answer 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()


文章来源: Image size (Python, OpenCV)